Skip to content

Commit

Permalink
Introduce namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Dec 30, 2021
1 parent 3bc9e6f commit 77da6db
Show file tree
Hide file tree
Showing 40 changed files with 557 additions and 375 deletions.
38 changes: 19 additions & 19 deletions bin/resque
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ $REDIS_BACKEND = getenv('REDIS_BACKEND');
$REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
if(!empty($REDIS_BACKEND)) {
if (empty($REDIS_BACKEND_DB))
Resque::setBackend($REDIS_BACKEND);
\Resque\Resque::setBackend($REDIS_BACKEND);
else
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
\Resque\Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
}

$logLevel = false;
Expand All @@ -70,7 +70,7 @@ if($APP_INCLUDE) {
// See if the APP_INCLUDE containes a logger object,
// If none exists, fallback to internal logger
if (!isset($logger) || !is_object($logger)) {
$logger = new Resque_Log($logLevel);
$logger = new \Resque\Logger($logLevel);
}

$BLOCKING = getenv('BLOCKING') !== FALSE;
Expand All @@ -89,8 +89,8 @@ if(!empty($COUNT) && $COUNT > 1) {

$PREFIX = getenv('PREFIX');
if(!empty($PREFIX)) {
$logger->log(Psr\Log\LogLevel::INFO, 'Prefix set to {prefix}', array('prefix' => $PREFIX));
Resque_Redis::prefix($PREFIX);
$logger->log(\Psr\Log\LogLevel::INFO, 'Prefix set to {prefix}', array('prefix' => $PREFIX));
\Resque\Redis::prefix($PREFIX);
}

function cleanup_children($signal){
Expand All @@ -100,19 +100,19 @@ function cleanup_children($signal){
if($count > 1) {
$children = array();
$GLOBALS['send_signal'] = FALSE;

$die_signals = array(SIGTERM, SIGINT, SIGQUIT);
$all_signals = array_merge($die_signals, array(SIGUSR1, SIGUSR2, SIGCONT, SIGPIPE));

for($i = 0; $i < $count; ++$i) {
$pid = Resque::fork();
$pid = \Resque\Resque::fork();
if($pid == -1) {
die("Could not fork worker ".$i."\n");
}
// Child, start the worker
elseif(!$pid) {
$queues = explode(',', $QUEUE);
$worker = new Resque_Worker($queues);
$worker = new \Resque\Worker\ResqueWorker($queues);
$worker->logLevel = $logLevel;
$worker->hasParent = TRUE;
fwrite(STDOUT, '*** Starting worker '.$worker."\n");
Expand All @@ -127,27 +127,27 @@ if($count > 1) {
foreach ($all_signals as $signal) {
pcntl_signal($signal, "cleanup_children");
}

$PIDFILE = getenv('PIDFILE');
if ($PIDFILE) {
if(file_put_contents($PIDFILE, getmypid()) === false){
$logger->log(Psr\Log\LogLevel::NOTICE, 'Could not write PID information to {pidfile}', array('pidfile' => $PIDFILE));
$logger->log(\Psr\Log\LogLevel::NOTICE, 'Could not write PID information to {pidfile}', array('pidfile' => $PIDFILE));
die(2);
}
}

$registered = TRUE;
}

if(function_exists('setproctitle')) {
setproctitle('resque-' . Resque::VERSION . ": Monitoring {$count} children: [".implode(',', array_keys($children))."]");
setproctitle('resque-' . \Resque\Resque::VERSION . ": Monitoring {$count} children: [".implode(',', array_keys($children))."]");
}

$childPID = pcntl_waitpid(-1, $childStatus, WNOHANG);
if ($childPID != 0) {
fwrite(STDOUT, "*** A child worker died: {$childPID}\n");
unset($children[$childPID]);
$i--;
$i--;
}
usleep(250000);
if ($GLOBALS['send_signal'] !== FALSE){
Expand All @@ -169,19 +169,19 @@ if($count > 1) {
// Start a single worker
else {
$queues = explode(',', $QUEUE);
$worker = new Resque_Worker($queues);
$worker = new \Resque\Worker\ResqueWorker($queues);
$worker->logLevel = $logLevel;
$worker->hasParent = FALSE;

$PIDFILE = getenv('PIDFILE');
if ($PIDFILE) {
if(file_put_contents($PIDFILE, getmypid()) === false) {
$logger->log(Psr\Log\LogLevel::NOTICE, 'Could not write PID information to {pidfile}', array('pidfile' => $PIDFILE));
$logger->log(\Psr\Log\LogLevel::NOTICE, 'Could not write PID information to {pidfile}', array('pidfile' => $PIDFILE));
die(2);
}
}

$logger->log(Psr\Log\LogLevel::NOTICE, 'Starting worker {worker}', array('worker' => $worker));
$logger->log(\Psr\Log\LogLevel::NOTICE, 'Starting worker {worker}', array('worker' => $worker));
$worker->work($interval, $BLOCKING);
}
?>
14 changes: 7 additions & 7 deletions bin/resque-scheduler
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ if (!class_exists('Composer\Autoload\ClassLoader', false)) {
$REDIS_BACKEND = getenv('REDIS_BACKEND');
$REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
if(!empty($REDIS_BACKEND)) {
if (empty($REDIS_BACKEND_DB))
Resque::setBackend($REDIS_BACKEND);
if (empty($REDIS_BACKEND_DB))
\Resque\Resque::setBackend($REDIS_BACKEND);
else
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
\Resque\Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
}

// Set log level for resque-scheduler
Expand All @@ -40,10 +40,10 @@ $LOGGING = getenv('LOGGING');
$VERBOSE = getenv('VERBOSE');
$VVERBOSE = getenv('VVERBOSE');
if(!empty($LOGGING) || !empty($VERBOSE)) {
$logLevel = ResqueScheduler_Worker::LOG_NORMAL;
$logLevel = \Resque\Worker\SchedulerWorker::LOG_NORMAL;
}
else if(!empty($VVERBOSE)) {
$logLevel = ResqueScheduler_Worker::LOG_VERBOSE;
$logLevel = \Resque\Worker\SchedulerWorker::LOG_VERBOSE;
}

// Check for jobs every $interval seconds
Expand All @@ -66,10 +66,10 @@ if($APP_INCLUDE) {
$PREFIX = getenv('PREFIX');
if(!empty($PREFIX)) {
fwrite(STDOUT, '*** Prefix set to '.$PREFIX."\n");
Resque_Redis::prefix($PREFIX);
\Resque\Redis::prefix($PREFIX);
}

$worker = new ResqueScheduler_Worker();
$worker = new \Resque\Worker\SchedulerWorker();
$worker->logLevel = $logLevel;

$PIDFILE = getenv('PIDFILE');
Expand Down
22 changes: 13 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@
"bin/resque-scheduler"
],
"autoload": {
"psr-0": {
"Resque": "lib",
"ResqueScheduler": "lib"
"psr-4": {
"Resque\\": "lib"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
},
"autoload-dev": {
"psr-4": {
"Resque\\Tests\\": "test/Resque/Tests"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
4 changes: 2 additions & 2 deletions demo/bad_job.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class Bad_PHP_Job
{
public function perform()
{
throw new Exception('Unable to run this job!');
throw new \Exception('Unable to run this job!');
}
}
}
10 changes: 5 additions & 5 deletions demo/check_status.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
require __DIR__ . '/init.php';

date_default_timezone_set('GMT');
Resque::setBackend('127.0.0.1:6379');
\Resque\Resque::setBackend('127.0.0.1:6379');
// You can also use a DSN-style format:
//Resque::setBackend('redis://user:[email protected]:6379');
//Resque::setBackend('redis://user:[email protected]:3432/2');
//\Resque\Resque::setBackend('redis://user:[email protected]:6379');
//\Resque\Resque::setBackend('redis://user:[email protected]:3432/2');

$status = new Resque_Job_Status($argv[1]);
$status = new \Resque\Job\Status($argv[1]);
if(!$status->isTracking()) {
die("Resque is not tracking the status of this job.\n");
}
Expand All @@ -20,4 +20,4 @@
while(true) {
fwrite(STDOUT, "Status of ".$argv[1]." is: ".$status->get()."\n");
sleep(1);
}
}
6 changes: 3 additions & 3 deletions demo/queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

require __DIR__ . '/init.php';
date_default_timezone_set('GMT');
Resque::setBackend('127.0.0.1:6379');
\Resque\Resque::setBackend('127.0.0.1:6379');

// You can also use a DSN-style format:
//Resque::setBackend('redis://user:[email protected]:6379');
Expand All @@ -18,9 +18,9 @@
),
);
if (empty($argv[2])) {
$jobId = Resque::enqueue('default', $argv[1], $args, true);
$jobId = \Resque\Resque::enqueue('default', $argv[1], $args, true);
} else {
$jobId = Resque::enqueue($argv[1], $argv[2], $args, true);
$jobId = \Resque\Resque::enqueue($argv[1], $argv[2], $args, true);
}

echo "Queued job ".$jobId."\n\n";
4 changes: 3 additions & 1 deletion lib/Resque/Event.php → lib/Event.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Resque;

/**
* Resque event/plugin system class
*
* @package Resque/Event
* @author Chris Boulton <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Event
class Event
{
/**
* @var array Array containing all registered callbacks, indexked by event name.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

namespace Resque\Exceptions;

use \RuntimeException;

/**
* Runtime exception class for a job that does not exit cleanly.
*
* @package Resque/Job
* @author Chris Boulton <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Job_DirtyExitException extends RuntimeException
class DirtyExitException extends RuntimeException
{
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

namespace Resque\Exceptions;

use \Exception as CoreException;

/**
* Exception to be thrown if while enqueuing a job it should not be created.
*
* @package Resque/Job
* @author Chris Boulton <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Job_DontCreate extends Exception
class DoNotCreateException extends CoreException
{
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

namespace Resque\Exceptions;

use \Exception as CoreException;

/**
* Exception to be thrown if a job should not be performed/run.
*
* @package Resque/Job
* @author Chris Boulton <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Job_DontPerform extends Exception
class DoNotPerformException extends CoreException
{
}
6 changes: 5 additions & 1 deletion lib/Resque/Exception.php → lib/Exceptions/Exception.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php

namespace Resque\Exceptions;

use \Exception as CoreException;

/**
* Resque exception.
*
* @package Resque
* @author Chris Boulton <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_Exception extends Exception
class Exception extends CoreException
{
}
15 changes: 15 additions & 0 deletions lib/Exceptions/InvalidTimestampException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Resque\Exceptions;

/**
* Exception thrown whenever an invalid timestamp has been passed to a job.
*
* @package ResqueScheduler
* @author Chris Boulton <[email protected]>
* @copyright (c) 2012 Chris Boulton
* @license http://www.opensource.org/licenses/mit-license.php
*/
class InvalidTimestampException extends Exception
{
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

namespace Resque\Exceptions;

/**
* Redis related exceptions
*
* @package Resque
* @author Chris Boulton <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Resque_RedisException extends Resque_Exception
class RedisException extends Exception
{
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php

namespace Resque\Failure;

/**
* Interface that all failure backends should implement.
*
* @package Resque/Failure
* @author Chris Boulton <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php
*/
interface Resque_Failure_Interface
interface FailureInterface
{
/**
* Initialize a failed job class and save it (where appropriate).
*
* @param object $payload Object containing details of the failed job.
* @param object $exception Instance of the exception that was thrown by the failed job.
* @param object $worker Instance of Resque_Worker that received the job.
* @param object $worker Instance of \Resque\Worker\ResqueWorker that received the job.
* @param string $queue The name of the queue the job was fetched from.
*/
public function __construct($payload, $exception, $worker, $queue);
Expand Down
Loading

0 comments on commit 77da6db

Please sign in to comment.