Skip to content

Commit

Permalink
Merge branch 'release/v1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rumur committed Jul 10, 2020
2 parents 9f871e3 + 001144a commit 5090386
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Release Notes

### v1.1.0 (2020-07-11)
### v1.2.0 (2020-07-11)

### Feature
- Added `force` argument to `PendingTask` in order to force re-register existed tasks

### v1.1.0 (2020-07-10)

### Feature
- Added an ability to add custom single tasks at specific timestamp.
Expand Down
46 changes: 34 additions & 12 deletions src/PendingTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,16 @@ public function onSuccess(callable $listener)
* @param string $interval Available type of scheduling
* @param int $extraTime The extra time that need to be adjusted.
* @param int|null $timestamp The calculated $timestamp
* @param bool $force Force to re-register task even if it exists.
*
* @return CronTask
*/
public function registerSingular(string $interval, int $extraTime = 1, ?int $timestamp = null): CronTask
{
public function registerSingular(
string $interval,
int $extraTime = 1,
?int $timestamp = null,
bool $force = false
): CronTask {
$task = $this->resolveTask()->useInterval($interval);

// In order to have one task, but several events
Expand All @@ -82,14 +87,19 @@ public function registerSingular(string $interval, int $extraTime = 1, ?int $tim
$task->useName($this->registry->singularActionName());
}

if (! $timestamp && $this->interval->has($interval) && $this->registry->isNotRegistered($task)) {
$this->registry->scheduleSingular(
$task,
$this->interval->calculateFromNow($interval, $extraTime)
);
if (! $timestamp && $this->interval->has($interval)) {
$timestamp = $this->interval->calculateFromNow($interval, $extraTime);
}

if (! $timestamp) {
return $task;
}

if ($timestamp && $this->registry->isNotRegistered($task)) {
if ($force && $this->registry->isRegistered($task)) {
$this->registry->resign($task);
}

if ($this->registry->isNotRegistered($task)) {
$this->registry->scheduleSingular($task, $timestamp);
}

Expand All @@ -101,19 +111,31 @@ public function registerSingular(string $interval, int $extraTime = 1, ?int $tim
*
* @param string $interval Available type of scheduling
* @param int|null $timestamp The calculated $timestamp or by default it is `now`
*
* @param bool $force Force to re-register task even if it exists.
* @return CronTask
*/
public function registerRecurrence(string $interval, ?int $timestamp = null): CronTask
public function registerRecurrence(string $interval, ?int $timestamp = null, bool $force = false): CronTask
{
$task = $this->resolveTask()->useInterval($interval);

if (! $task->name()) {
$task->useName($this->registry->recurrentActionName());
}

if ($this->interval->has($interval) && $this->registry->isNotRegistered($task)) {
$this->registry->scheduleRecurrence($task, $timestamp ?: $this->interval->now(), $interval);
if (! $this->interval->has($interval)) {
throw new \InvalidArgumentException("Wrong interval {$interval}");
}

if (! $timestamp) {
$timestamp = $this->interval->now();
}

if ($force && $this->registry->isRegistered($task)) {
$this->registry->resign($task);
}

if ($this->registry->isNotRegistered($task)) {
$this->registry->scheduleRecurrence($task, $timestamp, $interval);
}

return $task;
Expand Down

0 comments on commit 5090386

Please sign in to comment.