Skip to content

Commit

Permalink
feat: mini improvement (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhdramadhanarvin authored Oct 26, 2024
1 parent 213ee42 commit b643e96
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 31 deletions.
5 changes: 3 additions & 2 deletions app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use App\Jobs\FetchGoogleFit;
use App\Repositories\StepActivityRepositoryInterface;
use App\Services\GoogleApiService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;

class DashboardController extends Controller
Expand All @@ -30,7 +30,8 @@ public function index()
public function sync()
{
$user = Auth::user();
FetchGoogleFit::dispatch($user)->onQueue('stepsCount');
FetchGoogleFit::dispatch($user);
Log::info("Running queue...");
return $this->stepActivityRepository->getInToday($user->id);
}
}
34 changes: 6 additions & 28 deletions app/Jobs/FetchGoogleFit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@

use App\Models\User;
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, ShouldBeUnique
class FetchGoogleFit implements ShouldQueue
{
use Queueable;
use Dispatchable;
Expand All @@ -31,27 +23,13 @@ public function __construct(
) {
}

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) {
Log::info("Queue FetchGoogleFit: STARTING..");
$data = $googleApiService->syncData($this->user);
$step = $stepActivityRepository->getInToday($this->user->id);
Log::info("Queue FetchGoogleFit: STARTING..");
$data = $googleApiService->syncData($this->user);
$step = $stepActivityRepository->getInToday($this->user->id);

DB::transaction(function () use ($stepActivityRepository, $step, $data) {

if (!$step) {
$stepActivityRepository->create($this->user, [
Expand Down
2 changes: 1 addition & 1 deletion resources/js/Pages/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
faWalking,
} from "@fortawesome/free-solid-svg-icons";
import { CircularProgress, createTheme, ThemeProvider } from "@mui/material";
import { Head, Link } from "@inertiajs/react";
import { Head } from "@inertiajs/react";
import { useQuery } from "react-query";
import { useState } from "react";

Expand Down

0 comments on commit b643e96

Please sign in to comment.