Skip to content

Commit 672635c

Browse files
committed
Option "Restart immediately" to restart the task after a failure without delay.
1 parent 1c4a583 commit 672635c

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ Some options for setting default task options. The parameters specified in the t
316316

317317
#### Others
318318

319+
* `$restartImmediately` (default `false`) - If true, the failed task will be restarted without delay for Every/Delay
320+
modes, and at the same interval for Single mode (but only at the correct interval).
319321
* ```$minimumIntervalLength``` (default ```30```) - Minimum interval size in minutes (for task
320322
method ```addInterval()```). Currently, tasks are started sequentially and synchronously, so the scheduler cannot
321323
guarantee the exact time when the task will start. Because of this, I had to limit the minimum size of the interval to

src/Config.php

+21
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ class Config
9090
* @var int
9191
*/
9292
private $defaultTryDelay;
93+
/**
94+
* If true, the failed task will be restarted without delay for Every/Delay modes,
95+
* and at the same interval for Single mode (but only at the correct interval)
96+
* @var bool
97+
*/
98+
private $restartImmediately;
9399
/**
94100
* Minimum interval in minutes for task's addInterval method. It is made due to the fact that the scheduler
95101
* does not guarantee the start of the task at the exact time, and too small interval can lead to a missed task launch.
@@ -120,10 +126,12 @@ class Config
120126
* {{message}} and {{stacktrace}} variables. Pass null for default formatting:
121127
* "{{header}}\n[code]: {{code}}\n[exception]: {{class}}\n[message]: {{message}}\n[stacktrace]:\n{{stacktrace}}".
122128
* @param ?int $maxExceptionMsgLength The maximum length of the exception message (the longer one will be truncated).
129+
* @param bool $commandOutput If true, output from bash command tasks will be sent to stdout. Otherwise, it will be suppressed.
123130
* @param bool $defaultPreventOverlapping Determines if an overlapping task can be run launched.
124131
* @param int $defaultLockResetTimeout Locking reset timeout in minutes (to prevent freezing tasks).
125132
* @param int $defaultTries The number of attempts to execute the task in case of an error.
126133
* @param int $defaultTryDelay Delay before new try.
134+
* @param bool $restartImmediately If true, failed task will be restarted without delay
127135
* @param int $minimumIntervalLength Minimum interval in minutes for task's addInterval method.
128136
* ATTENTION: a low value can cause to skipped tasks, change at your own risk.
129137
* @throws Exception
@@ -144,6 +152,7 @@ public function __construct(
144152
int $defaultLockResetTimeout = 360,
145153
int $defaultTries = 1,
146154
int $defaultTryDelay = 0,
155+
bool $restartImmediately = false,
147156
int $minimumIntervalLength = 30
148157
)
149158
{
@@ -186,6 +195,8 @@ public function __construct(
186195
}
187196
$this->defaultTryDelay = $defaultTryDelay;
188197

198+
$this->restartImmediately = $restartImmediately;
199+
189200
if ($minimumIntervalLength <= 0) {
190201
throw new Exception('The minimum interval must be greater than zero.');
191202
}
@@ -338,6 +349,16 @@ public function getDefaultTryDelay(): int
338349
return $this->defaultTryDelay;
339350
}
340351

352+
/**
353+
* If true, the failed task will be restarted without delay for Every/Delay modes,
354+
* and at the same interval for Single mode (but only at the correct interval)
355+
* @return bool
356+
*/
357+
public function getRestartImmediately(): bool
358+
{
359+
return $this->restartImmediately;
360+
}
361+
341362
/**
342363
* Minimum interval in minutes for task's addInterval method. It is made due to the fact that the scheduler
343364
* does not guarantee the start of the task at the exact time, and too small interval can lead to a missed task launch.

src/Wrapper/TaskWrapper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function dispatch(): void
109109
return;
110110
}
111111

112-
if (!$lastLaunch->isWorking && $lastLaunch->errorCount !== 0) {
112+
if (!$lastLaunch->isWorking && $lastLaunch->errorCount !== 0 && $this->config->getRestartImmediately()) {
113113
$this->logDebug("The previous launch failed after $lastLaunch->errorCount tries. Restart.");
114114
$this->launchTask($startTime);
115115
return;

0 commit comments

Comments
 (0)