93 lines
2.9 KiB
PHP
93 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\BigdataResume;
|
|
use App\Models\ImportDatasource;
|
|
use App\Services\ServiceGoogleSheet;
|
|
use App\Services\ServicePbgTask;
|
|
use App\Services\ServiceTabPbgTask;
|
|
use App\Services\ServiceTokenSIMBG;
|
|
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;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ScrapingDataJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
/**
|
|
* Inject dependencies instead of creating them inside.
|
|
*/
|
|
public function __construct(
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle()
|
|
{
|
|
try {
|
|
|
|
$client = app(Client::class);
|
|
$service_pbg_task = app(ServicePbgTask::class);
|
|
$service_tab_pbg_task = app(ServiceTabPbgTask::class);
|
|
$service_google_sheet = app(ServiceGoogleSheet::class);
|
|
$service_token = app(ServiceTokenSIMBG::class);
|
|
// Create a record with "processing" status
|
|
$import_datasource = ImportDatasource::create([
|
|
'message' => 'Initiating scraping...',
|
|
'response_body' => null,
|
|
'status' => 'processing',
|
|
'start_time' => now(),
|
|
'failed_uuid' => null
|
|
]);
|
|
|
|
$failed_uuid = null;
|
|
|
|
// Run the scraping services
|
|
$service_google_sheet->run_service();
|
|
$service_pbg_task->run_service();
|
|
try{
|
|
$service_tab_pbg_task->run_service();
|
|
}catch(\Exception $e){
|
|
$failed_uuid = $service_tab_pbg_task->getFailedUUID();
|
|
throw $e;
|
|
}
|
|
|
|
// $data_setting_result = $service_google_sheet->get_big_resume_data();
|
|
|
|
// BigdataResume::generateResumeData($import_datasource->id, "all", $data_setting_result);
|
|
// BigdataResume::generateResumeData($import_datasource->id, now()->year, $data_setting_result);
|
|
|
|
// Update status to success
|
|
$import_datasource->update([
|
|
'status' => 'success',
|
|
'message' => 'Scraping completed successfully.',
|
|
'finish_time' => now()
|
|
]);
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error('Scraping failed: ' . $e->getMessage(), ['trace' => $e->getTraceAsString()]);
|
|
|
|
// Update status to failed
|
|
if (isset($import_datasource)) {
|
|
$import_datasource->update([
|
|
'status' => 'failed',
|
|
'response_body' => 'Terjadi kesalahan, Syncronize tidak selesai',
|
|
'finish_time' => now(),
|
|
'failed_uuid' => $failed_uuid,
|
|
]);
|
|
}
|
|
|
|
// Mark the job as failed
|
|
$this->fail($e);
|
|
}
|
|
}
|
|
}
|