summaryrefslogtreecommitdiff
path: root/app/Core/Queue/QueueManager.php
blob: f34cb220e04966e4e6aaf28fe614a677b4071d06 (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
64
65
66
67
68
69
70
71
<?php

namespace Kanboard\Core\Queue;

use Kanboard\Core\Base;
use Kanboard\Job\BaseJob;
use LogicException;
use SimpleQueue\Queue;

/**
 * Class QueueManager
 *
 * @package Kanboard\Core\Queue
 * @author  Frederic Guillot
 */
class QueueManager extends Base
{
    /**
     * @var Queue
     */
    protected $queue = null;

    /**
     * Set queue driver
     *
     * @access public
     * @param  Queue $queue
     * @return $this
     */
    public function setQueue(Queue $queue)
    {
        $this->queue = $queue;
        return $this;
    }

    /**
     * Send a new job to the queue
     *
     * @access public
     * @param  BaseJob $job
     * @return $this
     */
    public function push(BaseJob $job)
    {
        if ($this->queue !== null) {
            $this->queue->push(JobHandler::getInstance($this->container)->serializeJob($job));
        } else {
            call_user_func_array(array($job, 'execute'), $job->getJobParams());
        }

        return $this;
    }

    /**
     * Wait for new jobs
     *
     * @access public
     * @throws LogicException
     */
    public function listen()
    {
        if ($this->queue === null) {
            throw new LogicException('No Queue Driver defined!');
        }

        while ($job = $this->queue->pull()) {
            JobHandler::getInstance($this->container)->executeJob($job);
            $this->queue->completed($job);
        }
    }
}