-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d589c44
commit 7ae2e39
Showing
14 changed files
with
343 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}); | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.