aboutsummaryrefslogtreecommitdiff
path: root/src/phinde/Queue.php
blob: a8ead3d56d5bc682bbcc24b0c0e00a613acdf0e7 (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
62
63
<?php
namespace phinde;

class Queue
{
    protected $gmclient;

    public function __construct()
    {
        $this->gmclient = new \GearmanClient();
        $this->gmclient->addServer('127.0.0.1');
        $this->queueName = $GLOBALS['phinde']['queuePrefix'] . 'phinde_process';
    }

    public function addToProcessList($linkUrl, $actions)
    {
        Log::info(
            "Queuing for processing: $linkUrl"
            . ' (' . implode(',', $actions) . ')'
        );

        $this->gmclient->doBackground(
            $this->queueName,
            serialize(
                array(
                    'url'     => $linkUrl,
                    'actions' => $actions,
                )
            )
        );
        if ($this->gmclient->returnCode() != GEARMAN_SUCCESS) {
            Log::error(
                'Error queueing URL processing for '
                . $linkUrl . "\n"
                . 'Error code: ' . $this->gmclient->returnCode()
            );
            exit(2);
        }
    }

    public function getServerStatus()
    {
        $cmd = 'gearadmin --status'
            . '| grep ' . escapeshellarg($this->queueName);
        $line = exec($cmd);
        if ($line === '') {
            //job not registered
            $parts = [0, -1, -1, -1];
        } else {
            $parts = preg_split('#\s+#', $line);
            if (count($parts) !== 4) {
                throw new \Exception('gearadmin status line does not have 4 parts');
            }
        }

        return array(
            'tasks'      => $parts[1],
            'processing' => $parts[2],
            'workers'    => $parts[3],
        );
    }
}
?>