Skip to content

Commit 33a63f4

Browse files
committed
Merge pull request jobbyphp#44 from CarsonF/feature/extendable-background-job
Configurable background job
2 parents 77630fd + 3def4cd commit 33a63f4

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

bin/run-job

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
5+
require_once __DIR__ . '/../vendor/autoload.php';
6+
} else {
7+
require_once __DIR__ . '/../../../autoload.php';
8+
}
9+
10+
parse_str($argv[2], $config);
11+
12+
$cls = $config['jobClass'];
13+
14+
if (!is_a($cls, 'Jobby\BackgroundJob', true)) {
15+
throw new Jobby\Exception('"jobClass" needs to be an instanceof Jobby\BackgroundJob');
16+
}
17+
18+
/** @var \Jobby\BackgroundJob $job */
19+
$job = new $cls($argv[1], $config);
20+
$job->run();

src/BackgroundJob.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -284,23 +284,3 @@ protected function runFile()
284284
}
285285
}
286286
}
287-
288-
// run this file, if executed directly
289-
// @see: http://stackoverflow.com/questions/2413991/php-equivalent-of-pythons-name-main
290-
// @codeCoverageIgnoreStart
291-
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
292-
if (!empty($trace)) {
293-
return;
294-
}
295-
296-
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
297-
require_once __DIR__ . '/../vendor/autoload.php';
298-
} else {
299-
require_once __DIR__ . '/../../../autoload.php';
300-
}
301-
302-
global $argv;
303-
parse_str($argv[2], $config);
304-
$job = new BackgroundJob($argv[1], $config);
305-
$job->run();
306-
// @codeCoverageIgnoreEnd

src/Jobby.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(array $config = [])
3535
$this->setConfig($this->getDefaultConfig());
3636
$this->setConfig($config);
3737

38-
$this->script = __DIR__ . DIRECTORY_SEPARATOR . 'BackgroundJob.php';
38+
$this->script = realpath(__DIR__ . '/../bin/run-job');
3939
}
4040

4141
/**
@@ -56,6 +56,7 @@ protected function getHelper()
5656
public function getDefaultConfig()
5757
{
5858
return [
59+
'jobClass' => 'Jobby\BackgroundJob',
5960
'recipients' => null,
6061
'mailer' => 'sendmail',
6162
'maxRuntime' => null,

tests/JobbyTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public function testShouldFailIfMaxRuntimeExceeded()
286286
$jobby->run();
287287
sleep(2);
288288
$jobby->run();
289-
sleep(1);
289+
sleep(2);
290290

291291
$this->assertContains('ERROR: MaxRuntime of 1 secs exceeded!', $this->getLogContent());
292292
}

0 commit comments

Comments
 (0)