aboutsummaryrefslogtreecommitdiff
path: root/bin/phinde-worker.php
blob: 939db1f9a37691dbbd016072ce1608e23415860c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env php
<?php
namespace phinde;

chdir(dirname($argv[0]));

require_once __DIR__ . '/../src/init.php';

$cc = new \Console_CommandLine();
$cc->description = 'phinde queue worker';
$cc->version = '0.0.1';
$cc->addArgument(
    'queues',
    array(
        'description' => 'Queue(s) to process',
        'multiple'    => true,
        'default'     => array('crawl', 'index'),
        'choices'     => array('crawl', 'index'),
        'optional'    => true,
    )
);
try {
    $res = $cc->parse();
} catch (\Exception $e) {
    $cc->displayError($e->getMessage());
}

$queues = array_flip(array_unique($res->args['queues']));

$gmworker = new \GearmanWorker();
$gmworker->addServer('127.0.0.1');

if (isset($queues['crawl'])) {
    $gmworker->addFunction(
        $GLOBALS['phinde']['queuePrefix'] . 'phinde_crawl',
        function(\GearmanJob $job) {
            $data = unserialize($job->workload());
            echo "-- Crawling " . $data['url'] . "\n";
            passthru('./crawl.php ' . escapeshellarg($data['url']));
        }
    );
}
if (isset($queues['index'])) {
    $gmworker->addFunction(
        $GLOBALS['phinde']['queuePrefix'] . 'phinde_index',
        function(\GearmanJob $job) {
            $data = unserialize($job->workload());
            echo "-- Indexing " . $data['url'] . "\n";
            passthru('./index.php ' . escapeshellarg($data['url']));
            //exit();
        }
    );
}

while ($gmworker->work()) {
    if ($gmworker->returnCode() != GEARMAN_SUCCESS) {
        echo 'Error running job: ' . $gmworker->returnCode() . "\n";
        break;
    }
}
?>