Skip to content

Commit

Permalink
First version of command is done
Browse files Browse the repository at this point in the history
  • Loading branch information
luisdalmolin committed Jul 3, 2020
1 parent d589c44 commit 7ae2e39
Show file tree
Hide file tree
Showing 14 changed files with 343 additions and 118 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
],
"require": {
"php": "^7.1",
"illuminate/support": "^6.0"
"illuminate/support": "^6.0|^7.0"
},
"require-dev": {
"orchestra/testbench": "^4.0",
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {
"KirschbaumDevelopment\\LaravelQueueBatchRetry\\": "src"
"KirschbaumDevelopment\\BatchRetry\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"KirschbaumDevelopment\\LaravelQueueBatchRetry\\Tests\\": "tests"
"KirschbaumDevelopment\\BatchRetry\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -44,10 +44,10 @@
"extra": {
"laravel": {
"providers": [
"KirschbaumDevelopment\\LaravelQueueBatchRetry\\LaravelQueueBatchRetryServiceProvider"
"KirschbaumDevelopment\\BatchRetry\\BatchRetryServiceProvider"
],
"aliases": {
"LaravelQueueBatchRetry": "KirschbaumDevelopment\\LaravelQueueBatchRetry\\LaravelQueueBatchRetryFacade"
"BatchRetry": "KirschbaumDevelopment\\BatchRetry\\BatchRetryFacade"
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>

<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="QUEUE_CONNECTION" value="database"/>
</php>
</phpunit>
33 changes: 33 additions & 0 deletions src/BatchRetryServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace KirschbaumDevelopment\BatchRetry;

use Illuminate\Support\ServiceProvider;
use KirschbaumDevelopment\BatchRetry\Commands\BatchRetryCommand;

class BatchRetryServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('laravel-queue-batch-retry.php'),
], 'config');

$this->commands([
BatchRetryCommand::class,
]);
}
}

/**
* Register the application services.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'laravel-queue-batch-retry');
}
}
67 changes: 67 additions & 0 deletions src/Commands/BatchRetryCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace KirschbaumDevelopment\BatchRetry\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use KirschbaumDevelopment\BatchRetry\FailedJob;

class BatchRetryCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'queue:batch-retry
{--failed-after= : Only batch retry jobs failed after some specific date}
{--limit= : Limit the amount of jobs to retry}
{--queue= : Only retry on a specific queue}
{--filter= : Filter by a specific string. This will be a search in the payload of the job}
{--dry-run : Do a dry run of the batch retry to have an idea of the size of the batch}';

/**
* The console command description.
*
* @var string
*/
protected $description = '';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$failedJobs = FailedJob::query()
->when($this->option('queue'), function ($query) {
$query->where('queue', $this->option('queue'));
})
->when($this->option('failed-after'), function ($query) {
// dd(Carbon::parse($this->option('failed-after')));
$query->where(
'failed_at',
'>=',
Carbon::parse($this->option('failed-after'))->format('Y-m-d H:i:s')
);
})
->when($this->option('filter'), function ($query) {
$query->where('payload', 'like', '%'.$this->option('filter').'%');
});

if ($this->option('limit')) {
$failedJobs->take($this->option('limit'))
->get()
->each(function (FailedJob $failedJob) {
$this->call('queue:retry', ['id' => $failedJob->id]);
});
} else {
$failedJobs->chunkById(50, function ($jobsChunk) {
$jobsChunk->each(function (FailedJob $failedJob) {
$this->call('queue:retry', ['id' => $failedJob->id]);
});
});
}
}
}
19 changes: 19 additions & 0 deletions src/FailedJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace KirschbaumDevelopment\BatchRetry;

use Illuminate\Database\Eloquent\Model;

class FailedJob extends Model
{
protected $guarded = [];
protected $table = 'failed_jobs';
public $timestamps = false;

protected $casts = ['failed_at' => 'datetime'];

public function setPayloadAttribute($payload)
{
$this->attributes['payload'] = serialize($payload);
}
}
8 changes: 0 additions & 8 deletions src/LaravelQueueBatchRetry.php

This file was deleted.

21 changes: 0 additions & 21 deletions src/LaravelQueueBatchRetryFacade.php

This file was deleted.

60 changes: 0 additions & 60 deletions src/LaravelQueueBatchRetryServiceProvider.php

This file was deleted.

106 changes: 106 additions & 0 deletions tests/BatchRetryCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace KirschbaumDevelopment\BatchRetry\Tests;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Artisan;
use KirschbaumDevelopment\BatchRetry\FailedJob;

class BatchRetryCommandTest extends TestCase
{
/** @test */
public function it_can_retry_all_jobs()
{
$failedJobs = factory(FailedJob::class, 2)->create();

Artisan::call('queue:batch-retry');

$failedJobs->each(function ($failedJob) {
$this->assertEquals(2, DB::table('jobs')->count());
$this->assertNull($failedJob->fresh());
});
}

/** @test */
public function making_sure_the_chunks_work()
{
$failedJobs = factory(FailedJob::class, 75)->create();

Artisan::call('queue:batch-retry');

$this->assertEquals(75, DB::table('jobs')->count());
$this->assertEquals(0, FailedJob::count());
}

/** @test */
public function it_can_batch_retry_filtering_by_queue()
{
$failedJobDefault = factory(FailedJob::class)->create();
$failedJobPriority = factory(FailedJob::class)->create([
'queue' => 'priority',
]);

Artisan::call('queue:batch-retry', [
'--queue' => 'priority',
]);

$this->assertEquals(1, DB::table('jobs')->count());
$this->assertNull($failedJobPriority->fresh());
$this->assertNotNull($failedJobDefault->fresh());
}

/** @test */
public function it_can_batch_retry_with_limit_clause()
{
$failedJobsThatShouldRetry = factory(FailedJob::class, 2)->create();
$failedJobsThatShouldNotRetry = factory(FailedJob::class, 2)->create();

Artisan::call('queue:batch-retry', [
'--limit' => 2,
]);

$this->assertEquals(2, DB::table('jobs')->count());

$failedJobsThatShouldRetry->each(function ($failedJob) {
$this->assertNull($failedJob->fresh());
});

$failedJobsThatShouldNotRetry->each(function ($failedJob) {
$this->assertNotNull($failedJob->fresh());
});
}

/** @test */
public function it_can_batch_retry_using_search()
{
$someFailedJob = factory(FailedJob::class)->create([
'payload' => ['displayName' => 'App\Jobs\SomeJob']
]);
$someOtherFailedJob = factory(FailedJob::class)->create([
'payload' => ['displayName' => 'App\Jobs\SomeOtherJob']
]);

Artisan::call('queue:batch-retry', [
'--filter' => 'SomeJob',
]);

$this->assertEquals(1, DB::table('jobs')->count());
$this->assertNull($someFailedJob->fresh());
$this->assertNotNull($someOtherFailedJob->fresh());
}

/** @test */
public function it_can_batch_retry_limiting_by_date()
{
$newJobs = factory(FailedJob::class, 5)->create(['failed_at' => now()]);
$oldJobs = factory(FailedJob::class, 5)->create(['failed_at' => now()->subDays(10)]);

Artisan::call('queue:batch-retry', [
'--failed-after' => '5 days ago',
]);

$this->assertEquals(5, DB::table('jobs')->count());
$this->assertCount(0, $newJobs->fresh()->filter());
$this->assertCount(5, $oldJobs->fresh()->filter());
}
}
21 changes: 0 additions & 21 deletions tests/ExampleTest.php

This file was deleted.

Loading

0 comments on commit 7ae2e39

Please sign in to comment.