status page
[phinde.git] / src / phinde / Queue.php
1 <?php
2 namespace phinde;
3
4 class Queue
5 {
6     protected $gmclient;
7
8     public function __construct()
9     {
10         $this->gmclient = new \GearmanClient();
11         $this->gmclient->addServer('127.0.0.1');
12         $this->queueName = $GLOBALS['phinde']['queuePrefix'] . 'phinde_process';
13     }
14
15     public function addToProcessList($linkUrl, $actions)
16     {
17         Log::info(
18             "Queuing for processing: $linkUrl"
19             . ' (' . implode(',', $actions) . ')'
20         );
21
22         $this->gmclient->doBackground(
23             $this->queueName,
24             serialize(
25                 array(
26                     'url'     => $linkUrl,
27                     'actions' => $actions,
28                 )
29             )
30         );
31         if ($this->gmclient->returnCode() != GEARMAN_SUCCESS) {
32             Log::error(
33                 'Error queueing URL processing for '
34                 . $linkUrl . "\n"
35                 . 'Error code: ' . $this->gmclient->returnCode()
36             );
37             exit(2);
38         }
39     }
40
41     public function getServerStatus()
42     {
43         $cmd = 'gearadmin --status'
44             . '| grep ' . escapeshellarg($this->queueName);
45         $line = exec($cmd);
46
47         $parts = preg_split('#\s+#', $line);
48         if (count($parts) !== 4) {
49             throw new \Exception('gearadmin status line does not have 4 parts');
50         }
51
52         return array(
53             'tasks'      => $parts[1],
54             'processing' => $parts[2],
55             'workers'    => $parts[3],
56         );
57     }
58 }
59 ?>