Skip to content

Commit 4e6cc22

Browse files
Bodom78jenssegers
authored andcommitted
Fixes #789 (#792)
* Fixes #789 * StyleCI fixes * StyleCI fixes * StyleCI fixes
1 parent 87d8eed commit 4e6cc22

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,21 @@ If you want to use MongoDB as your database backend, change the the driver in `c
262262
],
263263
```
264264

265+
If you want to use MongoDB to handle failed jobs, change the database in `config/queue.php`:
266+
267+
```php
268+
'failed' => [
269+
'database' => 'mongodb',
270+
'table' => 'failed_jobs',
271+
],
272+
```
273+
274+
And add the service provider in `config/app.php`:
275+
276+
```php
277+
Jenssegers\Mongodb\MongodbQueueServiceProvider::class,
278+
```
279+
265280
### Sentry
266281

267282
If you want to use this library with [Sentry](https://cartalyst.com/manual/sentry), then check out https://github.com/jenssegers/Laravel-MongoDB-Sentry
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php namespace Jenssegers\Mongodb;
2+
3+
use Illuminate\Queue\QueueServiceProvider;
4+
use Jenssegers\Mongodb\Queue\Failed\MongoFailedJobProvider;
5+
6+
class MongodbQueueServiceProvider extends QueueServiceProvider
7+
{
8+
/**
9+
* Register the failed job services.
10+
*
11+
* @return void
12+
*/
13+
protected function registerFailedJobServices()
14+
{
15+
// Add compatible queue failer if mongodb is configured.
16+
if (config('queue.failed.database') == 'mongodb') {
17+
$this->app->singleton('queue.failer', function ($app) {
18+
return new MongoFailedJobProvider($app['db'], config('queue.failed.database'), config('queue.failed.table'));
19+
});
20+
} else {
21+
parent::registerFailedJobServices();
22+
}
23+
}
24+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php namespace Jenssegers\Mongodb\Queue\Failed;
2+
3+
use Carbon\Carbon;
4+
use Illuminate\Queue\Failed\DatabaseFailedJobProvider;
5+
6+
class MongoFailedJobProvider extends DatabaseFailedJobProvider
7+
{
8+
/**
9+
* Log a failed job into storage.
10+
*
11+
* @param string $connection
12+
* @param string $queue
13+
* @param string $payload
14+
*
15+
* @return void
16+
*/
17+
public function log($connection, $queue, $payload)
18+
{
19+
$failed_at = Carbon::now()->getTimestamp();
20+
21+
$this->getTable()->insert(compact('connection', 'queue', 'payload', 'failed_at'));
22+
}
23+
24+
/**
25+
* Get a list of all of the failed jobs.
26+
*
27+
* @return array
28+
*/
29+
public function all()
30+
{
31+
$all = $this->getTable()->orderBy('_id', 'desc')->get();
32+
33+
$all = array_map(function ($job) {
34+
$job['id'] = (string) $job['_id'];
35+
return $job;
36+
}, $all);
37+
38+
return $all;
39+
}
40+
41+
/**
42+
* Get a single failed job.
43+
*
44+
* @param mixed $id
45+
* @return array
46+
*/
47+
public function find($id)
48+
{
49+
$job = $this->getTable()->find($id);
50+
51+
$job['id'] = (string) $job['_id'];
52+
53+
return $job;
54+
}
55+
56+
/**
57+
* Delete a single failed job from storage.
58+
*
59+
* @param mixed $id
60+
* @return bool
61+
*/
62+
public function forget($id)
63+
{
64+
return $this->getTable()->where('_id', $id)->delete() > 0;
65+
}
66+
}

0 commit comments

Comments
 (0)