-
Notifications
You must be signed in to change notification settings - Fork 759
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Resque_Job_PID so a process PID can be obtained.
- Loading branch information
Showing
7 changed files
with
164 additions
and
23 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
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,43 @@ | ||
<?php | ||
/** | ||
* PID tracker for the forked worker job. | ||
* | ||
* @package Resque/Job | ||
* @author Chris Boulton <chris@bigcommerce.com> | ||
* @license http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
class Resque_Job_PID | ||
{ | ||
/** | ||
* Create a new PID tracker item for the supplied job ID. | ||
* | ||
* @param string $id The ID of the job to track the PID of. | ||
*/ | ||
public static function create($id) | ||
{ | ||
Resque::redis()->set('job:' . $id . ':pid', (string)getmypid()); | ||
} | ||
|
||
/** | ||
* Fetch the PID for the process actually executing the job. | ||
* | ||
* @param string $id The ID of the job to get the PID of. | ||
* | ||
* @return int PID of the process doing the job (on non-forking OS, PID of the worker, otherwise forked PID). | ||
*/ | ||
public static function get($id) | ||
{ | ||
return (int)Resque::redis()->get('job:' . $id . ':pid'); | ||
} | ||
|
||
/** | ||
* Remove the PID tracker for the job. | ||
* | ||
* @param string $id The ID of the job to remove the tracker from. | ||
*/ | ||
public static function del($id) | ||
{ | ||
Resque::redis()->del('job:' . $id . ':pid'); | ||
} | ||
} | ||
|
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,47 @@ | ||
<?php | ||
/** | ||
* Resque_Job_PID tests. | ||
* | ||
* @package Resque/Tests | ||
* @author Chris Boulton <chris@bigcommerce.com> | ||
* @license http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
class Resque_Tests_JobPIDTest extends Resque_Tests_TestCase | ||
{ | ||
/** | ||
* @var \Resque_Worker | ||
*/ | ||
protected $worker; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
// Register a worker to test with | ||
$this->worker = new Resque_Worker('jobs'); | ||
$this->worker->setLogger(new Resque_Log()); | ||
} | ||
|
||
public function testQueuedJobDoesNotReturnPID() | ||
{ | ||
$token = Resque::enqueue('jobs', 'Test_Job', null, true); | ||
$this->assertEquals(0, Resque_Job_PID::get($token)); | ||
} | ||
|
||
public function testRunningJobReturnsPID() | ||
{ | ||
// Cannot use InProgress_Job on non-forking OS. | ||
if(!function_exists('pcntl_fork')) return; | ||
|
||
$token = Resque::enqueue('jobs', 'InProgress_Job', null, true); | ||
$this->worker->work(0); | ||
$this->assertNotEquals(0, Resque_Job_PID::get($token)); | ||
} | ||
|
||
public function testFinishedJobDoesNotReturnPID() | ||
{ | ||
$token = Resque::enqueue('jobs', 'Test_Job', null, true); | ||
$this->worker->work(0); | ||
$this->assertEquals(0, Resque_Job_PID::get($token)); | ||
} | ||
} |
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