Compare commits
6
Commits
a364a51f85
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78daf88d67 | ||
|
|
2f78a1b97d | ||
|
|
ea92e0de8d | ||
|
|
c309facbd3 | ||
|
|
ea2e52728b | ||
|
|
ad8cb45cdc |
No files matched your search
+11
-1
@@ -1,12 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "mcnd/cronard",
|
"name": "mcnd/cronard",
|
||||||
"description": "A cron library which allows task to be run easily into an application.",
|
"description": "A cron library which allows task to be run easily into an application.",
|
||||||
|
"keywords": ["cronard","cron","task","psr15","middleware"],
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
"name": "Dave Mc Nicoll",
|
"name": "Dave Mc Nicoll",
|
||||||
"email": "mcndave@gmail.com"
|
"email": "info@mcnd.ca"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {},
|
"require": {},
|
||||||
@@ -14,5 +15,14 @@
|
|||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Cronard\\": "src/"
|
"Cronard\\": "src/"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"extra" : {
|
||||||
|
"lean" : {
|
||||||
|
"autoload": {
|
||||||
|
"definitions" : [
|
||||||
|
"meta/cronard.php"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use function DI\autowire, DI\create, DI\get;
|
||||||
|
|
||||||
|
use Laminas\Diactoros\Response\HtmlResponse;
|
||||||
|
|
||||||
|
use Cronard\CronardMiddleware,
|
||||||
|
Notes\Cronard\TaskFetcher;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
|
use Lean\Lean;
|
||||||
|
|
||||||
|
return [
|
||||||
|
CronardMiddleware::class => function($c) {
|
||||||
|
$cronardMiddleware = new CronardMiddleware($c, getenv('CRON_KEY'), function() : ResponseInterface {
|
||||||
|
return new HtmlResponse(sprintf("%s - cron task begin...", date('Y-m-d H:i:s')));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return $cronardMiddleware->fromAnnotations($c->get(TaskFetcher::class));
|
||||||
|
},
|
||||||
|
|
||||||
|
TaskFetcher::class => function($c) {
|
||||||
|
$fetcher = new TaskFetcher(null, null, $c->get('cronard.caching'));
|
||||||
|
|
||||||
|
$fetcher->setFolderList(array_map(function($item) {
|
||||||
|
return $item;
|
||||||
|
}, $c->get(Lean::class)->getCronard()));
|
||||||
|
|
||||||
|
return $fetcher;
|
||||||
|
},
|
||||||
|
];
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace Cronard;
|
namespace Cronard;
|
||||||
|
|
||||||
use Notes\Cronard\TaskFetcher;
|
|
||||||
use Psr\Http\Message\ResponseFactoryInterface,
|
use Psr\Http\Message\ResponseFactoryInterface,
|
||||||
Psr\Container\ContainerInterface,
|
Psr\Container\ContainerInterface,
|
||||||
Psr\Http\Message\ResponseInterface,
|
Psr\Http\Message\ResponseInterface,
|
||||||
@@ -22,10 +21,7 @@ class CronardMiddleware implements MiddlewareInterface
|
|||||||
|
|
||||||
public $container;
|
public $container;
|
||||||
|
|
||||||
public ? TaskFetcher $taskFetcher;
|
|
||||||
|
|
||||||
public function __construct(ContainerInterface $container, string $cronKey = "", $responseInterface = null, $variables = []) {
|
public function __construct(ContainerInterface $container, string $cronKey = "", $responseInterface = null, $variables = []) {
|
||||||
|
|
||||||
$this->cronKey = $cronKey;
|
$this->cronKey = $cronKey;
|
||||||
$this->response = $responseInterface;
|
$this->response = $responseInterface;
|
||||||
$this->variables = $variables;
|
$this->variables = $variables;
|
||||||
@@ -36,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,
|
||||||
|
|||||||
+19
-5
@@ -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,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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user