Skip to content

Commit

Permalink
Refactoring queue commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 30, 2016
1 parent a82a25f commit 44c5a36
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 45 deletions.
37 changes: 21 additions & 16 deletions src/Illuminate/Queue/Console/ListFailedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ class ListFailedCommand extends Command
*/
public function fire()
{
$jobs = $this->getFailedJobs();

if (count($jobs) == 0) {
if (count($jobs = $this->getFailedJobs()) == 0) {
return $this->info('No failed jobs!');
}

Expand All @@ -51,13 +49,11 @@ public function fire()
*/
protected function getFailedJobs()
{
$results = [];

foreach ($this->laravel['queue.failer']->all() as $failed) {
$results[] = $this->parseFailedJob((array) $failed);
}
$failed = $this->laravel['queue.failer']->all();

return array_filter($results);
return collect($failed)->map(function ($failed) {
return $this->parseFailedJob((array) $failed);
})->filter()->all();
}

/**
Expand Down Expand Up @@ -87,16 +83,25 @@ private function extractJobName($payload)

if ($payload && (! isset($payload['data']['command']))) {
return Arr::get($payload, 'job');
} elseif ($payload && isset($payload['data']['command'])) {
return $this->matchJobName($payload);
}
}

if ($payload && isset($payload['data']['command'])) {
preg_match('/"([^"]+)"/', $payload['data']['command'], $matches);
/**
* Match the job name from the payload.
*
* @param array $payload
* @return string
*/
protected function matchJobName($payload)
{
preg_match('/"([^"]+)"/', $payload['data']['command'], $matches);

if (isset($matches[1])) {
return $matches[1];
} else {
return Arr::get($payload, 'job');
}
if (isset($matches[1])) {
return $matches[1];
} else {
return Arr::get($payload, 'job');
}
}

Expand Down
45 changes: 27 additions & 18 deletions src/Illuminate/Queue/Console/RetryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,30 @@ class RetryCommand extends Command
* @return void
*/
public function fire()
{
foreach ($this->getJobIds() as $id) {
$this->retryJob($id);

$this->info("The failed job [{$id}] has been pushed back onto the queue!");

$this->laravel['queue.failer']->forget($id);
}
}

/**
* Get the job IDs to be retried.
*
* @return array
*/
protected function getJobIds()
{
$ids = $this->argument('id');

if (count($ids) === 1 && $ids[0] === 'all') {
$ids = Arr::pluck($this->laravel['queue.failer']->all(), 'id');
}

foreach ($ids as $id) {
$this->retryJob($id);
}
return $ids;
}

/**
Expand All @@ -48,27 +62,22 @@ public function fire()
*/
protected function retryJob($id)
{
$failed = $this->laravel['queue.failer']->find($id);

if (! is_null($failed)) {
$failed = (object) $failed;

$failed->payload = $this->resetAttempts($failed->payload);

$this->laravel['queue']->connection($failed->connection)
->pushRaw($failed->payload, $failed->queue);
if (is_null($failed = $this->laravel['queue.failer']->find($id))) {
return $this->error("No failed job matches the given ID [{$id}].");
}

$this->laravel['queue.failer']->forget($failed->id);
$failed = (object) $failed;

$this->info("The failed job [{$id}] has been pushed back onto the queue!");
} else {
$this->error("No failed job matches the given ID [{$id}].");
}
$this->laravel['queue']->connection($failed->connection)->pushRaw(
$this->resetAttempts($failed->payload), $failed->queue
);
}

/**
* Reset the payload attempts.
*
* Applicable to Redis jobs which store attempts in their payload.
*
* @param string $payload
* @return string
*/
Expand All @@ -77,7 +86,7 @@ protected function resetAttempts($payload)
$payload = json_decode($payload, true);

if (isset($payload['attempts'])) {
$payload['attempts'] = 1;
$payload['attempts'] = 0;
}

return json_encode($payload);
Expand Down
33 changes: 22 additions & 11 deletions src/Illuminate/Queue/Console/TableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,10 @@ public function fire()
{
$table = $this->laravel['config']['queue.connections.database.table'];

$tableClassName = Str::studly($table);

$fullPath = $this->createBaseMigration($table);

$stub = str_replace(
['{{table}}', '{{tableClassName}}'], [$table, $tableClassName], $this->files->get(__DIR__.'/stubs/jobs.stub')
$this->replaceMigration(
$this->createBaseMigration($table), $table, Str::studly($table)
);

$this->files->put($fullPath, $stub);

$this->info('Migration created successfully!');

$this->composer->dumpAutoloads();
Expand All @@ -82,10 +76,27 @@ public function fire()
*/
protected function createBaseMigration($table = 'jobs')
{
$name = 'create_'.$table.'_table';
return $this->laravel['migration.creator']->create(
'create_'.$table.'_table', $this->laravel->databasePath().'/migrations'
);
}

$path = $this->laravel->databasePath().'/migrations';
/**
* Replace the generated migration with the job table stub.
*
* @param string $path
* @param string $table
* @param string $tableClassName
* @return void
*/
protected function replaceMigration($path, $table, $tableClassName)
{
$stub = str_replace(
['{{table}}', '{{tableClassName}}'],
[$table, $tableClassName],
$this->files->get(__DIR__.'/stubs/jobs.stub')
);

return $this->laravel['migration.creator']->create($name, $path);
$this->files->put($path, $stub);
}
}
1 change: 1 addition & 0 deletions src/Illuminate/Queue/Console/stubs/jobs.stub
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Create{{tableClassName}}Table extends Migration
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');

$table->index(['queue', 'reserved_at']);
});
}
Expand Down

0 comments on commit 44c5a36

Please sign in to comment.