fix handle sync using button
This commit is contained in:
@@ -57,7 +57,7 @@ class ScrapingData extends Command
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
// Run the service
|
// Run the service
|
||||||
$service_google_sheet = new ServiceGoogleSheet($import_datasource->id);
|
$service_google_sheet = new ServiceGoogleSheet();
|
||||||
$service_google_sheet->run_service();
|
$service_google_sheet->run_service();
|
||||||
|
|
||||||
// Run the ServicePbgTask with injected Guzzle Client
|
// Run the ServicePbgTask with injected Guzzle Client
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ class ScrapingController extends Controller
|
|||||||
return $this->resError("Failed to execute while processing another scraping");
|
return $this->resError("Failed to execute while processing another scraping");
|
||||||
}
|
}
|
||||||
|
|
||||||
// run service artisan command
|
// dispatch(new SyncronizeSIMBG());
|
||||||
SyncronizeSIMBG::dispatch();
|
Artisan::call("app:scraping-data");
|
||||||
return $this->resSuccess(["message" => "Success execute scraping service on background, check status for more"]);
|
return $this->resSuccess(["message" => "Success execute scraping service on background, check status for more"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,12 @@
|
|||||||
|
|
||||||
namespace App\Jobs;
|
namespace App\Jobs;
|
||||||
|
|
||||||
|
use App\Models\ImportDatasource;
|
||||||
use App\Services\GoogleSheetService;
|
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\Bus\Queueable;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
use Illuminate\Foundation\Bus\Dispatchable;
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
@@ -18,17 +22,58 @@ class SyncronizeSIMBG implements ShouldQueue
|
|||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
// Avoid injecting non-serializable dependencies here
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle(): void
|
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 {
|
try {
|
||||||
$serviceSIMBG = app(ServiceSIMBG::class);
|
// Create an instance of GuzzleHttp\Client inside handle()
|
||||||
$serviceSIMBG->syncTaskPBG();
|
$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) {
|
} catch (\Exception $e) {
|
||||||
\Log::error("SyncronizeSIMBG Job Failed: " . $e->getMessage(), [
|
\Log::error("SyncronizeSIMBG Job Failed: " . $e->getMessage(), [
|
||||||
'exception' => $e,
|
'exception' => $e,
|
||||||
]);
|
]);
|
||||||
|
$import_datasource->update([
|
||||||
|
'status' => 'failed',
|
||||||
|
'message' => 'Failed job'
|
||||||
|
]);
|
||||||
$this->fail($e); // Mark the job as failed
|
$this->fail($e); // Mark the job as failed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ use App\Models\DataSetting;
|
|||||||
use App\Models\ImportDatasource;
|
use App\Models\ImportDatasource;
|
||||||
use App\Models\PbgTaskGoogleSheet;
|
use App\Models\PbgTaskGoogleSheet;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Exception;
|
||||||
use Google_Client;
|
use Google_Client;
|
||||||
use Google_Service_Sheets;
|
use Google_Service_Sheets;
|
||||||
use Log;
|
use Log;
|
||||||
@@ -15,7 +16,7 @@ class ServiceGoogleSheet
|
|||||||
protected $spreadsheetID;
|
protected $spreadsheetID;
|
||||||
protected $service_sheets;
|
protected $service_sheets;
|
||||||
protected $import_datasource;
|
protected $import_datasource;
|
||||||
public function __construct(int $import_datasource_id)
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->client = new Google_Client();
|
$this->client = new Google_Client();
|
||||||
$this->client->setApplicationName("Sibedas Google Sheets API");
|
$this->client->setApplicationName("Sibedas Google Sheets API");
|
||||||
@@ -27,21 +28,15 @@ class ServiceGoogleSheet
|
|||||||
$this->spreadsheetID = env("SPREAD_SHEET_ID");
|
$this->spreadsheetID = env("SPREAD_SHEET_ID");
|
||||||
|
|
||||||
$this->service_sheets = new Google_Service_Sheets($this->client);
|
$this->service_sheets = new Google_Service_Sheets($this->client);
|
||||||
$this->import_datasource = ImportDatasource::findOrFail($import_datasource_id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function run_service(){
|
public function run_service(){
|
||||||
$run_one = $this->sync_big_data();
|
try{
|
||||||
if(!$run_one){
|
$this->sync_big_data();
|
||||||
$this->import_datasource->update(['status' => 'failed']);
|
$this->sync_google_sheet_data();
|
||||||
return false;
|
}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() {
|
public function sync_google_sheet_data() {
|
||||||
try {
|
try {
|
||||||
@@ -49,7 +44,7 @@ class ServiceGoogleSheet
|
|||||||
|
|
||||||
if (empty($sheet_data) || count($sheet_data) < 2) {
|
if (empty($sheet_data) || count($sheet_data) < 2) {
|
||||||
Log::warning("sync_google_sheet_data: No valid data found.");
|
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) {
|
$cleanValue = function ($value) {
|
||||||
@@ -157,9 +152,10 @@ class ServiceGoogleSheet
|
|||||||
}
|
}
|
||||||
|
|
||||||
Log::info("sync google sheet done");
|
Log::info("sync google sheet done");
|
||||||
|
return true;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Log::error("sync_google_sheet_data failed", ['error' => $e->getMessage()]);
|
Log::error("sync_google_sheet_data failed", ['error' => $e->getMessage()]);
|
||||||
return false;
|
throw $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user