fix handle sync using button

This commit is contained in:
arifal
2025-03-18 08:10:06 +07:00
parent 5e139bc29c
commit e940b8d6c7
4 changed files with 61 additions and 20 deletions

View File

@@ -57,7 +57,7 @@ class ScrapingData extends Command
]);
// Run the service
$service_google_sheet = new ServiceGoogleSheet($import_datasource->id);
$service_google_sheet = new ServiceGoogleSheet();
$service_google_sheet->run_service();
// Run the ServicePbgTask with injected Guzzle Client

View File

@@ -23,8 +23,8 @@ class ScrapingController extends Controller
return $this->resError("Failed to execute while processing another scraping");
}
// run service artisan command
SyncronizeSIMBG::dispatch();
// dispatch(new SyncronizeSIMBG());
Artisan::call("app:scraping-data");
return $this->resSuccess(["message" => "Success execute scraping service on background, check status for more"]);
}

View File

@@ -2,8 +2,12 @@
namespace App\Jobs;
use App\Models\ImportDatasource;
use App\Services\GoogleSheetService;
use App\Services\ServiceSIMBG;
use App\Services\ServiceGoogleSheet;
use App\Services\ServicePbgTask;
use App\Services\ServiceTabPbgTask;
use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@@ -18,17 +22,58 @@ class SyncronizeSIMBG implements ShouldQueue
public function __construct()
{
// Avoid injecting non-serializable dependencies here
}
public function handle(): void
{
$import_datasource = ImportDatasource::where('status', 'processing')->first();
if (!$import_datasource) {
$import_datasource = ImportDatasource::create([
'message' => 'Initiating scraping...',
'response_body' => null,
'status' => 'processing'
]);
}
try {
$serviceSIMBG = app(ServiceSIMBG::class);
$serviceSIMBG->syncTaskPBG();
// Create an instance of GuzzleHttp\Client inside handle()
$client = new Client();
// Create instances of services inside handle()
$service_pbg_task = app(ServicePbgTask::class);
$service_tab_pbg_task = app(ServiceTabPbgTask::class);
// Create a record with "processing" status
// Run the service
$service_google_sheet = new ServiceGoogleSheet();
\Log::info('Starting Google Sheet service');
$service_google_sheet->run_service();
\Log::info('Google Sheet service completed');
\Log::info('Starting PBG Task service');
$service_pbg_task->run_service();
\Log::info('PBG Task service completed');
\Log::info('Starting Tab PBG Task service');
$service_tab_pbg_task->run_service();
\Log::info('Tab PBG Task service completed');
// Update the record status to "success" after completion
$import_datasource->update([
'status' => 'success',
'message' => 'Scraping completed successfully.'
]);
} catch (\Exception $e) {
\Log::error("SyncronizeSIMBG Job Failed: " . $e->getMessage(), [
'exception' => $e,
]);
$import_datasource->update([
'status' => 'failed',
'message' => 'Failed job'
]);
$this->fail($e); // Mark the job as failed
}
}

View File

@@ -5,6 +5,7 @@ use App\Models\DataSetting;
use App\Models\ImportDatasource;
use App\Models\PbgTaskGoogleSheet;
use Carbon\Carbon;
use Exception;
use Google_Client;
use Google_Service_Sheets;
use Log;
@@ -15,7 +16,7 @@ class ServiceGoogleSheet
protected $spreadsheetID;
protected $service_sheets;
protected $import_datasource;
public function __construct(int $import_datasource_id)
public function __construct()
{
$this->client = new Google_Client();
$this->client->setApplicationName("Sibedas Google Sheets API");
@@ -27,21 +28,15 @@ class ServiceGoogleSheet
$this->spreadsheetID = env("SPREAD_SHEET_ID");
$this->service_sheets = new Google_Service_Sheets($this->client);
$this->import_datasource = ImportDatasource::findOrFail($import_datasource_id);
}
public function run_service(){
$run_one = $this->sync_big_data();
if(!$run_one){
$this->import_datasource->update(['status' => 'failed']);
return false;
try{
$this->sync_big_data();
$this->sync_google_sheet_data();
}catch(Exception $e){
throw $e;
}
$run_two = $this->sync_google_sheet_data();
if(!$run_two){
$this->import_datasource->update(['status' => 'failed']);
return false;
}
return true;
}
public function sync_google_sheet_data() {
try {
@@ -49,7 +44,7 @@ class ServiceGoogleSheet
if (empty($sheet_data) || count($sheet_data) < 2) {
Log::warning("sync_google_sheet_data: No valid data found.");
return false;
throw new \Exception("sync_google_sheet_data: No valid data found.");
}
$cleanValue = function ($value) {
@@ -157,9 +152,10 @@ class ServiceGoogleSheet
}
Log::info("sync google sheet done");
return true;
} catch (\Exception $e) {
Log::error("sync_google_sheet_data failed", ['error' => $e->getMessage()]);
return false;
throw $e;
}
}