-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98976d9
commit 932b1d1
Showing
10 changed files
with
209 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [ | ||
|