diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index bb49cba..42c4749 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -30,7 +30,7 @@ public function index() public function sync() { $user = Auth::user(); - FetchGoogleFit::dispatch($user); + FetchGoogleFit::dispatch($user)->onQueue('stepsCount'); return $this->stepActivityRepository->getInToday($user->id); } } diff --git a/app/Jobs/FetchGoogleFit.php b/app/Jobs/FetchGoogleFit.php index 00c23f4..a1c1967 100644 --- a/app/Jobs/FetchGoogleFit.php +++ b/app/Jobs/FetchGoogleFit.php @@ -6,30 +6,46 @@ use App\Repositories\StepActivityRepositoryInterface; use App\Repositories\TokenRepositoryInterface; use App\Services\GoogleApiService; +use Illuminate\Cache\Repository; +use Illuminate\Contracts\Queue\ShouldBeUnique; +use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Queue\Queueable; use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\Middleware\WithoutOverlapping; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; -class FetchGoogleFit implements ShouldQueue +class FetchGoogleFit implements ShouldQueue, ShouldBeUnique { use Queueable; use Dispatchable; - /** - * Create a new job instance. - */ + public $uniqueFor = 60; + public function __construct( public User $user, ) { } - /** - * Execute the job. - */ + public function uniqueId(): string + { + return $this->user->id; + } + + public function uniqueVia(): Repository + { + return Cache::driver('redis'); + } + + public function middleware(): array + { + return [(new WithoutOverlapping($this->user->id))->releaseAfter(60)]; + } + public function handle(GoogleApiService $googleApiService, StepActivityRepositoryInterface $stepActivityRepository): void { DB::transaction(function () use ($googleApiService, $stepActivityRepository) { diff --git a/routes/console.php b/routes/console.php index 8c60f5c..c5c6d07 100644 --- a/routes/console.php +++ b/routes/console.php @@ -2,5 +2,6 @@ use Illuminate\Support\Facades\Schedule; -Schedule::command('queue:work --stop-when-empty')->everySecond()->runInBackground()->withoutOverlapping()->sendOutputTo(getcwd()."/queue.log"); -Schedule::command('app:convert-steps-to-coin --stop-when-empty')->dailyAt('23:00')->runInBackground()->withoutOverlapping()->sendOutputTo(getcwd()."/converter.log"); +Schedule::command('queue:work --stop-when-empty --queue=high,default')->everyMinute()->withoutOverlapping()->sendOutputTo(getcwd()."/queue.log"); +Schedule::command('queue:work --stop-when-empty --queue=stepsCount')->everyFiveMinutes()->withoutOverlapping()->sendOutputTo(getcwd()."/queue.log"); +Schedule::command('app:convert-steps-to-coin --stop-when-empty')->dailyAt('23:00')->withoutOverlapping()->sendOutputTo(getcwd()."/converter.log");