Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Console\Scheduling;
4
 
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Support\Reflector;
7
use InvalidArgumentException;
8
use LogicException;
9
use RuntimeException;
10
use Throwable;
11
 
12
class CallbackEvent extends Event
13
{
14
    /**
15
     * The callback to call.
16
     *
17
     * @var string
18
     */
19
    protected $callback;
20
 
21
    /**
22
     * The parameters to pass to the method.
23
     *
24
     * @var array
25
     */
26
    protected $parameters;
27
 
28
    /**
29
     * The result of the callback's execution.
30
     *
31
     * @var mixed
32
     */
33
    protected $result;
34
 
35
    /**
36
     * The exception that was thrown when calling the callback, if any.
37
     *
38
     * @var \Throwable|null
39
     */
40
    protected $exception;
41
 
42
    /**
43
     * Create a new event instance.
44
     *
45
     * @param  \Illuminate\Console\Scheduling\EventMutex  $mutex
46
     * @param  string|callable  $callback
47
     * @param  array  $parameters
48
     * @param  \DateTimeZone|string|null  $timezone
49
     * @return void
50
     *
51
     * @throws \InvalidArgumentException
52
     */
53
    public function __construct(EventMutex $mutex, $callback, array $parameters = [], $timezone = null)
54
    {
55
        if (! is_string($callback) && ! Reflector::isCallable($callback)) {
56
            throw new InvalidArgumentException(
57
                'Invalid scheduled callback event. Must be a string or callable.'
58
            );
59
        }
60
 
61
        $this->mutex = $mutex;
62
        $this->callback = $callback;
63
        $this->parameters = $parameters;
64
        $this->timezone = $timezone;
65
    }
66
 
67
    /**
68
     * Run the callback event.
69
     *
70
     * @param  \Illuminate\Contracts\Container\Container  $container
71
     * @return mixed
72
     *
73
     * @throws \Throwable
74
     */
75
    public function run(Container $container)
76
    {
77
        parent::run($container);
78
 
79
        if ($this->exception) {
80
            throw $this->exception;
81
        }
82
 
83
        return $this->result;
84
    }
85
 
86
    /**
87
     * Determine if the event should skip because another process is overlapping.
88
     *
89
     * @return bool
90
     */
91
    public function shouldSkipDueToOverlapping()
92
    {
93
        return $this->description && parent::shouldSkipDueToOverlapping();
94
    }
95
 
96
    /**
97
     * Indicate that the callback should run in the background.
98
     *
99
     * @return void
100
     *
101
     * @throws \RuntimeException
102
     */
103
    public function runInBackground()
104
    {
105
        throw new RuntimeException('Scheduled closures can not be run in the background.');
106
    }
107
 
108
    /**
109
     * Run the callback.
110
     *
111
     * @param  \Illuminate\Contracts\Container\Container  $container
112
     * @return int
113
     */
114
    protected function execute($container)
115
    {
116
        try {
117
            $this->result = is_object($this->callback)
118
                ? $container->call([$this->callback, '__invoke'], $this->parameters)
119
                : $container->call($this->callback, $this->parameters);
120
 
121
            return $this->result === false ? 1 : 0;
122
        } catch (Throwable $e) {
123
            $this->exception = $e;
124
 
125
            return 1;
126
        }
127
    }
128
 
129
    /**
130
     * Do not allow the event to overlap each other.
131
     *
621 lars 132
     * The expiration time of the underlying cache lock may be specified in minutes.
133
     *
148 lars 134
     * @param  int  $expiresAt
135
     * @return $this
136
     *
137
     * @throws \LogicException
138
     */
139
    public function withoutOverlapping($expiresAt = 1440)
140
    {
141
        if (! isset($this->description)) {
142
            throw new LogicException(
143
                "A scheduled event name is required to prevent overlapping. Use the 'name' method before 'withoutOverlapping'."
144
            );
145
        }
146
 
147
        return parent::withoutOverlapping($expiresAt);
148
    }
149
 
150
    /**
151
     * Allow the event to only run on one server for each cron expression.
152
     *
153
     * @return $this
154
     *
155
     * @throws \LogicException
156
     */
157
    public function onOneServer()
158
    {
159
        if (! isset($this->description)) {
160
            throw new LogicException(
161
                "A scheduled event name is required to only run on one server. Use the 'name' method before 'onOneServer'."
162
            );
163
        }
164
 
165
        return parent::onOneServer();
166
    }
167
 
168
    /**
169
     * Get the summary of the event for display.
170
     *
171
     * @return string
172
     */
173
    public function getSummaryForDisplay()
174
    {
175
        if (is_string($this->description)) {
176
            return $this->description;
177
        }
178
 
179
        return is_string($this->callback) ? $this->callback : 'Callback';
180
    }
181
 
182
    /**
183
     * Get the mutex name for the scheduled command.
184
     *
185
     * @return string
186
     */
187
    public function mutexName()
188
    {
189
        return 'framework/schedule-'.sha1($this->description ?? '');
190
    }
191
 
192
    /**
193
     * Clear the mutex for the event.
194
     *
195
     * @return void
196
     */
197
    protected function removeMutex()
198
    {
199
        if ($this->description) {
200
            parent::removeMutex();
201
        }
202
    }
203
}