Skip to content
This repository has been archived by the owner on May 1, 2022. It is now read-only.

Commit

Permalink
fail job on process termination
Browse files Browse the repository at this point in the history
  • Loading branch information
sebsel committed Nov 5, 2017
1 parent 729ed53 commit 7cc8549
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Basic queue to the Kirby CMS, using Cron and Kirby's flat file system",
"author": "Sebastiaan Andeweg",
"license": "MIT",
"version": "2.0.1",
"version": "2.1.0",
"type": "kirby-plugin",
"files": [
"package.json",
Expand Down
28 changes: 18 additions & 10 deletions queue-for-kirby.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function get($key, $default = null)
class Queue
{
private static $actions = [];
public static $current_job;

/**
* Defines an action to perform when job is worked on
Expand Down Expand Up @@ -107,21 +108,27 @@ public static function work()
// Protect ourselfs against multiple workers at once
if (static::isWorking()) exit();
static::setWorking();
register_shutdown_function(function(){
if(queue::$current_job) {
queue::failed(queue::$current_job, 'Job action terminated execution');
queue::stopWorking();
}
});

if (static::hasJobs()) {
$job = static::_get_next_job();
static::$current_job = static::_get_next_job();
try {
if (!isset(static::$actions[$job->name()])
or !is_callable(static::$actions[$job->name()])) {
throw new Error("Action '{$job->name()}' not defined");
if (!isset(static::$actions[static::$current_job->name()])
or !is_callable(static::$actions[static::$current_job->name()])) {
throw new Error("Action '" . static::$current_job->name() . "'' not defined");
}
if (call_user_func(static::$actions[$job->name()], $job) === false) {
if (call_user_func(static::$actions[static::$current_job->name()], static::$current_job) === false) {
throw new Error('Job returned false');
}
} catch (Exception $e) {
static::failed($job, $e->getMessage());
static::failed(static::$current_job, $e->getMessage());
} catch (Error $e) {
static::failed($job, $e->getMessage());
static::failed(static::$current_job, $e->getMessage());
}
}

Expand Down Expand Up @@ -231,18 +238,19 @@ public static function failedPath()
return static::path() . DS . '.failed';
}

private static function isWorking()
public static function isWorking()
{
return f::exists(static::path() . DS . '.working');
}

private static function setWorking()
public static function setWorking()
{
dir::make(static::path() . DS . '.working');
}

private static function stopWorking()
public static function stopWorking()
{
queue::$current_job = null;
dir::remove(static::path() . DS . '.working');
}
}
22 changes: 22 additions & 0 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,4 +388,26 @@ public function job_fails_on_non_existent_action()
$this->assertEquals($job['name'], 'job_without_action');
$this->assertEquals($job['error'], 'Action \'job_without_action\' not defined');
}

/** @test */
public function job_fails_on_teminating_process()
{
// # This one is impossible to test via PHPUnit, because we
// # exit the PHP-unit process as well. To test this function,
// # uncomment it, run the test it, and see the contents of
// # the `site/queue/.failed` folder yourself.
//
// # Assert for yourself that:
// # $job['name'] == 'teminating_job'
// # $job['error'] == 'Job action terminated execution'
//
// # After that, remove the file from the queue.
//
// queue::define('teminating_job', function() {
// die();
// });
// queue::add('teminating_job');
// queue::work();
}

}

0 comments on commit 7cc8549

Please sign in to comment.