2.0.0 Add namespacing + PHP8.0 support (#1)

2.0.0 (2021-02-19)

Moved to PSR-4
Namespaced codebase
Added more comments throughout
Co-Authored-By: idanoo <daniel@m2.nz>
Co-Committed-By: idanoo <daniel@m2.nz>
This commit is contained in:
idanoo 2021-02-19 12:23:32 +13:00
parent ebec2f7bf7
commit 80d64e79ff
56 changed files with 2215 additions and 1423 deletions

11
examples/BadPHPJob.php Normal file
View file

@ -0,0 +1,11 @@
<?php
namespace Resque\Example;
class BadPHPJob
{
public function perform()
{
throw new \Exception('Unable to run this job!');
}
}

26
examples/CheckStatus.php Normal file
View file

@ -0,0 +1,26 @@
<?php
namespace Resque\Example;
if (empty($argv[1])) {
die('Specify the ID of a job to monitor the status of.');
}
require __DIR__ . '/init.php';
date_default_timezone_set('GMT');
\Resque\Resque::setBackend('127.0.0.1:6379');
// You can also use a DSN-style format:
//Resque::setBackend('redis://user:pass@127.0.0.1:6379');
//Resque::setBackend('redis://user:pass@a.host.name:3432/2');
$status = new \Resque\Job\Status($argv[1]);
if (!$status->isTracking()) {
die("Resque is not tracking the status of this job.\n");
}
echo "Tracking status of " . $argv[1] . ". Press [break] to stop.\n\n";
while (true) {
fwrite(STDOUT, "Status of " . $argv[1] . " is: " . $status->get() . "\n");
sleep(1);
}

27
examples/Init.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace Resque\Example;
// Find and initialize Composer
// NOTE: You should NOT use this when developing against php-resque.
// The autoload code below is specifically for this demo.
$files = [
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
];
foreach ($files as $file) {
if (file_exists($file)) {
require_once $file;
break;
}
}
if (!class_exists('Composer\Autoload\ClassLoader', false)) {
die(
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL
);
}

11
examples/LongPHPJob.php Normal file
View file

@ -0,0 +1,11 @@
<?php
namespace Resque\Example;
class LongPHPJob
{
public function perform()
{
sleep(600);
}
}

13
examples/PHPErrorJob.php Normal file
View file

@ -0,0 +1,13 @@
<?php
/** @noinspection PhpUndefinedFunctionInspection */
namespace Resque\Example;
class PHPErrorJob
{
public function perform()
{
callToUndefinedFunction();
}
}

13
examples/PHPJob.php Normal file
View file

@ -0,0 +1,13 @@
<?php
namespace Resque\Example;
class PHPJob
{
public function perform()
{
fwrite(STDOUT, 'Start job! -> ');
sleep(1);
fwrite(STDOUT, 'Job ended!' . PHP_EOL);
}
}

View file

@ -0,0 +1,52 @@
<?php
namespace Resque\Example;
// Somewhere in our application, we need to register:
// \Resque\Event::listen('afterEnqueue', ['My_Resque_Plugin', 'afterEnqueue']);
// \Resque\Event::listen('beforeFirstFork', ['My_Resque_Plugin', 'beforeFirstFork']);
// \Resque\Event::listen('beforeFork', ['My_Resque_Plugin', 'beforeFork']);
// \Resque\Event::listen('afterFork', ['My_Resque_Plugin', 'afterFork']);
// \Resque\Event::listen('beforePerform', ['My_Resque_Plugin', 'beforePerform']);
// \Resque\Event::listen('afterPerform', ['My_Resque_Plugin', 'afterPerform']);
// \Resque\Event::listen('onFailure', ['My_Resque_Plugin', 'onFailure']);
class SampleResquePlugin
{
public static function afterEnqueue($class, $arguments)
{
echo "Job was queued for " . $class . ". Arguments:";
print_r($arguments);
}
public static function beforeFirstFork($worker)
{
echo "Worker started. Listening on queues: " . implode(', ', $worker->queues(false)) . "\n";
}
public static function beforeFork($job)
{
echo "Just about to fork to run " . $job;
}
public static function afterFork($job)
{
echo "Forked to run " . $job . ". This is the child process.\n";
}
public static function beforePerform($job)
{
echo "Cancelling " . $job . "\n";
// throw new Resque_Job_DontPerform;
}
public static function afterPerform($job)
{
echo "Just performed " . $job . "\n";
}
public static function onFailure($exception, $job)
{
echo $job . " threw an exception:\n" . $exception;
}
}

29
examples/queue.php Normal file
View file

@ -0,0 +1,29 @@
<?php
namespace Resque\Example;
if (empty($argv[1])) {
die('Specify the name of a job to add. e.g, php queue.php PHPJob');
}
require __DIR__ . '/init.php';
date_default_timezone_set('GMT');
\Resque\Resque::setBackend('127.0.0.1:6379');
// You can also use a DSN-style format:
//Resque::setBackend('redis://user:pass@127.0.0.1:6379');
//Resque::setBackend('redis://user:pass@a.host.name:3432/2');
$args = [
'time' => time(),
'array' => [
'test' => 'test',
],
];
if (empty($argv[2])) {
$jobId = \Resque\Resque::enqueue('default', $argv[1], $args, true);
} else {
$jobId = \Resque\Resque::enqueue($argv[1], $argv[2], $args, true);
}
echo "Queued job " . $jobId . "\n\n";

10
examples/resque.logrotate Normal file
View file

@ -0,0 +1,10 @@
/var/log/resque/*.log {
daily
missingok
rotate 7
compress
compressoptions -4
notifempty
create 640 root www-data
copytruncate
}