4 Commits
2 changed files with 17 additions and 3 deletions

No files matched your search

+1 -1
View File
@@ -17,7 +17,7 @@ return [
return new HtmlResponse(sprintf("%s - cron task begin...", date('Y-m-d H:i:s'))); return new HtmlResponse(sprintf("%s - cron task begin...", date('Y-m-d H:i:s')));
}, []); }, []);
return $cronardMiddleware->fromFile(getenv("META_PATH")."/crontab.php")->fromAnnotations($c->get(TaskFetcher::class)); return $cronardMiddleware->fromAnnotations($c->get(TaskFetcher::class));
}, },
TaskFetcher::class => function($c) { TaskFetcher::class => function($c) {
+16 -2
View File
@@ -18,9 +18,14 @@ trait CronardTrait {
$cronard = new Cronard(); $cronard = new Cronard();
foreach($this->crontabs as $tab) { foreach($this->crontabs as $tab) {
try {
$cronard->setTab(stripslashes($tab['cron'])); $cronard->setTab(stripslashes($tab['cron']));
$cronard->run(array_merge($this->variables, $variables), $tab['callback']); $cronard->run(array_merge($this->variables, $variables), $tab['callback']);
} }
catch(\Throwable $ex) {
error_log($ex->getMessage());
}
}
} }
public function fromFile(string $filepath) : self public function fromFile(string $filepath) : self
@@ -41,18 +46,27 @@ trait CronardTrait {
public function fromAnnotations(TaskFetcher $fetcher) : self public function fromAnnotations(TaskFetcher $fetcher) : self
{ {
$tasks = []; $launched = [];
foreach($fetcher->compile() as $task) { foreach($fetcher->compile() as $task) {
$this->crontabs[] = [ $this->crontabs[] = [
'cron' => $task['annotation']->cron, '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 ) { if ( $task['annotation']->method ?? false ) {
$request = $request->withMethod($task['annotation']->method); $request = $request->withMethod($task['annotation']->method);
} }
$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']), []); return $container->make($task['class'])->{$task['method']}($request->withAttribute('lean.cronard', $task['annotation']), []);
} }
else {
return null;
}
}
]; ];
} }