Skip to content

Commit

Permalink
Sync connection added
Browse files Browse the repository at this point in the history
  • Loading branch information
A5hleyRich committed Apr 20, 2018
1 parent 395ece9 commit c84587c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ You can also specify the number of times a job should be attempted before being
wp_queue()->cron( 3 );
```

## Local Development

When developing locally you may want jobs processed instantly, instead of them being pushed to the queue. This can be useful for debugging jobs via Xdebug. Add the following filter to use the `sync` connection:

```
add_filter( ‘wp_queue_default_connection’, function() {
return ‘sync’;
} );
```

## License

WP Queue is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
83 changes: 83 additions & 0 deletions src/WP_Queue/Connections/SyncConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace WP_Queue\Connections;

use Exception;
use WP_Queue\Job;

class SyncConnection implements ConnectionInterface {
/**
* Push a job onto the queue.
*
* @param Job $job
* @param int $delay
*
* @return bool|int
*/
public function push(Job $job, $delay = 0) {
$job->handle();

return true;
}

/**
* Retrieve a job from the queue.
*
* @return bool|Job
*/
public function pop() {
return false;
}

/**
* Delete a job from the queue.
*
* @param Job $job
*
* @return bool
*/
public function delete($job) {
return false;
}

/**
* Release a job back onto the queue.
*
* @param Job $job
*
* @return bool
*/
public function release($job) {
return false;
}

/**
* Push a job onto the failure queue.
*
* @param Job $job
* @param Exception $exception
*
* @return bool
*/
public function failure($job, Exception $exception) {
return false;
}

/**
* Get total jobs in the queue.
*
* @return int
*/
public function jobs() {
return 0;
}

/**
* Get total jobs in the failures queue.
*
* @return int
*/
public function failed_jobs() {
return 0;
}
}
4 changes: 4 additions & 0 deletions src/WP_Queue/QueueManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use WP_Queue\Connections\DatabaseConnection;
use WP_Queue\Connections\RedisConnection;
use WP_Queue\Connections\SyncConnection;
use WP_Queue\Exceptions\ConnectionNotFoundException;

class QueueManager {
Expand All @@ -21,6 +22,8 @@ class QueueManager {
* @return Queue
*/
public static function resolve( $connection ) {
$connection = apply_filters( 'wp_queue_default_connection', $connection );

if ( isset( static::$instances[ $connection ] ) ) {
return static::$instances[ $connection ];
}
Expand Down Expand Up @@ -57,6 +60,7 @@ protected static function connections() {
$connections = array(
'database' => new DatabaseConnection( $GLOBALS['wpdb'] ),
'redis' => new RedisConnection(),
'sync' => new SyncConnection(),
);

return apply_filters( 'wp_queue_connections', $connections );
Expand Down

0 comments on commit c84587c

Please sign in to comment.