Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #789 #792

Merged
merged 4 commits into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/Jenssegers/Mongodb/MongodbQueueServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php namespace Jenssegers\Mongodb;

use Illuminate\Queue\QueueServiceProvider;
use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider;

class MongodbQueueServiceProvider extends QueueServiceProvider
{
/**
* Register the failed job services.
*
* @return void
*/
protected function registerFailedJobServices()
{
// Add compatible queue failer if mongodb is configured.
if (config('queue.failed.database') == 'mongodb') {
$this->app->singleton('queue.failer', function ($app) {
return new MongoFailedJobProvider($app['db'], config('queue.failed.database'), config('queue.failed.table'));
});
} else {
parent::registerFailedJobServices();
}
}
}
66 changes: 66 additions & 0 deletions src/Jenssegers/Mongodb/Queue/Failed/MongoFailedJobProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php namespace Jenssegers\Mongodb\Queue\Failed;

use Carbon\Carbon;
use Illuminate\Queue\Failed\DatabaseFailedJobProvider;

class MongoFailedJobProvider extends DatabaseFailedJobProvider
{
/**
* 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()->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;
}
}