5 Commits
3 changed files with 23 additions and 7 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) {
+1 -1
View File
@@ -32,7 +32,7 @@ class CronardMiddleware implements MiddlewareInterface
{ {
if ( $this->cronKey === ( $request->getQueryParams()['cronkey'] ?? false ) ) { if ( $this->cronKey === ( $request->getQueryParams()['cronkey'] ?? false ) ) {
$response = $this->response->call($this); $response = $this->response->call($this);
$this->launch([ $this->launch([
$request, $request,
$response, $response,
+21 -5
View File
@@ -18,8 +18,13 @@ trait CronardTrait {
$cronard = new Cronard(); $cronard = new Cronard();
foreach($this->crontabs as $tab) { foreach($this->crontabs as $tab) {
$cronard->setTab(stripslashes($tab['cron'])); try {
$cronard->run(array_merge($this->variables, $variables), $tab['callback']); $cronard->setTab(stripslashes($tab['cron']));
$cronard->run(array_merge($this->variables, $variables), $tab['callback']);
}
catch(\Throwable $ex) {
error_log($ex->getMessage());
}
} }
} }
@@ -41,17 +46,28 @@ 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) {
set_time_limit(0);
if ( $task['annotation']->method ?? false ) { if ( $task['annotation']->method ?? false ) {
$request = $request->withMethod($task['annotation']->method); $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;
}
} }
]; ];
} }