summaryrefslogtreecommitdiff
path: root/vendor/fguillot/simple-queue/src/Adapter/DisqueQueueAdapter.php
blob: 047658d77d1636ead96a8a2aa0d9e50c398ae7f3 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

namespace SimpleQueue\Adapter;

use DateTime;
use Disque\Client as DisqueClient;
use Disque\Queue\Job as DisqueJob;
use SimpleQueue\Job;
use SimpleQueue\QueueAdapterInterface;

/**
 * Class DisqueQueueAdapter
 *
 * @package SimpleQueue\Adapter
 */
class DisqueQueueAdapter implements QueueAdapterInterface
{
    /**
     * @var DisqueClient
     */
    protected $disque;

    /**
     * @var string
     */
    protected $queueName;

    /**
     * DisqueQueueAdapter constructor.
     *
     * @param DisqueClient $disque
     * @param string       $queueName
     */
    public function __construct(DisqueClient $disque, $queueName)
    {
        $this->disque = $disque;
        $this->queueName = $queueName;
    }

    /**
     * Send a job
     *
     * @access public
     * @param  Job $job
     * @return $this
     */
    public function push(Job $job)
    {
        $this->disque->queue($this->queueName)->push(new DisqueJob($job->getBody()));
        return $this;
    }

    /**
     * Schedule a job in the future
     *
     * @access public
     * @param  Job      $job
     * @param  DateTime $dateTime
     * @return $this
     */
    public function schedule(Job $job, DateTime $dateTime)
    {
        $this->disque->queue($this->queueName)->schedule(new DisqueJob($job->serialize()), $dateTime);
        return $this;
    }

    /**
     * Wait and get job from a queue
     *
     * @access public
     * @return Job|null
     */
    public function pull()
    {
        $disqueJob = $this->disque->queue($this->queueName)->pull();

        if ($disqueJob === null) {
            return null;
        }

        return new Job($disqueJob->getBody(), $disqueJob->getId());
    }

    /**
     * Acknowledge a job
     *
     * @access public
     * @param  Job $job
     * @return $this
     */
    public function completed(Job $job)
    {
        $this->disque->queue($this->queueName)->processed(new DisqueJob($job->getBody(), $job->getId()));
        return $this;
    }

    /**
     * Mark a job as failed
     *
     * @access public
     * @param  Job $job
     * @return $this
     */
    public function failed(Job $job)
    {
        $this->disque->queue($this->queueName)->failed(new DisqueJob($job->getBody(), $job->getId()));
        return $this;
    }
}