Skip to content

Commit

Permalink
Merge 73ae83b into 3c20c17
Browse files Browse the repository at this point in the history
  • Loading branch information
adurolms committed Feb 16, 2016
2 parents 3c20c17 + 73ae83b commit 1648aa8
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/Jenssegers/Mongodb/MongodbServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php namespace Jenssegers\Mongodb;

use Illuminate\Support\ServiceProvider;
use Illuminate\Queue\Failed\NullFailedJobProvider;
use Jenssegers\Mongodb\Queue\MongoConnector;
use Jenssegers\Mongodb\Queue\Failed\DatabaseFailedJobProvider;

class MongodbServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -33,5 +35,16 @@ public function register()
return new MongoConnector($this->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;
}
});
}
}
120 changes: 120 additions & 0 deletions src/Jenssegers/Mongodb/Queue/Failed/DatabaseFailedJobProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Jenssegers\Mongodb\Queue\Failed;

use Carbon\Carbon;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Queue\Failed\FailedJobProviderInterface;

class DatabaseFailedJobProvider implements FailedJobProviderInterface {

/**
* The connection resolver implementation.
*
* @var \Illuminate\Database\ConnectionResolverInterface
*/
protected $resolver;

/**
* The database connection name.
*
* @var string
*/
protected $database;

/**
* The database table.
*
* @var string
*/
protected $collection;

/**
* Create a new database failed job provider.
*
* @param \Illuminate\Database\ConnectionResolverInterface $resolver
* @param string $database
* @param string $collection
* @return void
*/
public function __construct(ConnectionResolverInterface $resolver, $database, $collection) {
$this->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);
}
}

0 comments on commit 1648aa8

Please sign in to comment.