INA-5 | New way of setting next scheduled run date

This commit is contained in:
Nikola Cirkovic 2022-05-24 01:42:43 +02:00
parent 99dc9ac65a
commit fbb64effec
2 changed files with 27 additions and 22 deletions

View File

@ -111,31 +111,10 @@ class TaskScheduler implements ShouldQueue
}
$amount_of_days_until_next_run = $this->getAmountOfDays($scheduler->repeat_every);
$scheduler->scheduled_run = Carbon::now()->addDays($amount_of_days_until_next_run);
$scheduler->scheduled_run = $scheduler->nextScheduledDate();
$scheduler->save();
}
private function getAmountOfDays(string $repeat_every): int
{
switch ($repeat_every) {
case Scheduler::DAILY:
return 1;
break;
case Scheduler::MONTHLY:
return 30;
break;
case Scheduler::WEEKLY:
return 7;
break;
case Scheduler::QUARTERLY:
return 90;
break;
case Scheduler::ANNUALLY:
return 365;
break;
}
}
private function fetchJobs()
{

View File

@ -14,6 +14,7 @@ namespace App\Models;
use App\Services\TaskScheduler\TaskSchedulerService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
/**
* @property boolean paused
@ -61,4 +62,29 @@ class Scheduler extends Model
{
return $this->hasOne(ScheduledJob::class, 'scheduler_id', 'id');
}
public function nextScheduledDate() :?Carbon
{
/*
As we are firing at UTC+0 if our offset is negative it is technically firing the day before so we always need
to add ON a day - a day = 86400 seconds
*/
$offset = 86400;
switch ($this->repeat_every) {
case self::DAILY:
return Carbon::parse($this->scheduled_run)->startOfDay()->addDay()->addSeconds($offset);
case self::WEEKLY:
return Carbon::parse($this->scheduled_run)->startOfDay()->addWeek()->addSeconds($offset);
case self::MONTHLY:
return Carbon::parse($this->scheduled_run)->startOfDay()->addMonth()->addSeconds($offset);
case self::QUARTERLY:
return Carbon::parse($this->scheduled_run)->startOfDay()->addMonths(3)->addSeconds($offset);
case self::ANNUALLY:
return Carbon::parse($this->scheduled_run)->startOfDay()->addYear()->addSeconds($offset);
default:
return null;
}
}
}