fix service scraping data

This commit is contained in:
arifal
2025-03-18 06:42:42 +07:00
parent 2e385f80cd
commit f9e1aa1604
4 changed files with 526 additions and 21 deletions

View File

@@ -5,6 +5,7 @@ namespace App\Console\Commands;
use App\Models\ImportDatasource;
use App\Services\ServiceGoogleSheet;
use App\Services\ServicePbgTask;
use App\Services\ServiceTabPbgTask;
use GuzzleHttp\Client; // Import Guzzle Client
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
@@ -28,15 +29,17 @@ class ScrapingData extends Command
private $client;
private $service_pbg_task;
private $service_tab_pbg_task;
/**
* Inject dependencies.
*/
public function __construct(Client $client, ServicePbgTask $service_pbg_task)
public function __construct(Client $client, ServicePbgTask $service_pbg_task, ServiceTabPbgTask $serviceTabPbgTask)
{
parent::__construct();
$this->client = $client;
$this->service_pbg_task = $service_pbg_task;
$this->service_tab_pbg_task = $serviceTabPbgTask;
}
/**
@@ -44,7 +47,6 @@ class ScrapingData extends Command
*/
public function handle()
{
DB::beginTransaction(); // Start transaction
try {
// Create a record with "processing" status
@@ -61,15 +63,16 @@ class ScrapingData extends Command
// Run the ServicePbgTask with injected Guzzle Client
$this->service_pbg_task->run_service();
// run the service pbg task assignments
$this->service_tab_pbg_task->run_service();
// Update the record status to "success" after completion
$import_datasource->update([
'status' => 'success',
'message' => 'Scraping completed successfully.'
]);
DB::commit(); // Commit the transaction if everything is successful
} catch (\Exception $e) {
DB::rollBack(); // Rollback transaction on error
// Log the error for debugging
Log::error('Scraping failed: ' . $e->getMessage(), ['trace' => $e->getTraceAsString()]);