Fix schedules running twice, closes #1288

This commit is contained in:
Dane Everitt 2018-09-03 14:32:33 -07:00
parent 413a22a3d5
commit 5bd3f59455
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
6 changed files with 18 additions and 72 deletions

View file

@ -57,6 +57,11 @@ class ProcessRunnableCommand extends Command
public function handle()
{
$schedules = $this->repository->getSchedulesToProcess(Chronos::now()->toAtomString());
if ($schedules->count() < 1) {
$this->line('There are no scheduled tasks for servers that need to be run.');
return;
}
$bar = $this->output->createProgressBar(count($schedules));
$schedules->each(function ($schedule) use ($bar) {

View file

@ -2,7 +2,6 @@
namespace Pterodactyl\Http\Controllers\Server\Tasks;
use Cake\Chronos\Chronos;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Pterodactyl\Http\Controllers\Controller;
@ -71,7 +70,7 @@ class ActionController extends Controller
$server = $request->attributes->get('server');
$this->authorize('toggle-schedule', $server);
$this->processScheduleService->setRunTimeOverride(Chronos::now())->handle(
$this->processScheduleService->handle(
$request->attributes->get('schedule')
);

View file

@ -75,6 +75,7 @@ class ScheduleRepository extends EloquentRepository implements ScheduleRepositor
{
return $this->getBuilder()->with('tasks')
->where('is_active', true)
->where('is_processing', false)
->where('next_run_at', '<=', $timestamp)
->get($this->getColumns());
}

View file

@ -2,11 +2,8 @@
namespace Pterodactyl\Services\Schedules;
use DateTimeInterface;
use Cron\CronExpression;
use Cake\Chronos\Chronos;
use Pterodactyl\Models\Schedule;
use Cake\Chronos\ChronosInterface;
use Illuminate\Contracts\Bus\Dispatcher;
use Pterodactyl\Jobs\Schedule\RunTaskJob;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
@ -19,11 +16,6 @@ class ProcessScheduleService
*/
private $dispatcher;
/**
* @var \DateTimeInterface|null
*/
private $runTimeOverride;
/**
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
*/
@ -51,20 +43,6 @@ class ProcessScheduleService
$this->taskRepository = $taskRepository;
}
/**
* Set the time that this schedule should be run at. This will override the time
* defined on the schedule itself. Useful for triggering one-off task runs.
*
* @param \DateTimeInterface $time
* @return $this
*/
public function setRunTimeOverride(DateTimeInterface $time)
{
$this->runTimeOverride = $time;
return $this;
}
/**
* Process a schedule and push the first task onto the queue worker.
*
@ -89,7 +67,7 @@ class ProcessScheduleService
$this->scheduleRepository->update($schedule->id, [
'is_processing' => true,
'next_run_at' => $this->getRunAtTime($formattedCron),
'next_run_at' => CronExpression::factory($formattedCron)->getNextRunDate(),
]);
$this->taskRepository->update($task->id, ['is_queued' => true]);
@ -98,19 +76,4 @@ class ProcessScheduleService
(new RunTaskJob($task->id, $schedule->id))->delay($task->time_offset)
);
}
/**
* Get the timestamp to store in the database as the next_run time for a schedule.
*
* @param string $formatted
* @return \Cake\Chronos\ChronosInterface
*/
private function getRunAtTime(string $formatted): ChronosInterface
{
if (! is_null($this->runTimeOverride)) {
return $this->runTimeOverride instanceof ChronosInterface ? $this->runTimeOverride : Chronos::instance($this->runTimeOverride);
}
return Chronos::instance(CronExpression::factory($formatted)->getNextRunDate());
}
}