diff --git a/composer.json b/composer.json index 4be3a93..14531a9 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "interaapps/ulole", "type": "library", - "version": "3.0.4", + "version": "3.0.5", "authors": [ { "name": "JulianFun123", @@ -9,7 +9,8 @@ } ], "require": { - "php": ">=7.0.0" + "php": ">=7.0.0", + "interaapps/uloleorm": "^2.0" }, "autoload": { "psr-4": { diff --git a/de/interaapps/ulole/core/WebApplication.php b/de/interaapps/ulole/core/WebApplication.php index b7d9cd0..68dbc9c 100644 --- a/de/interaapps/ulole/core/WebApplication.php +++ b/de/interaapps/ulole/core/WebApplication.php @@ -2,6 +2,7 @@ namespace de\interaapps\ulole\core; use de\interaapps\ulole\core\config\Configuration; +use de\interaapps\ulole\core\jobs\JobHandler; use de\interaapps\ulole\orm\Database; use de\interaapps\ulole\orm\UloleORM; use de\interaapps\ulole\router\Router; @@ -10,6 +11,7 @@ abstract class WebApplication { private $router; private $environment; private $inCLI = false; + private $jobHandler; public function start(Environment $environment){ $this->environment = $environment; @@ -18,6 +20,8 @@ public function start(Environment $environment){ ->setIncludeDirectory("resources/views") ->setNamespace("app\\controller"); + $this->jobHandler = new JobHandler(); + $this->init(); /** @@ -72,4 +76,12 @@ public function setInCLI($inCLI) : WebApplication { $this->inCLI = $inCLI; return $this; } + + public function getJobHandler() : JobHandler { + return $this->jobHandler; + } + + public function setJobHandler($jobHandler): void { + $this->jobHandler = $jobHandler; + } } \ No newline at end of file diff --git a/de/interaapps/ulole/core/cli/CLI.php b/de/interaapps/ulole/core/cli/CLI.php index 48c529d..c51ddc8 100644 --- a/de/interaapps/ulole/core/cli/CLI.php +++ b/de/interaapps/ulole/core/cli/CLI.php @@ -4,6 +4,8 @@ class CLI { public $commands = []; public $descriptions = []; + public $applicationAttribs = []; + /** * Change the not found errormessage */ @@ -17,7 +19,7 @@ class CLI { /** * Register a new command * @param String function-name (Command) - * @param Function function (example:function() {return "Hello world";}) + * @param Closure function (example:function() {return "Hello world";}) * @param String (Optional) Description */ public function register(string $name, $func, string $description="") { @@ -59,6 +61,15 @@ public function getCommands(): array { public function getDescriptions(): array { return $this->descriptions; } + + public function getApplicationAttrib($i) { + return $this->applicationAttribs[$i]; + } + + public function setApplicationAttrib($key, $value): CLI { + $this->applicationAttribs[$key] = $value; + return $this; + } } ?> \ No newline at end of file diff --git a/de/interaapps/ulole/core/cli/CLIBootstrapper.php b/de/interaapps/ulole/core/cli/CLIBootstrapper.php index cb6c708..81fff3e 100644 --- a/de/interaapps/ulole/core/cli/CLIBootstrapper.php +++ b/de/interaapps/ulole/core/cli/CLIBootstrapper.php @@ -3,8 +3,10 @@ use de\interaapps\ulole\core\cli\modules\create\CreateORMCLI; use de\interaapps\ulole\core\cli\modules\DBNavigatorCLI; +use de\interaapps\ulole\core\cli\modules\JobCLI; use de\interaapps\ulole\core\cli\modules\ORMCLI; use de\interaapps\ulole\core\cli\modules\ReplCLI; +use de\interaapps\ulole\core\WebApplication; class CLIBootstrapper { public $cli, $args; @@ -13,22 +15,33 @@ public function __construct($args) { $this->cli = new CLI(); $this->args = $args; + } + + public function addFrameworkHandlers(WebApplication $app): CLIBootstrapper { $this->register(new FrameworkCLI()) ->register(new ReplCLI()) ->register(new ORMCLI) ->register(new DBNavigatorCLI()) ->register(new CreateORMCLI()) - ; + ->register(new JobCLI($app->getJobHandler())); + return $this; } + public function register(CLIHandler $cliHandler) : CLIBootstrapper { $cliHandler->registerCommands($this->cli); return $this; } + public function setApplicationAttrib($key, $value): CLIBootstrapper { + $this->cli->setApplicationAttrib($key, $value); + return $this; + } + public function run() { $command = $this->args[1]; $this->cli->run($this->args, $command); } + } \ No newline at end of file diff --git a/de/interaapps/ulole/core/cli/modules/JobCLI.php b/de/interaapps/ulole/core/cli/modules/JobCLI.php new file mode 100644 index 0000000..21cc81b --- /dev/null +++ b/de/interaapps/ulole/core/cli/modules/JobCLI.php @@ -0,0 +1,34 @@ +jobHandler = $jobHandler; + } + + public function registerCommands(CLI $cli) + { + + $cli->register("jobs:work", function ($args) { + while (true) { + Colors::info("Executing..."); + + foreach ($this->jobHandler->handleAll() as $exception) { + Colors::error("Error while execution: ".$exception->getMessage()); + } + sleep(isset($args[2]) ? $args[2] : 3); + } + }, "A script to wait for new jobs and runs them"); + } +} \ No newline at end of file diff --git a/de/interaapps/ulole/core/jobs/Job.php b/de/interaapps/ulole/core/jobs/Job.php new file mode 100644 index 0000000..cac6f0d --- /dev/null +++ b/de/interaapps/ulole/core/jobs/Job.php @@ -0,0 +1,6 @@ +database = $database; + } + + public function push(Job $job, $repeat = 3): bool { + if ($this->mode == "database") { + $jobModel = new JobModel(); + $jobModel->object = serialize($job); + $jobModel->state = "OPEN"; + $jobModel->repeat = $repeat; + return $jobModel->save(); + } else if ($this->mode == "sync") { + $tries = 0; + while ($tries++ < $repeat) { + if ($this->runNow($job)) + return true; + } + } + return false; + } + + public function runNow(Job $job) : bool { + try { + $job->run($this); + return true; + } catch (\Exception $e) {} + return false; + } + + public function getOpened(): array { + return JobModel::table($this->database)->where("state", "OPEN")->orWhere("state", "FAILED")->all(); + } + + public function handleAll() : array { + $errors = []; + foreach ($this->getOpened() as $jobModel) { + $jobModel->state = "RUNNING"; + $jobModel->save($this->database); + if ($jobModel->failed_count++ < $jobModel->repeat) { + try { + $job = unserialize($jobModel->object); + $job->run($this); + + $jobModel->delete(); + } catch (\Exception $e) { + $errors[] = $e; + $jobModel->state = "FAILED"; + $jobModel->save($this->database); + } + } + } + return $errors; + } + + /** + * @param string $mode + * @return JobHandler + */ + public function setMode(string $mode): JobHandler + { + $this->mode = $mode; + return $this; + } + + /** + * @return string + */ + public function getMode(): string + { + return $this->mode; + } +} \ No newline at end of file diff --git a/de/interaapps/ulole/core/jobs/JobModel.php b/de/interaapps/ulole/core/jobs/JobModel.php new file mode 100644 index 0000000..8a0c584 --- /dev/null +++ b/de/interaapps/ulole/core/jobs/JobModel.php @@ -0,0 +1,16 @@ +create($this->tableName, function (Blueprint $blueprint) { + $blueprint->id(); + $blueprint->string("object"); + $blueprint->enum("state", ["OPEN", "RUNNING", "FAILED"])->default('OPEN'); + $blueprint->int("repeat")->default(3); + $blueprint->int("failed_count")->default(0); + $blueprint->timestamp("created_at")->currentTimestamp(); + }); + } + + public function down(Database $database) { + return $database->drop($this->tableName); + + } + + public function setTableName($tableName): void { + $this->tableName = $tableName; + } +} \ No newline at end of file diff --git a/uppm.json b/uppm.json index 7c3c16b..75456ee 100644 --- a/uppm.json +++ b/uppm.json @@ -1,6 +1,6 @@ { "name": "ulole", - "version": "3.0.4", + "version": "3.0.5", "description": "", "author": "", "keywords": [