diff --git a/README.md b/README.md index ea94aa43c..1a7f53c0a 100644 --- a/README.md +++ b/README.md @@ -262,6 +262,21 @@ If you want to use MongoDB as your database backend, change the the driver in `c ], ``` +If you want to use MongoDB to handle failed jobs, change the database in `config/queue.php`: + +```php +'failed' => [ + 'database' => 'mongodb', + 'table' => 'failed_jobs', + ], +``` + +And add the service provider in `config/app.php`: + +```php +Jenssegers\Mongodb\MongodbQueueServiceProvider::class, +``` + ### Sentry If you want to use this library with [Sentry](https://cartalyst.com/manual/sentry), then check out https://github.com/jenssegers/Laravel-MongoDB-Sentry diff --git a/src/Jenssegers/Mongodb/MongodbQueueServiceProvider.php b/src/Jenssegers/Mongodb/MongodbQueueServiceProvider.php new file mode 100644 index 000000000..8321992e4 --- /dev/null +++ b/src/Jenssegers/Mongodb/MongodbQueueServiceProvider.php @@ -0,0 +1,24 @@ +app->singleton('queue.failer', function ($app) { + return new MongoFailedJobProvider($app['db'], config('queue.failed.database'), config('queue.failed.table')); + }); + } else { + parent::registerFailedJobServices(); + } + } +} diff --git a/src/Jenssegers/Mongodb/Queue/Failed/MongoFailedJobProvider.php b/src/Jenssegers/Mongodb/Queue/Failed/MongoFailedJobProvider.php new file mode 100644 index 000000000..4234f885d --- /dev/null +++ b/src/Jenssegers/Mongodb/Queue/Failed/MongoFailedJobProvider.php @@ -0,0 +1,66 @@ +getTimestamp(); + + $this->getTable()->insert(compact('connection', 'queue', 'payload', 'failed_at')); + } + + /** + * Get a list of all of the failed jobs. + * + * @return array + */ + public function all() + { + $all = $this->getTable()->orderBy('_id', 'desc')->get(); + + $all = array_map(function ($job) { + $job['id'] = (string) $job['_id']; + return $job; + }, $all); + + return $all; + } + + /** + * Get a single failed job. + * + * @param mixed $id + * @return array + */ + public function find($id) + { + $job = $this->getTable()->find($id); + + $job['id'] = (string) $job['_id']; + + return $job; + } + + /** + * Delete a single failed job from storage. + * + * @param mixed $id + * @return bool + */ + public function forget($id) + { + return $this->getTable()->where('_id', $id)->delete() > 0; + } +}