remove anchor from source URLs
[phinde.git] / bin / phinde-worker.php
1 #!/usr/bin/env php
2 <?php
3 namespace phinde;
4
5 chdir(dirname($argv[0]));
6
7 require_once __DIR__ . '/../src/init.php';
8
9 $cc = new \Console_CommandLine();
10 $cc->description = 'phinde queue worker';
11 $cc->version = '0.0.1';
12 $cc->addArgument(
13     'queues',
14     array(
15         'description' => 'Queue(s) to process',
16         'multiple'    => true,
17         'default'     => array('crawl', 'index'),
18         'choices'     => array('crawl', 'index'),
19         'optional'    => true,
20     )
21 );
22 try {
23     $res = $cc->parse();
24 } catch (\Exception $e) {
25     $cc->displayError($e->getMessage());
26 }
27
28 $queues = array_flip(array_unique($res->args['queues']));
29
30 $gmworker = new \GearmanWorker();
31 $gmworker->addServer('127.0.0.1');
32
33 if (isset($queues['crawl'])) {
34     $gmworker->addFunction(
35         $GLOBALS['phinde']['queuePrefix'] . 'phinde_crawl',
36         function(\GearmanJob $job) {
37             $data = unserialize($job->workload());
38             echo "-- Crawling " . $data['url'] . "\n";
39             passthru('./crawl.php ' . escapeshellarg($data['url']));
40         }
41     );
42 }
43 if (isset($queues['index'])) {
44     $gmworker->addFunction(
45         $GLOBALS['phinde']['queuePrefix'] . 'phinde_index',
46         function(\GearmanJob $job) {
47             $data = unserialize($job->workload());
48             echo "-- Indexing " . $data['url'] . "\n";
49             passthru('./index.php ' . escapeshellarg($data['url']));
50             //exit();
51         }
52     );
53 }
54
55 while ($gmworker->work()) {
56     if ($gmworker->returnCode() != GEARMAN_SUCCESS) {
57         echo 'Error running job: ' . $gmworker->returnCode() . "\n";
58         break;
59     }
60 }
61 ?>