- WIP on Middleware to allows only one launch from a task which could be called at least twice due to having multiple cron attached to

This commit is contained in:
Dave M. 2025-07-15 12:49:56 +00:00
parent c309facbd3
commit ea92e0de8d
2 changed files with 13 additions and 4 deletions

View File

@ -41,17 +41,26 @@ trait CronardTrait {
public function fromAnnotations(TaskFetcher $fetcher) : self
{
$tasks = [];
$launched = [];
foreach($fetcher->compile() as $task) {
$this->crontabs[] = [
'cron' => $task['annotation']->cron,
'callback' => function(ServerRequestInterface $request, ResponseInterface $response, ContainerInterface $container) use ($task) {
'callback' => function(ServerRequestInterface $request, ResponseInterface $response, ContainerInterface $container) use ($task, & $launched) {
if ( $task['annotation']->method ?? false ) {
$request = $request->withMethod($task['annotation']->method);
}
return $container->make($task['class'])->{$task['method']}($request->withAttribute('lean.cronard', $task['annotation']), []);
$uid = sprintf("%s::%s", $task['class'], $task['method']);
if ( empty($launched[$uid]) ) {
$launched[$uid] = true;
return $container->make($task['class'])->{$task['method']}($request->withAttribute('lean.cronard', $task['annotation']), []);
}
else {
return null;
}
}
];
}