Created
December 30, 2020 01:00
-
-
Save sayhicoelho/7e5349627ec50257e57b0eff21e6f4db to your computer and use it in GitHub Desktop.
Laravel: Run only latest delayed job
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
<?php | |
namespace App\Support; | |
use Illuminate\Contracts\Bus\Dispatcher; | |
use Illuminate\Support\Facades\Redis; | |
class DelayedJob | |
{ | |
/** | |
* Job delay in seconds. | |
* | |
* @var int $delay | |
*/ | |
private static $delay = 600; | |
/** | |
* Dispatch the job with the given arguments. | |
* | |
* @param mixed $job | |
* @param string $queue | |
* @return void | |
*/ | |
public static function dispatch($job, $queue = 'default') | |
{ | |
$jobId = app(Dispatcher::class) | |
->dispatch($job->onQueue($queue) | |
->delay(now()->addSeconds(static::$delay)) | |
); | |
$prefix = static::getPrefix(); | |
$key = static::getKey($jobId, $queue); | |
$keys = Redis::keys(static::getKey('*', $queue)); | |
if (count($keys) > 0) { | |
Redis::del(array_map(function ($k) use ($prefix) { | |
return str_replace($prefix, '', $k); | |
}, $keys)); | |
} | |
Redis::set($key, now()->format('Y-m-d H:i:s')); | |
} | |
/** | |
* Check if the job can be executed. | |
* | |
* @param \Illuminate\Contracts\Queue\Job $job | |
* @return bool | |
*/ | |
public static function canHandle($job) | |
{ | |
$key = static::getKey( | |
$job->getJobId(), | |
$job->getQueue() | |
); | |
if (Redis::exists($key)) { | |
Redis::del($key); | |
return true; | |
} | |
return false; | |
} | |
protected static function getKey($jobId, $queue = 'default') | |
{ | |
return "delayed:{$queue}:{$jobId}"; | |
} | |
protected static function getPrefix() | |
{ | |
return config('database.redis.options.prefix'); | |
} | |
} |
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
<?php | |
namespace App\Http\Controllers; | |
use App\Jobs\ExampleJob; | |
use App\Support\DelayedJob; | |
use Illuminate\Http\Request; | |
class ExampleController extends Controller | |
{ | |
public function doSomething(Request $request) | |
{ | |
$data = now()->format('Y-m-d H:i:s'); | |
$queue = 'example'; | |
DelayedJob::dispatch(new ExampleJob($data), $queue); | |
return response()->json([ | |
'message' => 'Dispatched.' | |
]); | |
} | |
} |
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
<?php | |
namespace App\Jobs; | |
use App\Support\DelayedJob; | |
use Illuminate\Bus\Queueable; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
use Illuminate\Foundation\Bus\Dispatchable; | |
use Illuminate\Queue\InteractsWithQueue; | |
use Illuminate\Queue\SerializesModels; | |
class ExampleJob implements ShouldQueue | |
{ | |
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | |
/** | |
* The job's data. | |
* | |
* @var mixed | |
*/ | |
private $data; | |
/** | |
* Create a new job instance. | |
* | |
* @return void | |
*/ | |
public function __construct($data) | |
{ | |
$this->data = $data; | |
} | |
/** | |
* Execute the job. | |
* | |
* @return void | |
*/ | |
public function handle() | |
{ | |
if (DelayedJob::canHandle($this->job)) { | |
echo 'Job executed having data: ' . $this->data . PHP_EOL; | |
} else { | |
echo 'Job can\'t execute.' . PHP_EOL; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment