Skip to content
This repository was archived by the owner on May 7, 2023. It is now read-only.

Commit 86352a9

Browse files
committed
jobs iteration, trigger jobs from foomo toolbox and improved on dead job detection
1 parent 8b57326 commit 86352a9

File tree

10 files changed

+125
-65
lines changed

10 files changed

+125
-65
lines changed

lib/Foomo/JobList.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class JobList implements Jobs\JobListInterface
2828
{
2929
public static function getJobs()
3030
{
31-
$jobs = array(Config\TempFileGCCreator::getGC());
31+
$jobs = [
32+
Config\TempFileGCCreator::getGC(),
33+
new BasicAuth\Token\GC()
34+
];
3235
if(Session::getEnabled()) {
3336
$jobs[] = Session\GCJob::create()->maxExecutionTime(3000);
3437
}

lib/Foomo/Jobs/AbstractJob.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ public static function create()
235235
return new $className;
236236
}
237237

238+
public function getURL()
239+
{
240+
return \Foomo\Utils::getServerUrl() . \Foomo\Module::getHtdocsPath('jobRunner.php') . "/" . urlencode($this->getSecretId(Utils::getExecutionSecret()));
241+
}
242+
238243
/**
239244
* do your thing here
240245
*/

lib/Foomo/Jobs/Frontend/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Controller
3030
/**
3131
* my model
3232
*
33-
* @var Foomo\Jobs\Frontend\Model
33+
* @var \Foomo\Jobs\Frontend\Model
3434
*/
3535
public $model;
3636
public function actionDefault() {}

lib/Foomo/Jobs/Runner.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,23 @@ public static function runAJob(AbstractJob $job) {
7676
}
7777
}
7878
Utils::updateStatusJobStart($jobId, $pid, $locked);
79-
80-
call_user_func_array(array($job, 'run'), array());
79+
try {
80+
call_user_func_array(array($job, 'run'), array());
81+
Utils::updateStatusJobDone($jobId, $pid);
82+
} catch(\Exception $e) {
83+
Utils::updateStatusJobError($jobId, $pid, $errorCode = JobStatus::ERROR_DIED, $errorMessage = $e->getMessage(), $isRunning = false, $isLocked = false);
84+
}
8185

82-
Utils::updateStatusJobDone($jobId, $pid, $locked);
8386
if ($job->getLock()) {
8487
// clean up
8588
\Foomo\Lock::release($job->getId());
86-
Utils::updateStatusJobDone($jobId, $pid, false);
87-
self::$callback = false;
8889
}
90+
self::$callback = false;
8991
}
9092

9193
public static function shutDownListener($params) {
9294
if (self::$callback) {
95+
trigger_error(__METHOD__);
9396
$error = error_get_last();
9497
if (isset($error['type']) && ($error['type'] === E_ERROR || $error['type'] === E_USER_ERROR)) {
9598
echo "Can do custom output and/or logging for fatal error here...";

lib/Foomo/Jobs/Utils.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,27 @@ public static function collectJobs() {
4848
*/
4949
public static function getStatus(AbstractJob $job) {
5050
$jobId = $job->getId();
51-
return self::getPersistedStatus($jobId);
51+
$currentStatus = self::getPersistedStatus($jobId);
52+
if($currentStatus->isLocked && !\Foomo\Lock::isLocked($jobId)) {
53+
// that is a locking lie
54+
\Foomo\Lock::lock($jobId, true);
55+
$persisted = self::getPersistedStatus($jobId);
56+
if($persisted->isLocked) {
57+
// that is bullshit - we got the lock - sbdy died
58+
self::updateStatusJobError(
59+
$jobId,
60+
$persisted->pid,
61+
JobStatus::ERROR_DIED,
62+
"sbdy died alone",
63+
false,
64+
false
65+
);
66+
}
67+
\Foomo\Lock::release($job->getId());
68+
return self::getPersistedStatus($jobId);
69+
} else {
70+
return $currentStatus;
71+
}
5272
}
5373

5474
public static function getExecutionSecret() {
@@ -145,7 +165,8 @@ public static function updateStatusJobDone($jobId, $pid) {
145165
* @param string $isRunning one of self::STATUS_
146166
* @param boolean $isLocked
147167
*/
148-
public static function updateStatusJobError($jobId, $pid, $errorCode, $errorMessage, $isRunning = false, $isLocked = false) {
168+
public static function updateStatusJobError($jobId, $pid, $errorCode, $errorMessage, $isRunning = false, $isLocked = false)
169+
{
149170
$status = self::getPersistedStatus($jobId);
150171
$fileName = self::getJobStatusFile($jobId);
151172
$status->status = $isRunning ? JobStatus::STATUS_RUNNING : JobStatus::STATUS_NOT_RUNNING;
@@ -158,19 +179,19 @@ public static function updateStatusJobError($jobId, $pid, $errorCode, $errorMess
158179
self::log($jobId, $status);
159180
self::persistStatus($fileName, $status);
160181
self::sendNotificationEmail($status->errorCode, $status->errorMessage);
161-
162-
163-
164182
}
165183

166-
private static function getPersistedStatus($jobId) {
184+
private static function getPersistedStatus($jobId)
185+
{
167186
$fileName = self::getJobStatusFile($jobId);
187+
$status = new JobStatus();
168188
if (file_exists($fileName)) {
169-
$contents = unserialize(file_get_contents($fileName));
170-
} else {
171-
$contents = new JobStatus();
189+
$unserialized = unserialize(file_get_contents($fileName));
190+
if(is_object($unserialized) && $unserialized instanceof JobStatus) {
191+
$status = $unserialized;
192+
}
172193
}
173-
return $contents;
194+
return $status;
174195
}
175196

176197
private static function getJobStatusFile($jobId) {
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
<?php
22
namespace Foomo\Jobs\Mock;
3-
3+
44
/**
55
* @link www.foomo.org
66
* @license www.gnu.org/licenses/lgpl.txt
77
* @author Jan Halfar [email protected]
88
*/
99
class DieWhileWorkingJob extends \Foomo\Jobs\AbstractJob
1010
{
11-
protected $executionRule = '* * * * *';
12-
13-
public function getMaxExecutionTime() {
14-
return 1;
15-
}
16-
public function getId()
17-
{
18-
return sha1(__CLASS__);
19-
}
20-
public function getDescription()
21-
{
22-
return 'sleep for some time and die before waking up';
23-
}
24-
public function run()
25-
{
26-
//i should live forever
27-
while (true) {
28-
$i = 1 + 1;
29-
30-
}
31-
}
11+
protected $executionRule = '* * * * *';
12+
13+
public function getMaxExecutionTime()
14+
{
15+
return 1;
16+
}
17+
18+
public function getId()
19+
{
20+
return sha1(__CLASS__);
21+
}
22+
23+
public function getDescription()
24+
{
25+
return 'sleep for some time and die before waking up';
26+
}
27+
28+
public function run()
29+
{
30+
self::fuckoff();
31+
}
3232
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace Foomo\Jobs\Mock;
3+
4+
/**
5+
* @link www.foomo.org
6+
* @license www.gnu.org/licenses/lgpl.txt
7+
* @author Jan Halfar [email protected]
8+
*/
9+
class DieWithExceptionJob extends \Foomo\Jobs\AbstractJob
10+
{
11+
protected $executionRule = '* * * * *';
12+
13+
public function getMaxExecutionTime()
14+
{
15+
return 1;
16+
}
17+
18+
public function getId()
19+
{
20+
return sha1(__CLASS__);
21+
}
22+
23+
public function getDescription()
24+
{
25+
return 'sleep for some time and die before waking up';
26+
}
27+
28+
public function run()
29+
{
30+
throw new \Exception(__METHOD__);
31+
}
32+
}

tests/Foomo/Jobs/RunnerTest.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@ public function tearDown() {
1515
}
1616

1717
public function testRun() {
18-
$jobs = Utils::collectJobs();
19-
$executionSecret = Utils::getExecutionSecret();
2018
\Foomo\Jobs\Runner::runAJob(Mock\SleeperJob::create());
2119
$status = \Foomo\Jobs\Utils::getStatus(Mock\SleeperJob::create());
2220
$this->assertEquals(JobStatus::STATUS_NOT_RUNNING, $status->status);
2321
}
2422

2523
public function testExecutionRuleValidation() {
26-
$executionSecret = Utils::getExecutionSecret();
27-
24+
2825
try{
2926
Mock\SleeperJob::create()->executionRule('* * * * *');
3027
} catch (\Exception $e) {
@@ -125,15 +122,28 @@ public function testExiterJob() {
125122

126123
public function testDieWhileWorkingJob() {
127124
self::callAsync('DieWhileWorkingJob');
128-
sleep(2);
125+
sleep(1);
129126
$status = \Foomo\Jobs\Utils::getStatus(Mock\DieWhileWorkingJob::create());
127+
$json = json_encode($status, JSON_PRETTY_PRINT) . " time: " . time();
130128
$this->assertNotEquals(getmypid(), $status->pid, 'pid should differ');
131-
$this->assertFalse($status->isLocked, 'should not be locked');
132-
$this->assertEquals(JobStatus::STATUS_NOT_RUNNING, $status->status, 'we should not be running now');
133-
$this->assertEquals(JobStatus::ERROR_DIED, $status->errorCode);
129+
$this->assertFalse($status->isLocked, 'should not be locked ' . $json);
130+
$this->assertEquals(JobStatus::STATUS_NOT_RUNNING, $status->status, 'we should not be running now ' . $json);
131+
$this->assertEquals(JobStatus::ERROR_DIED, $status->errorCode, $json);
134132

135133
}
136-
134+
public function testDieWithExceptionJob() {
135+
self::callAsync('DieWithExceptionJob');
136+
sleep(1);
137+
$status = \Foomo\Jobs\Utils::getStatus(Mock\DieWithExceptionJob::create());
138+
$json = json_encode($status, JSON_PRETTY_PRINT) . " time: " . time();
139+
$this->assertFalse($status->isLocked, 'should not be locked ' . $json);
140+
$this->assertEquals(JobStatus::STATUS_NOT_RUNNING, $status->status, 'we should not be running now ' . $json);
141+
$this->assertEquals(JobStatus::ERROR_DIED, $status->errorCode, $json);
142+
$this->assertEquals(__NAMESPACE__ . "\\Mock\\DieWithExceptionJob::run", $status->errorMessage, $json);
143+
144+
145+
}
146+
137147

138148
public function testConcurrency() {
139149
self::callAsync('SleeperJob');

tests/Foomo/Jobs/runJob.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,7 @@
22

33
if (isset($_GET['job'])) {
44
$jobName = $_GET['job'];
5-
6-
if ($jobName == 'SleeperJob') {
7-
\Foomo\Jobs\Runner::runAJob(\Foomo\Jobs\Mock\SleeperJob::create());
8-
}
9-
10-
if ($jobName == 'DierJob') {
11-
\Foomo\Jobs\Runner::runAJob(\Foomo\Jobs\Mock\DierJob::create());
12-
}
13-
14-
if ($jobName == 'ExiterJob') {
15-
\Foomo\Jobs\Runner::runAJob(\Foomo\Jobs\Mock\ExiterJob::create());
16-
}
17-
18-
if ($jobName == 'DieWhileWorkingJob') {
19-
\Foomo\Jobs\Runner::runAJob(\Foomo\Jobs\Mock\DieWhileWorkingJob::create());
20-
}
5+
$job = call_user_func_array([ "Foomo\\Jobs\\Mock\\" . $jobName, "create"], []);
6+
\Foomo\Jobs\Runner::runAJob($job);
217
}
228

views/Foomo/Jobs/Frontend/partials/job.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/* @var $view Foomo\MVC\View */
44
?>
55
<tr>
6-
<td colspan="9"><?= $module . ' ' . get_class($job) ?></td>
6+
<td colspan="9"><?= $module . ' ' . $view->escape(get_class($job)) ?> - <a href="<?= $view->escape($job->getURL()) ?>">run job</a></td>
77
</tr>
88
<tr style="<?= \Foomo\Jobs\Utils::getStatus($job)->isOk() ? 'color:black' : 'color:red' ?>">
99
<td><?= $view->escape($job->getExecutionRule()) ?></td>

0 commit comments

Comments
 (0)