diff --git a/src/WP_Queue/Cron.php b/src/WP_Queue/Cron.php index a006deb..fe47e8c 100644 --- a/src/WP_Queue/Cron.php +++ b/src/WP_Queue/Cron.php @@ -7,13 +7,18 @@ class Cron { /** * @var string */ - protected $identifier; + protected $id; /** * @var Worker */ protected $worker; + /** + * @var int + */ + protected $interval; + /** * Timestamp of when processing the queue started. * @@ -24,12 +29,14 @@ class Cron { /** * Cron constructor. * - * @param string $identifier + * @param string $id * @param Worker $worker + * @param int $interval */ - public function __construct( $identifier, $worker ) { - $this->identifier = $identifier; - $this->worker = $worker; + public function __construct( $id, $worker, $interval ) { + $this->id = $id; + $this->worker = $worker; + $this->interval = $interval; } /** @@ -37,7 +44,7 @@ public function __construct( $identifier, $worker ) { * * @return bool */ - public function is_enabled() { + protected function is_enabled() { if ( defined( 'DISABLE_WP_QUEUE_CRON' ) && DISABLE_WP_QUEUE_CRON ) { return false; } @@ -47,30 +54,36 @@ public function is_enabled() { /** * Init cron class. + * + * @return bool */ public function init() { + if ( ! $this->is_enabled() ) { + return false; + } + add_filter( 'cron_schedules', array( $this, 'schedule_cron' ) ); - add_action( "wp_queue_worker_{$this->identifier}", array( $this, 'cron_worker' ) ); + add_action( "wp_queue_worker_{$this->id}", array( $this, 'cron_worker' ) ); - if ( ! wp_next_scheduled( "wp_queue_worker_{$this->identifier}" ) ) { + if ( ! wp_next_scheduled( "wp_queue_worker_{$this->id}" ) ) { // Schedule health check - wp_schedule_event( time(), 'wp_queue_cron_interval', "wp_queue_worker_{$this->identifier}" ); + wp_schedule_event( time(), 'wp_queue_cron_interval', "wp_queue_worker_{$this->id}" ); } + + return true; } /** - * Add 5 minutes to cron schedules. + * Add interval to cron schedules. * * @param array $schedules * * @return array */ public function schedule_cron( $schedules ) { - $interval = apply_filters( 'wp_queue_cron_interval', 5 ); - - $schedules['wp_queue_cron_interval'] = array( - 'interval' => MINUTE_IN_SECONDS * $interval, - 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), + $schedules["wp_queue_cron_interval_{$this->id}"] = array( + 'interval' => MINUTE_IN_SECONDS * $this->interval, + 'display' => sprintf( __( 'Every %d Minutes' ), $this->interval ), ); return $schedules; @@ -121,7 +134,7 @@ protected function get_memory_limit() { $memory_limit = '256M'; } - if ( ! $memory_limit || -1 == $memory_limit ) { + if ( ! $memory_limit || - 1 == $memory_limit ) { // Unlimited, set to 1GB $memory_limit = '1000M'; } diff --git a/src/WP_Queue/Queue.php b/src/WP_Queue/Queue.php index 5385c77..636df67 100644 --- a/src/WP_Queue/Queue.php +++ b/src/WP_Queue/Queue.php @@ -12,19 +12,17 @@ class Queue { protected $connection; /** - * @var string + * @var Cron */ - protected $identifier; + protected $cron; /** * Queue constructor. * * @param ConnectionInterface $connection - * @param string $identifier */ - public function __construct( ConnectionInterface $connection, $identifier ) { + public function __construct( ConnectionInterface $connection ) { $this->connection = $connection; - $this->identifier = $identifier; } /** @@ -43,13 +41,17 @@ public function push( Job $job, $delay = 0 ) { * Create a cron worker. * * @param int $attempts + * @param int $interval + * + * @return Cron */ - public function cron( $attempts = 3 ) { - $cron = new Cron( $this->identifier, $this->worker( $attempts ) ); - - if ( $cron->is_enabled() ) { - $cron->init(); + public function cron( $attempts = 3, $interval = 5 ) { + if ( is_null( $this->cron ) ) { + $this->cron = new Cron( get_class( $this->connection ), $this->worker( $attempts ), $interval ); + $this->cron->init(); } + + return $this->cron; } /** diff --git a/src/WP_Queue/QueueManager.php b/src/WP_Queue/QueueManager.php index 2f9e2ac..bf23ce7 100644 --- a/src/WP_Queue/QueueManager.php +++ b/src/WP_Queue/QueueManager.php @@ -43,7 +43,7 @@ protected static function build( $connection ) { throw new ConnectionNotFoundException(); } - static::$instances[ $connection ] = new Queue( $connections[ $connection ], $connection ); + static::$instances[ $connection ] = new Queue( $connections[ $connection ] ); return static::$instances[ $connection ]; } diff --git a/tests/TestDatabaseConnection.php b/tests/TestDatabaseConnection.php index 35cfb3e..f2d9607 100644 --- a/tests/TestDatabaseConnection.php +++ b/tests/TestDatabaseConnection.php @@ -132,6 +132,8 @@ public function test_failed_jobs() { } } -class TestJob extends Job { - public function handle() {} +if ( ! class_exists( 'TestJob' ) ) { + class TestJob extends Job { + public function handle() {} + } } \ No newline at end of file diff --git a/tests/TestQueue.php b/tests/TestQueue.php new file mode 100644 index 0000000..c36134c --- /dev/null +++ b/tests/TestQueue.php @@ -0,0 +1,62 @@ +shouldReceive( 'push' )->once()->andReturn( $insert_id ); + + $queue = new Queue( $connection ); + + $this->assertEquals( $insert_id, $queue->push( new TestJob() ) ); + } + + public function test_push_fail() { + $connection = Mockery::mock( ConnectionInterface::class ); + $connection->shouldReceive( 'push' )->once()->andReturn( false ); + + $queue = new Queue( $connection ); + + $this->assertFalse( $queue->push( new TestJob() ) ); + } + + public function test_cron() { + $connection = Mockery::mock( ConnectionInterface::class ); + $queue = new Queue( $connection ); + + WP_Mock::userFunction( 'wp_next_scheduled', array( + 'return' => time(), + ) ); + + $this->assertInstanceOf( Cron::class, $queue->cron() ); + } + + public function test_worker() { + $connection = Mockery::mock( ConnectionInterface::class ); + $queue = new Queue( $connection ); + + $this->assertInstanceOf( Worker::class, $queue->worker( 3 ) ); + } +} + +if ( ! class_exists( 'TestJob' ) ) { + class TestJob extends Job { + public function handle() {} + } +} \ No newline at end of file