- 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:
parent
c309facbd3
commit
ea92e0de8d
@ -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,
|
||||||
|
@ -41,17 +41,26 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user