Skip to content

Commit

Permalink
ManagedScheduler: acquired job locks are scoped just to their id
Browse files Browse the repository at this point in the history
Changing run frequency or job name will not make process loose the lock.
  • Loading branch information
mabar committed Dec 11, 2023
1 parent b3cd4da commit 21352e1
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `runJobs()` returns `Generator<int, JobSummary, void, RunSummary>` instead of `RunSummary`
- `ProcessJobExecutor`
- constructor requires `JobManager` as first parameter
- `ManagedScheduler`
- acquired job locks are scoped just to their id - changing run frequency or job name will not make process loose
the lock
13 changes: 12 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ $scheduler = new SimpleScheduler($errorHandler);

## Locks and job overlapping

Crontab jobs are time-based and simply run at specified intervals. If they take too long, they may overlap and run
Jobs are time-based and simply run at specified intervals. If they take too long, they may overlap and run
simultaneously. This may cause issues if the jobs access the same resources, such as files or databases, leading to
conflicts or data corruption.

Expand Down Expand Up @@ -274,6 +274,17 @@ new CallbackJob(function (JobLock $lock): void {
});
```

To make sure locks are correctly used during deployments, specify constant id for every added job, lock identifiers rely
on that fact. Otherwise, your job id will change when new jobs are added before it and acquired lock will be ignored.

```php
$scheduler->addJob(
/* ... */,
/* ... */,
'job-id',
);
```

## Parallelization and process isolation

It is important for crontab scheduler tasks to be executed asynchronously and in separate processes because this
Expand Down
4 changes: 1 addition & 3 deletions src/ManagedScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ private function runInternal($id, Job $job, CronExpression $expression, int $sec
$this->clock->now(),
);

$lock = $this->lockFactory->createLock(
"Orisai.Scheduler.Job/{$info->getExpression()}-{$info->getName()}-$id",
);
$lock = $this->lockFactory->createLock("Orisai.Scheduler.Job/$id");

if (!$lock->acquire()) {
return [
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public function testSkip(): void
new CronExpression('* * * * *'),
);

$lock = $lockFactory->createLock('Orisai.Scheduler.Job/* * * * *-job1-0');
$lock = $lockFactory->createLock('Orisai.Scheduler.Job/0');
$lock->acquire();

$command = new RunCommand($scheduler);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Command/RunJobCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function testSkip(): void
new CronExpression('* * * * *'),
);

$lock = $lockFactory->createLock('Orisai.Scheduler.Job/* * * * *-job1-0');
$lock = $lockFactory->createLock('Orisai.Scheduler.Job/0');
$lock->acquire();

$command = new RunJobCommand($scheduler);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/SimpleSchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ static function () use (&$i2): void {
new CronExpression('* * * * *'),
);

$lock = $lockFactory->createLock('Orisai.Scheduler.Job/* * * * *-job1-0');
$lock = $lockFactory->createLock('Orisai.Scheduler.Job/0');
$lock->acquire();

// Lock is active, job is not executed (but the other one is)
Expand Down

0 comments on commit 21352e1

Please sign in to comment.