diff --git a/README.md b/README.md index 032d5ce3d..0749eeb35 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,31 @@ If you want to use Laravel's native Auth functionality, register this included s This service provider will slightly modify the internal DatabaseReminderRepository to add support for MongoDB based password reminders. If you don't use password reminders, you don't have to register this service provider and everything else should work just fine. + +### Queues + +If you want to use MongoDB as your database backend, change the the driver in `config/queue.php`: + +```php +'connections' => [ + 'database' => [ + 'driver' => 'mongodb', + 'table' => 'jobs', + 'queue' => 'default', + 'expire' => 60, + ], +] +``` + +To log failed queue jobs to MongoDB, override `failed` in `config/queue.php` (appears at the end of `queue.php`): + +```php +'failed' => [ + 'database' => 'mongodb', + 'collection' => 'failed_jobs', +] +``` + ### 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/MongodbServiceProvider.php b/src/Jenssegers/Mongodb/MongodbServiceProvider.php index 426440e5f..5e4e0eb4c 100644 --- a/src/Jenssegers/Mongodb/MongodbServiceProvider.php +++ b/src/Jenssegers/Mongodb/MongodbServiceProvider.php @@ -1,7 +1,9 @@ app['db']); }); }); + + // Support queue failer to record failed jobs + $this->app->singleton('queue.failer', function ($app) { + $config = $app['config']['queue.failed']; + + if (isset($config['collection'])) { + return new DatabaseFailedJobProvider($app['db'], $config['database'], $config['collection']); + } else { + return new NullFailedJobProvider; + } + }); } } diff --git a/src/Jenssegers/Mongodb/Queue/Failed/DatabaseFailedJobProvider.php b/src/Jenssegers/Mongodb/Queue/Failed/DatabaseFailedJobProvider.php new file mode 100644 index 000000000..8bdd2f6cb --- /dev/null +++ b/src/Jenssegers/Mongodb/Queue/Failed/DatabaseFailedJobProvider.php @@ -0,0 +1,120 @@ +collection = $collection; + $this->resolver = $resolver; + $this->database = $database; + } + + /** + * Log a failed job into storage. + * + * @param string $connection + * @param string $queue + * @param string $payload + * @return void + */ + public function log($connection, $queue, $payload) { + $failed_at = Carbon::now(); + $this->getCollection()->insert(compact('connection', 'queue', 'payload', 'failed_at')); + } + + /** + * Get a list of all of the failed jobs. + * + * @return array + */ + public function all() { + $fails = $this->getCollection()->get(); + $jobs = []; + foreach ($fails as $fail) { + $fail = ['id' => (string) $fail['_id']] + $fail; + unset($fail['_id']); + $failed = new Carbon($fail['failed_at']['date'], $fail['failed_at']['timezone']); + $fail['failed_at'] = (string) $failed; + $jobs[] = $fail; + } + return $jobs; + } + + /** + * Get a single failed job. + * + * @param mixed $id + * @return array + */ + public function find($id) { + $fail = $this->getCollection()->find($id); + if ($fail) { + $fail = ['id' => (string) $fail['_id']] + $fail; + unset($fail['_id']); + return (object) $fail; + } + return $fail; + } + + /** + * Delete a single failed job from storage. + * + * @param mixed $id + * @return bool + */ + public function forget($id) { + return $this->getCollection()->delete($id) > 0; + } + + /** + * Flush all of the failed jobs from storage. + * + * @return void + */ + public function flush() { + $this->getCollection()->truncate(); + } + + /** + * Get a new query builder instance for the table. + * + * @return \Jenssegers\Mongodb\Query\Builder + */ + protected function getCollection() { + return $this->resolver->connection($this->database)->collection($this->collection); + } +}