81 lines
2.6 KiB
PHP
81 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\ImportDatasource;
|
|
use App\Services\GoogleSheetService;
|
|
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;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class SyncronizeSIMBG implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public $tries = 1;
|
|
|
|
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 {
|
|
// 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
|
|
}
|
|
}
|
|
}
|