Skip to content

Commit

Permalink
Added a simple Job-System
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Feb 19, 2021
1 parent 98976d9 commit 932b1d1
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 5 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"name": "interaapps/ulole",
"type": "library",
"version": "3.0.4",
"version": "3.0.5",
"authors": [
{
"name": "JulianFun123",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.0.0"
"php": ">=7.0.0",
"interaapps/uloleorm": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
12 changes: 12 additions & 0 deletions de/interaapps/ulole/core/WebApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,6 +11,7 @@ abstract class WebApplication {
private $router;
private $environment;
private $inCLI = false;
private $jobHandler;

public function start(Environment $environment){
$this->environment = $environment;
Expand All @@ -18,6 +20,8 @@ public function start(Environment $environment){
->setIncludeDirectory("resources/views")
->setNamespace("app\\controller");

$this->jobHandler = new JobHandler();

$this->init();

/**
Expand Down Expand Up @@ -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;
}
}
13 changes: 12 additions & 1 deletion de/interaapps/ulole/core/cli/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class CLI {
public $commands = [];
public $descriptions = [];
public $applicationAttribs = [];

/**
* Change the not found errormessage
*/
Expand All @@ -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="") {
Expand Down Expand Up @@ -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;
}
}

?>
15 changes: 14 additions & 1 deletion de/interaapps/ulole/core/cli/CLIBootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

}
34 changes: 34 additions & 0 deletions de/interaapps/ulole/core/cli/modules/JobCLI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php


namespace de\interaapps\ulole\core\cli\modules;

use de\interaapps\ulole\core\cli\CLI;
use de\interaapps\ulole\core\cli\CLIHandler;
use de\interaapps\ulole\core\cli\Colors;
use de\interaapps\ulole\core\jobs\JobHandler;

class JobCLI extends CLIHandler
{

private $jobHandler;

public function __construct(JobHandler $jobHandler) {
$this->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");
}
}
6 changes: 6 additions & 0 deletions de/interaapps/ulole/core/jobs/Job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace de\interaapps\ulole\core\jobs;

interface Job {
function run(JobHandler $jobHandler = null);
}
80 changes: 80 additions & 0 deletions de/interaapps/ulole/core/jobs/JobHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
namespace de\interaapps\ulole\core\jobs;

class JobHandler
{
private $database;
private $mode = "database";

public function __construct($database = "main") {
$this->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;
}
}
16 changes: 16 additions & 0 deletions de/interaapps/ulole/core/jobs/JobModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace de\interaapps\ulole\core\jobs;

use de\interaapps\ulole\orm\ORMModel;

class JobModel {
use ORMModel;

public $id;
public $object;
public $state;
public $repeat;
public $failed_count;
public $created_at;

}
31 changes: 31 additions & 0 deletions de/interaapps/ulole/core/jobs/JobsMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace de\interaapps\ulole\core\jobs;


use de\interaapps\ulole\orm\Database;
use de\interaapps\ulole\orm\migration\Blueprint;
use de\interaapps\ulole\orm\migration\Migration;

class JobsMigration implements Migration
{
private $tableName = "uloleorm_jobs";
public function up(Database $database) {
return $database->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;
}
}
2 changes: 1 addition & 1 deletion uppm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ulole",
"version": "3.0.4",
"version": "3.0.5",
"description": "",
"author": "",
"keywords": [
Expand Down

0 comments on commit 932b1d1

Please sign in to comment.