Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Queue\Middleware;
4
 
5
use Illuminate\Container\Container;
6
use Illuminate\Contracts\Cache\Repository as Cache;
7
use Illuminate\Support\InteractsWithTime;
8
 
9
class WithoutOverlapping
10
{
11
    use InteractsWithTime;
12
 
13
    /**
14
     * The job's unique key used for preventing overlaps.
15
     *
16
     * @var string
17
     */
18
    public $key;
19
 
20
    /**
21
     * The number of seconds before a job should be available again if no lock was acquired.
22
     *
23
     * @var \DateTimeInterface|int|null
24
     */
25
    public $releaseAfter;
26
 
27
    /**
28
     * The number of seconds before the lock should expire.
29
     *
30
     * @var int
31
     */
32
    public $expiresAfter;
33
 
34
    /**
35
     * The prefix of the lock key.
36
     *
37
     * @var string
38
     */
39
    public $prefix = 'laravel-queue-overlap:';
40
 
41
    /**
42
     * Share the key across different jobs.
43
     *
44
     * @var bool
45
     */
46
    public $shareKey = false;
47
 
48
    /**
49
     * Create a new middleware instance.
50
     *
51
     * @param  string  $key
52
     * @param  \DateTimeInterface|int|null  $releaseAfter
53
     * @param  \DateTimeInterface|int  $expiresAfter
54
     * @return void
55
     */
56
    public function __construct($key = '', $releaseAfter = 0, $expiresAfter = 0)
57
    {
58
        $this->key = $key;
59
        $this->releaseAfter = $releaseAfter;
60
        $this->expiresAfter = $this->secondsUntil($expiresAfter);
61
    }
62
 
63
    /**
64
     * Process the job.
65
     *
66
     * @param  mixed  $job
67
     * @param  callable  $next
68
     * @return mixed
69
     */
70
    public function handle($job, $next)
71
    {
72
        $lock = Container::getInstance()->make(Cache::class)->lock(
73
            $this->getLockKey($job), $this->expiresAfter
74
        );
75
 
76
        if ($lock->get()) {
77
            try {
78
                $next($job);
79
            } finally {
80
                $lock->release();
81
            }
82
        } elseif (! is_null($this->releaseAfter)) {
83
            $job->release($this->releaseAfter);
84
        }
85
    }
86
 
87
    /**
88
     * Set the delay (in seconds) to release the job back to the queue.
89
     *
90
     * @param  \DateTimeInterface|int  $releaseAfter
91
     * @return $this
92
     */
93
    public function releaseAfter($releaseAfter)
94
    {
95
        $this->releaseAfter = $releaseAfter;
96
 
97
        return $this;
98
    }
99
 
100
    /**
101
     * Do not release the job back to the queue if no lock can be acquired.
102
     *
103
     * @return $this
104
     */
105
    public function dontRelease()
106
    {
107
        $this->releaseAfter = null;
108
 
109
        return $this;
110
    }
111
 
112
    /**
113
     * Set the maximum number of seconds that can elapse before the lock is released.
114
     *
115
     * @param  \DateTimeInterface|\DateInterval|int  $expiresAfter
116
     * @return $this
117
     */
118
    public function expireAfter($expiresAfter)
119
    {
120
        $this->expiresAfter = $this->secondsUntil($expiresAfter);
121
 
122
        return $this;
123
    }
124
 
125
    /**
126
     * Set the prefix of the lock key.
127
     *
128
     * @param  string  $prefix
129
     * @return $this
130
     */
131
    public function withPrefix(string $prefix)
132
    {
133
        $this->prefix = $prefix;
134
 
135
        return $this;
136
    }
137
 
138
    /**
139
     * Indicate that the lock key should be shared across job classes.
140
     *
141
     * @return $this
142
     */
143
    public function shared()
144
    {
145
        $this->shareKey = true;
146
 
147
        return $this;
148
    }
149
 
150
    /**
151
     * Get the lock key for the given job.
152
     *
153
     * @param  mixed  $job
154
     * @return string
155
     */
156
    public function getLockKey($job)
157
    {
158
        return $this->shareKey
159
            ? $this->prefix.$this->key
160
            : $this->prefix.get_class($job).':'.$this->key;
161
    }
162
}