-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSchedule.php
More file actions
774 lines (676 loc) · 16.5 KB
/
Copy pathSchedule.php
File metadata and controls
774 lines (676 loc) · 16.5 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
<?php
declare(strict_types=1);
namespace Bow\Scheduler;
use DateTimeInterface;
use DateTimeZone;
class Schedule
{
/**
* The cron expression representing the task's frequency
*
* @var string
*/
protected string $expression = '* * * * *';
/**
* The timezone the date should be evaluated on
*
* @var ?DateTimeZone
*/
protected ?DateTimeZone $timezone = null;
/**
* Indicates if overlapping should be prevented
*
* @var bool
*/
protected bool $withoutOverlapping = false;
/**
* The number of minutes the mutex should be valid
*
* @var int
*/
protected int $expiresAt = 1440;
/**
* Indicates if output should be appended
*
* @var bool
*/
protected bool $runInBackground = false;
/**
* The array of callbacks to filter when the task should run
*
* @var array
*/
protected array $filters = [];
/**
* The array of callbacks to reject when the task should run
*
* @var array
*/
protected array $rejects = [];
/**
* The description of the scheduled task
*
* @var ?string
*/
protected ?string $description = null;
/**
* The owning scheduled event
*
* @var ?ScheduledEvent
*/
protected ?ScheduledEvent $event = null;
/**
* Create a new schedule instance
*
* @return void
*/
public function __construct()
{
//
}
/**
* Run the task every minute
*
* @return $this
*/
public function everyMinute(): static
{
return $this->spliceIntoPosition(1, '*');
}
/**
* Run the task every two minutes
*
* @return $this
*/
public function everyTwoMinutes(): static
{
return $this->spliceIntoPosition(1, '*/2');
}
/**
* Run the task every five minutes
*
* @return $this
*/
public function everyFiveMinutes(): static
{
return $this->spliceIntoPosition(1, '*/5');
}
/**
* Run the task every ten minutes
*
* @return $this
*/
public function everyTenMinutes(): static
{
return $this->spliceIntoPosition(1, '*/10');
}
/**
* Run the task every fifteen minutes
*
* @return $this
*/
public function everyFifteenMinutes(): static
{
return $this->spliceIntoPosition(1, '*/15');
}
/**
* Run the task every thirty minutes
*
* @return $this
*/
public function everyThirtyMinutes(): static
{
return $this->spliceIntoPosition(1, '0,30');
}
/**
* Run the task hourly
*
* @return $this
*/
public function hourly(): static
{
return $this->spliceIntoPosition(1, '0');
}
/**
* Run the task hourly at a given offset
*
* @param array|int $offset
* @return $this
*/
public function hourlyAt(array|int $offset): static
{
$offset = is_array($offset) ? implode(',', $offset) : $offset;
return $this->spliceIntoPosition(1, (string) $offset);
}
/**
* Run the task every two hours
*
* @return $this
*/
public function everyTwoHours(): static
{
return $this->spliceIntoPosition(1, '0')
->spliceIntoPosition(2, '*/2');
}
/**
* Run the task every three hours
*
* @return $this
*/
public function everyThreeHours(): static
{
return $this->spliceIntoPosition(1, '0')
->spliceIntoPosition(2, '*/3');
}
/**
* Run the task every four hours
*
* @return $this
*/
public function everyFourHours(): static
{
return $this->spliceIntoPosition(1, '0')
->spliceIntoPosition(2, '*/4');
}
/**
* Run the task every six hours
*
* @return $this
*/
public function everySixHours(): static
{
return $this->spliceIntoPosition(1, '0')
->spliceIntoPosition(2, '*/6');
}
/**
* Run the task daily
*
* @return $this
*/
public function daily(): static
{
return $this->spliceIntoPosition(1, '0')
->spliceIntoPosition(2, '0');
}
/**
* Run the task daily at a given time
*
* @param string $time
* @return $this
*/
public function dailyAt(string $time): static
{
$segments = explode(':', $time);
return $this->spliceIntoPosition(2, (int) $segments[0])
->spliceIntoPosition(1, count($segments) === 2 ? (int) $segments[1] : '0');
}
/**
* Run the task twice daily
*
* @param int $first
* @param int $second
* @return $this
*/
public function twiceDaily(int $first = 1, int $second = 13): static
{
return $this->spliceIntoPosition(1, '0')
->spliceIntoPosition(2, "{$first},{$second}");
}
/**
* Run the task weekly
*
* @return $this
*/
public function weekly(): static
{
return $this->spliceIntoPosition(1, '0')
->spliceIntoPosition(2, '0')
->spliceIntoPosition(5, '0');
}
/**
* Run the task weekly on a given day and time
*
* @param array|int $dayOfWeek
* @param string $time
* @return $this
*/
public function weeklyOn(array|int $dayOfWeek, string $time = '0:0'): static
{
$this->dailyAt($time);
$dayOfWeek = is_array($dayOfWeek) ? implode(',', $dayOfWeek) : $dayOfWeek;
return $this->spliceIntoPosition(5, (string) $dayOfWeek);
}
/**
* Run the task monthly
*
* @return $this
*/
public function monthly(): static
{
return $this->spliceIntoPosition(1, '0')
->spliceIntoPosition(2, '0')
->spliceIntoPosition(3, '1');
}
/**
* Run the task monthly on a given day and time
*
* @param int $dayOfMonth
* @param string $time
* @return $this
*/
public function monthlyOn(int $dayOfMonth = 1, string $time = '0:0'): static
{
$this->dailyAt($time);
return $this->spliceIntoPosition(3, (string) $dayOfMonth);
}
/**
* Run the task twice monthly
*
* @param int $first
* @param int $second
* @param string $time
* @return $this
*/
public function twiceMonthly(int $first = 1, int $second = 16, string $time = '0:0'): static
{
$this->dailyAt($time);
return $this->spliceIntoPosition(3, "{$first},{$second}");
}
/**
* Run the task quarterly
*
* @return $this
*/
public function quarterly(): static
{
return $this->spliceIntoPosition(1, '0')
->spliceIntoPosition(2, '0')
->spliceIntoPosition(3, '1')
->spliceIntoPosition(4, '1,4,7,10');
}
/**
* Run the task yearly
*
* @return $this
*/
public function yearly(): static
{
return $this->spliceIntoPosition(1, '0')
->spliceIntoPosition(2, '0')
->spliceIntoPosition(3, '1')
->spliceIntoPosition(4, '1');
}
/**
* Run the task yearly on a given month, day, and time
*
* @param int $month
* @param int $dayOfMonth
* @param string $time
* @return $this
*/
public function yearlyOn(int $month = 1, int $dayOfMonth = 1, string $time = '0:0'): static
{
$this->dailyAt($time);
return $this->spliceIntoPosition(3, (string) $dayOfMonth)
->spliceIntoPosition(4, (string) $month);
}
/**
* Schedule the task to run on given days of the week
*
* @param array|int|string $days
* @return $this
*/
public function days(array|int|string $days): static
{
$days = is_array($days) ? implode(',', $days) : $days;
return $this->spliceIntoPosition(5, (string) $days);
}
/**
* Schedule the task to run on Mondays
*
* @return $this
*/
public function mondays(): static
{
return $this->days(1);
}
/**
* Schedule the task to run on Tuesdays
*
* @return $this
*/
public function tuesdays(): static
{
return $this->days(2);
}
/**
* Schedule the task to run on Wednesdays
*
* @return $this
*/
public function wednesdays(): static
{
return $this->days(3);
}
/**
* Schedule the task to run on Thursdays
*
* @return $this
*/
public function thursdays(): static
{
return $this->days(4);
}
/**
* Schedule the task to run on Fridays
*
* @return $this
*/
public function fridays(): static
{
return $this->days(5);
}
/**
* Schedule the task to run on Saturdays
*
* @return $this
*/
public function saturdays(): static
{
return $this->days(6);
}
/**
* Schedule the task to run on Sundays
*
* @return $this
*/
public function sundays(): static
{
return $this->days(0);
}
/**
* Schedule the task to run on weekdays
*
* @return $this
*/
public function weekdays(): static
{
return $this->days('1-5');
}
/**
* Schedule the task to run on weekends
*
* @return $this
*/
public function weekends(): static
{
return $this->days('0,6');
}
/**
* Set the cron expression with a custom expression
*
* @param string $expression
* @return $this
*/
public function cron(string $expression): static
{
$this->expression = $expression;
return $this;
}
/**
* Set the timezone the date should be evaluated on
*
* @param DateTimeZone|string $timezone
* @return $this
*/
public function timezone(DateTimeZone|string $timezone): static
{
$this->timezone = $timezone instanceof DateTimeZone
? $timezone
: new DateTimeZone($timezone);
return $this;
}
/**
* Indicate that the job should run in background
*
* @return $this
*/
public function runInBackground(): static
{
$this->runInBackground = true;
return $this;
}
/**
* Indicate that overlapping should be prevented
*
* @param int $expiresAt
* @return $this
*/
public function withoutOverlapping(int $expiresAt = 1440): static
{
$this->withoutOverlapping = true;
$this->expiresAt = $expiresAt;
return $this;
}
/**
* Register a callback to further filter the schedule
*
* @param callable $callback
* @return $this
*/
public function when(callable $callback): static
{
$this->filters[] = $callback;
return $this;
}
/**
* Register a callback to further filter the schedule
*
* @param callable $callback
* @return $this
*/
public function skip(callable $callback): static
{
$this->rejects[] = $callback;
return $this;
}
/**
* Set the description of the scheduled task
*
* @param string $description
* @return $this
*/
public function description(string $description): static
{
$this->description = $description;
return $this;
}
/**
* Get the cron expression
*
* @return string
*/
public function getExpression(): string
{
return $this->expression;
}
/**
* Get the timezone
*
* @return ?DateTimeZone
*/
public function getTimezone(): ?DateTimeZone
{
return $this->timezone;
}
/**
* Get the description
*
* @return ?string
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* Determine if the task should prevent overlapping
*
* @return bool
*/
public function shouldPreventOverlapping(): bool
{
return $this->withoutOverlapping;
}
/**
* Get the expires at value
*
* @return int
*/
public function getExpiresAt(): int
{
return $this->expiresAt;
}
/**
* Check if the task should run in background
*
* @return bool
*/
public function shouldRunInBackground(): bool
{
return $this->runInBackground;
}
/**
* Determine if the filters pass for the task
*
* @return bool
*/
public function filtersPass(): bool
{
foreach ($this->filters as $callback) {
if (!call_user_func($callback)) {
return false;
}
}
foreach ($this->rejects as $callback) {
if (call_user_func($callback)) {
return false;
}
}
return true;
}
/**
* Determine if the task is due to run
*
* @param DateTimeInterface $currentTime
* @return bool
*/
public function isDue(DateTimeInterface $currentTime): bool
{
$dateParts = $this->getDateParts($currentTime);
$cronParts = explode(' ', $this->expression);
if (count($cronParts) !== 5) {
return false;
}
return $this->matchesCronPart($cronParts[0], $dateParts['minute']) &&
$this->matchesCronPart($cronParts[1], $dateParts['hour']) &&
$this->matchesCronPart($cronParts[2], $dateParts['day']) &&
$this->matchesCronPart($cronParts[3], $dateParts['month']) &&
$this->matchesCronPart($cronParts[4], $dateParts['weekday']);
}
/**
* Get the date parts from a DateTime
*
* @param DateTimeInterface $date
* @return array
*/
protected function getDateParts(DateTimeInterface $date): array
{
$timezone = $this->timezone ?? $date->getTimezone();
$date = \DateTime::createFromInterface($date)->setTimezone($timezone);
return [
'minute' => (int) $date->format('i'),
'hour' => (int) $date->format('G'),
'day' => (int) $date->format('j'),
'month' => (int) $date->format('n'),
'weekday' => (int) $date->format('w'),
];
}
/**
* Check if a cron part matches the given value
*
* @param string $cronPart
* @param int $value
* @return bool
*/
protected function matchesCronPart(string $cronPart, int $value): bool
{
// Match any value
if ($cronPart === '*') {
return true;
}
// Handle step values (e.g., */5)
if (str_starts_with($cronPart, '*/')) {
$step = (int) substr($cronPart, 2);
return $step > 0 && $value % $step === 0;
}
// Handle ranges (e.g., 1-5)
if (str_contains($cronPart, '-')) {
[$start, $end] = explode('-', $cronPart);
return $value >= (int) $start && $value <= (int) $end;
}
// Handle lists (e.g., 1,3,5)
if (str_contains($cronPart, ',')) {
$parts = array_map('intval', explode(',', $cronPart));
return in_array($value, $parts, true);
}
// Direct match
return (int) $cronPart === $value;
}
/**
* Splice a value into the cron expression
*
* @param int $position
* @param int|string $value
* @return $this
*/
protected function spliceIntoPosition(int $position, int|string $value): static
{
$segments = explode(' ', $this->expression);
$segments[$position - 1] = (string) $value;
$this->expression = implode(' ', $segments);
return $this;
}
/**
* Set the owning scheduled event
*
* @param ScheduledEvent $event
* @return $this
*/
public function setEvent(ScheduledEvent $event): static
{
$this->event = $event;
return $this;
}
/**
* Get the owning scheduled event
*
* @return ?ScheduledEvent
*/
public function getEvent(): ?ScheduledEvent
{
return $this->event;
}
/**
* Set the queue connection to use for task execution
*
* @param string $connection
* @return $this
*/
public function onConnection(string $connection): static
{
if ($this->event) {
$this->event->onConnection($connection);
}
return $this;
}
}