Fix schedule loop triggering
This commit is contained in:
parent
5bde5bee16
commit
0159079608
2 changed files with 30 additions and 23 deletions
|
@ -71,8 +71,13 @@ App::post('/v1/functions')
|
||||||
->param('enabled', true, new Boolean(), 'Is function enabled?', true)
|
->param('enabled', true, new Boolean(), 'Is function enabled?', true)
|
||||||
->inject('response')
|
->inject('response')
|
||||||
->inject('dbForProject')
|
->inject('dbForProject')
|
||||||
|
->inject('project')
|
||||||
|
->inject('user')
|
||||||
->inject('events')
|
->inject('events')
|
||||||
->action(function (string $functionId, string $name, array $execute, string $runtime, array $events, string $schedule, int $timeout, bool $enabled, Response $response, Database $dbForProject, Event $eventsInstance) {
|
->action(function (string $functionId, string $name, array $execute, string $runtime, array $events, string $schedule, int $timeout, bool $enabled, Response $response, Database $dbForProject, Document $project, Document $user, Event $eventsInstance) {
|
||||||
|
|
||||||
|
$cron = !empty($schedule) ? new CronExpression($schedule) : null;
|
||||||
|
$next = !empty($schedule) ? DateTime::format($cron->getNextRunDate()) : null;
|
||||||
|
|
||||||
$functionId = ($functionId == 'unique()') ? ID::unique() : $functionId;
|
$functionId = ($functionId == 'unique()') ? ID::unique() : $functionId;
|
||||||
$function = $dbForProject->createDocument('functions', new Document([
|
$function = $dbForProject->createDocument('functions', new Document([
|
||||||
|
@ -86,11 +91,22 @@ App::post('/v1/functions')
|
||||||
'schedule' => $schedule,
|
'schedule' => $schedule,
|
||||||
'scheduleUpdatedAt' => DateTime::now(),
|
'scheduleUpdatedAt' => DateTime::now(),
|
||||||
'schedulePrevious' => null,
|
'schedulePrevious' => null,
|
||||||
'scheduleNext' => null,
|
'scheduleNext' => $next,
|
||||||
'timeout' => $timeout,
|
'timeout' => $timeout,
|
||||||
'search' => implode(' ', [$functionId, $name, $runtime])
|
'search' => implode(' ', [$functionId, $name, $runtime])
|
||||||
]));
|
]));
|
||||||
|
|
||||||
|
if ($next) {
|
||||||
|
// Async task reschedule
|
||||||
|
$functionEvent = new Func();
|
||||||
|
$functionEvent
|
||||||
|
->setFunction($function)
|
||||||
|
->setType('schedule')
|
||||||
|
->setUser($user)
|
||||||
|
->setProject($project)
|
||||||
|
->schedule(new \DateTime($next));
|
||||||
|
}
|
||||||
|
|
||||||
$eventsInstance->setParam('functionId', $function->getId());
|
$eventsInstance->setParam('functionId', $function->getId());
|
||||||
|
|
||||||
$response
|
$response
|
||||||
|
@ -442,11 +458,9 @@ App::put('/v1/functions/:functionId')
|
||||||
throw new Exception(Exception::FUNCTION_NOT_FOUND);
|
throw new Exception(Exception::FUNCTION_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
$original = $function->getAttribute('schedule', '');
|
$cron = !empty($schedule) ? new CronExpression($schedule) : null;
|
||||||
$cron = (!empty($function->getAttribute('deployment')) && !empty($schedule)) ? new CronExpression($schedule) : null;
|
$next = !empty($schedule) ? DateTime::format($cron->getNextRunDate()) : null;
|
||||||
$next = (!empty($function->getAttribute('deployment')) && !empty($schedule)) ? DateTime::format($cron->getNextRunDate()) : null;
|
|
||||||
|
|
||||||
$scheduleUpdatedAt = $schedule !== $original ? DateTime::now() : $function->getAttribute('scheduleUpdatedAt');
|
|
||||||
$enabled ??= $function->getAttribute('enabled', true);
|
$enabled ??= $function->getAttribute('enabled', true);
|
||||||
|
|
||||||
$function = $dbForProject->updateDocument('functions', $function->getId(), new Document(array_merge($function->getArrayCopy(), [
|
$function = $dbForProject->updateDocument('functions', $function->getId(), new Document(array_merge($function->getArrayCopy(), [
|
||||||
|
@ -454,14 +468,14 @@ App::put('/v1/functions/:functionId')
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
'events' => $events,
|
'events' => $events,
|
||||||
'schedule' => $schedule,
|
'schedule' => $schedule,
|
||||||
'scheduleUpdatedAt' => $scheduleUpdatedAt,
|
'scheduleUpdatedAt' => DateTime::now(),
|
||||||
'scheduleNext' => $next,
|
'scheduleNext' => $next,
|
||||||
'timeout' => $timeout,
|
'timeout' => $timeout,
|
||||||
'enabled' => $enabled,
|
'enabled' => $enabled,
|
||||||
'search' => implode(' ', [$functionId, $name, $function->getAttribute('runtime')]),
|
'search' => implode(' ', [$functionId, $name, $function->getAttribute('runtime')]),
|
||||||
])));
|
])));
|
||||||
|
|
||||||
if ($next && $schedule !== $original) {
|
if ($next) {
|
||||||
// Async task reschedule
|
// Async task reschedule
|
||||||
$functionEvent = new Func();
|
$functionEvent = new Func();
|
||||||
$functionEvent
|
$functionEvent
|
||||||
|
@ -519,24 +533,10 @@ App::patch('/v1/functions/:functionId/deployments/:deploymentId')
|
||||||
throw new Exception(Exception::BUILD_NOT_READY);
|
throw new Exception(Exception::BUILD_NOT_READY);
|
||||||
}
|
}
|
||||||
|
|
||||||
$schedule = $function->getAttribute('schedule', '');
|
|
||||||
$cron = (empty($function->getAttribute('deployment')) && !empty($schedule)) ? new CronExpression($schedule) : null;
|
|
||||||
$next = (empty($function->getAttribute('deployment')) && !empty($schedule)) ? DateTime::format($cron->getNextRunDate()) : null;
|
|
||||||
|
|
||||||
$function = $dbForProject->updateDocument('functions', $function->getId(), new Document(array_merge($function->getArrayCopy(), [
|
$function = $dbForProject->updateDocument('functions', $function->getId(), new Document(array_merge($function->getArrayCopy(), [
|
||||||
'deployment' => $deployment->getId(),
|
'deployment' => $deployment->getId()
|
||||||
'scheduleNext' => $next,
|
|
||||||
])));
|
])));
|
||||||
|
|
||||||
if ($next) { // Init first schedule
|
|
||||||
$functionEvent = new Func();
|
|
||||||
$functionEvent
|
|
||||||
->setType('schedule')
|
|
||||||
->setFunction($function)
|
|
||||||
->setProject($project)
|
|
||||||
->schedule(new \DateTime($next));
|
|
||||||
}
|
|
||||||
|
|
||||||
$events
|
$events
|
||||||
->setParam('functionId', $function->getId())
|
->setParam('functionId', $function->getId())
|
||||||
->setParam('deploymentId', $deployment->getId());
|
->setParam('deploymentId', $deployment->getId());
|
||||||
|
|
|
@ -150,14 +150,21 @@ class FunctionsV1 extends Worker
|
||||||
throw new Exception('Function not found (' . $function->getId() . ')');
|
throw new Exception('Function not found (' . $function->getId() . ')');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
\var_dump("Got here");
|
||||||
|
|
||||||
if ($functionOriginal->getAttribute('schedule') !== $function->getAttribute('schedule')) { // Schedule has changed from previous run, ignore this run.
|
if ($functionOriginal->getAttribute('schedule') !== $function->getAttribute('schedule')) { // Schedule has changed from previous run, ignore this run.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
\var_dump("Got here 2");
|
||||||
|
|
||||||
if ($functionOriginal->getAttribute('scheduleUpdatedAt') !== $function->getAttribute('scheduleUpdatedAt')) { // Double execution due to rapid cron changes, ignore this run.
|
if ($functionOriginal->getAttribute('scheduleUpdatedAt') !== $function->getAttribute('scheduleUpdatedAt')) { // Double execution due to rapid cron changes, ignore this run.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
\var_dump("Got here 3");
|
||||||
|
|
||||||
$cron = new CronExpression($function->getAttribute('schedule'));
|
$cron = new CronExpression($function->getAttribute('schedule'));
|
||||||
$next = DateTime::format($cron->getNextRunDate());
|
$next = DateTime::format($cron->getNextRunDate());
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue