Skip to content

Commit

Permalink
[FEATURE] Determine the current time before the first task is run, to…
Browse files Browse the repository at this point in the history
… support long-running tasks (#51)

Closes #49
  • Loading branch information
NicoHaase authored Jan 31, 2024
1 parent 9c643da commit d899417
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Task/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ public function setPart(int $position, string $value): self
*/
public function isDue($currentTime = 'now'): bool
{
if (is_string($currentTime)) {
trigger_deprecation(
'rewieer/taskschedulerbundle',
'0.12',
'Calling isDue() with a string is deprecated, use a DateTimeInteface instance instead'
);
}

return $this->cron->isDue($currentTime);
}
}
5 changes: 5 additions & 0 deletions Task/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Rewieer\TaskSchedulerBundle\Task;

use DateTime;
use Rewieer\TaskSchedulerBundle\Event\EventDispatcher;

class Scheduler
Expand Down Expand Up @@ -39,6 +40,10 @@ public function run($currentTime = 'now'): void
{
$this->dispatcher->dispatch(SchedulerEvents::ON_START);

if (is_string($currentTime)) {
$currentTime = new DateTime($currentTime);
}

foreach ($this->tasks as $task) {
if ($task->isDue($currentTime)) {
$this->runTask($task);
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"keywords": ["cron", "task", "scheduler", "symfony", "bundle"],
"require": {
"php": "^7.4|^8.1",
"symfony/deprecation-contracts": "^2.5|^3.4",
"symfony/framework-bundle": "^5.4|^6.4|^7.0",
"symfony/console": "^5.3|^6.4|^7.0",
"dragonmantank/cron-expression": "^3.3"
Expand Down

0 comments on commit d899417

Please sign in to comment.