Skip to content

Commit

Permalink
fix: improve schedule task
Browse files Browse the repository at this point in the history
  • Loading branch information
mhdramadhanarvin committed Sep 21, 2024
1 parent 66436d3 commit fa04edb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
30 changes: 23 additions & 7 deletions app/Jobs/FetchGoogleFit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

0 comments on commit fa04edb

Please sign in to comment.