@@ -81,43 +81,69 @@ a simple URL string, you can use a closure or command instead.
81
81
$schedule->url('https://my-status-cloud.com?site=foo.com')->everyFiveMinutes();
82
82
```
83
83
84
+ ## Single Instance Tasks
85
+
86
+ Some tasks can run longer than their scheduled interval. To prevent multiple instances of the same task running simultaneously, you can use the ` singleInstance() ` method:
87
+
88
+ ``` php
89
+ $schedule->command('demo:heavy-task')->everyMinute()->singleInstance();
90
+ ```
91
+
92
+ With this setup, even if the task takes more than one minute to complete, a new instance won't start until the running one finishes.
93
+
94
+ ### Setting Lock Duration
95
+
96
+ By default, the lock will remain active until the task completes execution. However, you can specify a maximum lock duration by passing a TTL (time-to-live) value in seconds to the ` singleInstance() ` method:
97
+
98
+ ``` php
99
+ // Lock for a maximum of 30 minutes (1800 seconds)
100
+ $schedule->command('demo:heavy-task')
101
+ ->everyFifteenMinutes()
102
+ ->singleInstance(30 * MINUTE);
103
+ ```
104
+
105
+ This is useful in preventing "stuck" locks. If a task crashes unexpectedly, the lock might remain indefinitely. Setting a TTL ensures the lock eventually expires.
106
+
107
+ If a task completes before the TTL expires, the lock is released immediately. The TTL only represents the maximum duration the lock can exist.
108
+
84
109
## Frequency Options
85
110
86
111
There are a number of ways available to specify how often the task is called.
87
112
88
113
89
- | Method | Description |
90
- | :----------------------------------| :----------------------------------------------------------------------|
91
- | ` ->cron('* * * * *') ` | Run on a custom cron schedule. |
92
- | ` ->daily('4:00 am') ` | Runs daily at 12:00am, unless a time string is passed in. |
93
- | ` ->hourly() / ->hourly(15) ` | Runs at the top of every hour or at specified minute. |
94
- | ` ->everyHour(3, 15) ` | Runs every 3 hours at XX:15. |
95
- | ` ->betweenHours(6,12) ` | Runs between hours 6 and 12. |
96
- | ` ->hours([0,10,16]) ` | Runs at hours 0, 10 and 16. |
97
- | ` ->everyMinute(20) ` | Runs every 20 minutes. |
98
- | ` ->betweenMinutes(0,30) ` | Runs between minutes 0 and 30. |
99
- | ` ->minutes([0,20,40]) ` | Runs at specific minutes 0,20 and 40. |
100
- | ` ->everyFiveMinutes() ` | Runs every 5 minutes (12:00, 12:05, 12:10, etc) |
101
- | ` ->everyFifteenMinutes() ` | Runs every 15 minutes (12:00, 12:15, etc) |
102
- | ` ->everyThirtyMinutes() ` | Runs every 30 minutes (12:00, 12:30, etc) |
103
- | ` ->days([0,3]) ` | Runs only on Sunday and Wednesday ( 0 is Sunday , 6 is Saturday ) |
104
- | ` ->sundays('3:15am') ` | Runs every Sunday at midnight, unless time passed in. |
105
- | ` ->mondays('3:15am') ` | Runs every Monday at midnight, unless time passed in. |
106
- | ` ->tuesdays('3:15am') ` | Runs every Tuesday at midnight, unless time passed in. |
107
- | ` ->wednesdays('3:15am') ` | Runs every Wednesday at midnight, unless time passed in. |
108
- | ` ->thursdays('3:15am') ` | Runs every Thursday at midnight, unless time passed in. |
109
- | ` ->fridays('3:15am') ` | Runs every Friday at midnight, unless time passed in. |
110
- | ` ->saturdays('3:15am') ` | Runs every Saturday at midnight, unless time passed in. |
111
- | ` ->monthly('12:21pm') ` | Runs the first day of every month at 12:00am unless time passed in. |
112
- | ` ->daysOfMonth([1,15]) ` | Runs only on days 1 and 15. |
113
- | ` ->everyMonth(4) ` | Runs every 4 months. |
114
- | ` ->betweenMonths(4,7) ` | Runs between months 4 and 7. |
115
- | ` ->months([1,7]) ` | Runs only on January and July. |
116
- | ` ->quarterly('5:00am') ` | Runs the first day of each quarter (Jan 1, Apr 1, July 1, Oct 1) |
117
- | ` ->yearly('12:34am') ` | Runs the first day of the year. |
118
- | ` ->weekdays('1:23pm') ` | Runs M-F at 12:00 am unless time passed in. |
119
- | ` ->weekends('2:34am') ` | Runs Saturday and Sunday at 12:00 am unless time passed in. |
120
- | ` ->environments('local', 'prod') ` | Restricts the task to run only in the specified environments |
114
+ | Method | Description |
115
+ | :----------------------------------------------| :--------------------------------------------------------------------|
116
+ | ` ->cron('* * * * *') ` | Run on a custom cron schedule. |
117
+ | ` ->daily('4:00 am') ` | Runs daily at 12:00am, unless a time string is passed in. |
118
+ | ` ->hourly() / ->hourly(15) ` | Runs at the top of every hour or at specified minute. |
119
+ | ` ->everyHour(3, 15) ` | Runs every 3 hours at XX:15. |
120
+ | ` ->betweenHours(6,12) ` | Runs between hours 6 and 12. |
121
+ | ` ->hours([0,10,16]) ` | Runs at hours 0, 10 and 16. |
122
+ | ` ->everyMinute(20) ` | Runs every 20 minutes. |
123
+ | ` ->betweenMinutes(0,30) ` | Runs between minutes 0 and 30. |
124
+ | ` ->minutes([0,20,40]) ` | Runs at specific minutes 0,20 and 40. |
125
+ | ` ->everyFiveMinutes() ` | Runs every 5 minutes (12:00, 12:05, 12:10, etc) |
126
+ | ` ->everyFifteenMinutes() ` | Runs every 15 minutes (12:00, 12:15, etc) |
127
+ | ` ->everyThirtyMinutes() ` | Runs every 30 minutes (12:00, 12:30, etc) |
128
+ | ` ->days([0,3]) ` | Runs only on Sunday and Wednesday ( 0 is Sunday , 6 is Saturday ) |
129
+ | ` ->sundays('3:15am') ` | Runs every Sunday at midnight, unless time passed in. |
130
+ | ` ->mondays('3:15am') ` | Runs every Monday at midnight, unless time passed in. |
131
+ | ` ->tuesdays('3:15am') ` | Runs every Tuesday at midnight, unless time passed in. |
132
+ | ` ->wednesdays('3:15am') ` | Runs every Wednesday at midnight, unless time passed in. |
133
+ | ` ->thursdays('3:15am') ` | Runs every Thursday at midnight, unless time passed in. |
134
+ | ` ->fridays('3:15am') ` | Runs every Friday at midnight, unless time passed in. |
135
+ | ` ->saturdays('3:15am') ` | Runs every Saturday at midnight, unless time passed in. |
136
+ | ` ->monthly('12:21pm') ` | Runs the first day of every month at 12:00am unless time passed in. |
137
+ | ` ->daysOfMonth([1,15]) ` | Runs only on days 1 and 15. |
138
+ | ` ->everyMonth(4) ` | Runs every 4 months. |
139
+ | ` ->betweenMonths(4,7) ` | Runs between months 4 and 7. |
140
+ | ` ->months([1,7]) ` | Runs only on January and July. |
141
+ | ` ->quarterly('5:00am') ` | Runs the first day of each quarter (Jan 1, Apr 1, July 1, Oct 1) |
142
+ | ` ->yearly('12:34am') ` | Runs the first day of the year. |
143
+ | ` ->weekdays('1:23pm') ` | Runs M-F at 12:00 am unless time passed in. |
144
+ | ` ->weekends('2:34am') ` | Runs Saturday and Sunday at 12:00 am unless time passed in. |
145
+ | ` ->environments('local', 'prod') ` | Restricts the task to run only in the specified environments. |
146
+ | ` ->singleInstance() / ->singleInstance(HOUR) ` | Prevents concurrent executions of the same task. |
121
147
122
148
123
149
These methods can be combined to create even more nuanced timings:
0 commit comments