From c67aa979c2c51779fd168a1b8c0be25db5d40a83 Mon Sep 17 00:00:00 2001 From: arifal Date: Fri, 7 Mar 2025 14:04:37 +0700 Subject: [PATCH] change column type expertise and fix syncronize simbg service --- app/Console/Commands/ExecuteScraping.php | 2 +- app/Jobs/SyncronizeSIMBG.php | 11 ++++- app/Models/TaskAssignment.php | 2 +- app/Services/ServiceSIMBG.php | 41 ++++++------------- config/database.php | 2 + config/queue.php | 2 +- ...pertise_experience_in_task_assignments.php | 32 +++++++++++++++ 7 files changed, 58 insertions(+), 34 deletions(-) create mode 100644 database/migrations/2025_03_07_105725_modify_expertise_experience_in_task_assignments.php diff --git a/app/Console/Commands/ExecuteScraping.php b/app/Console/Commands/ExecuteScraping.php index 48159df..d440889 100644 --- a/app/Console/Commands/ExecuteScraping.php +++ b/app/Console/Commands/ExecuteScraping.php @@ -34,7 +34,7 @@ class ExecuteScraping extends Command } public function handle() { - SyncronizeSIMBG::dispatch(); + SyncronizeSIMBG::dispatch()->onQueue('default'); Log::info("running scheduler daily scraping"); } } diff --git a/app/Jobs/SyncronizeSIMBG.php b/app/Jobs/SyncronizeSIMBG.php index 18fb3ac..2c5dab7 100644 --- a/app/Jobs/SyncronizeSIMBG.php +++ b/app/Jobs/SyncronizeSIMBG.php @@ -22,7 +22,14 @@ class SyncronizeSIMBG implements ShouldQueue public function handle(): void { - $serviceSIMBG = app(ServiceSIMBG::class); - $serviceSIMBG->syncTaskPBG(); + try { + $serviceSIMBG = app(ServiceSIMBG::class); + $serviceSIMBG->syncTaskPBG(); + } catch (\Exception $e) { + \Log::error("SyncronizeSIMBG Job Failed: " . $e->getMessage(), [ + 'exception' => $e, + ]); + $this->fail($e); // Mark the job as failed + } } } diff --git a/app/Models/TaskAssignment.php b/app/Models/TaskAssignment.php index 6ebf643..8c8fda0 100644 --- a/app/Models/TaskAssignment.php +++ b/app/Models/TaskAssignment.php @@ -12,7 +12,7 @@ class TaskAssignment extends Model protected $fillable = [ 'user_id', 'name', 'username', 'email', 'phone_number', 'role', 'role_name', 'is_active', 'file', 'expertise', 'experience', - 'is_verif', 'uid', 'status', 'status_name', 'note', 'pbg_task_uid' + 'is_verif', 'uid', 'status', 'status_name', 'note', 'pbg_task_uid', 'tas_id' ]; protected $casts = [ diff --git a/app/Services/ServiceSIMBG.php b/app/Services/ServiceSIMBG.php index 1b4b25c..773a83b 100644 --- a/app/Services/ServiceSIMBG.php +++ b/app/Services/ServiceSIMBG.php @@ -339,20 +339,12 @@ class ServiceSIMBG $decodedResponse = json_decode($response->getContent(), true); if (isset($decodedResponse['errors']['code']) && $decodedResponse['errors']['code'] === 'token_not_valid') { - Log::warning("Token is invalid, refreshing token..."); - - // Regenerate token $initResToken = $this->getToken(); - // Check if new token is valid if (!empty($initResToken->original['data']['token']['access'])) { $new_token = $initResToken->original['data']['token']['access']; - - // **Fix: Update headers before retrying** $headers['Authorization'] = "Bearer " . $new_token; - - Log::info("Token refreshed successfully, retrying API request..."); - continue; // Retry with new token + continue; } else { Log::error("Failed to refresh token"); return $this->resError("Failed to refresh token"); @@ -464,29 +456,21 @@ class ServiceSIMBG 'Authorization' => "Bearer " . $token, ]; - for ($attempt = 0; $attempt < 2; $attempt++) { // Try twice (original + retry) + for ($attempt = 0; $attempt < 2; $attempt++) { $res = $this->service_client->get($url, $headers); // Check if response is JsonResponse and decode it if ($res instanceof \Illuminate\Http\JsonResponse) { $decodedResponse = json_decode($res->getContent(), true); - // Handle invalid token case if (isset($decodedResponse['errors']['code']) && $decodedResponse['errors']['code'] === 'token_not_valid') { - Log::warning("Token is invalid, refreshing token..."); - - // Regenerate the token $initResToken = $this->getToken(); - // Check if the new token is valid if (!empty($initResToken->original['data']['token']['access'])) { $new_token = $initResToken->original['data']['token']['access']; - // **Fix: Update headers with the new token** $headers['Authorization'] = "Bearer " . $new_token; - - Log::info("Token refreshed successfully, retrying API request..."); - continue; // Retry the request with the new token + continue; } else { Log::error("Failed to refresh token"); return $this->resError("Failed to refresh token"); @@ -494,7 +478,6 @@ class ServiceSIMBG } } - // If request succeeds, break out of retry loop break; } @@ -502,7 +485,6 @@ class ServiceSIMBG $responseData = $res->original ?? []; $data = $responseData['data']['data'] ?? []; if (empty($data)) { - Log::error("API response indicates failure", ['url' => $url, 'uuid' => $uuid, 'response' => $responseData]); return false; } @@ -583,7 +565,7 @@ class ServiceSIMBG foreach ($datas as $data) { $task_assignments[] = [ - 'pbg_task_uid' => $uuid, // Assuming this is a foreign key + 'pbg_task_uid' => $uuid, 'user_id' => $data['user_id'], 'name' => $data['name'], 'username' => $data['username'], @@ -592,22 +574,23 @@ class ServiceSIMBG 'role' => $data['role'], 'role_name' => $data['role_name'], 'is_active' => $data['is_active'], - 'file' => json_encode($data['file']), // Store as JSON if it's an array - 'expertise' => $data['expertise'], - 'experience' => $data['experience'], + 'file' => !empty($data['file']) ? json_encode($data['file']) : null, + 'expertise' => !empty($data['expertise']) ? json_encode($data['expertise']) : null, + 'experience' => !empty($data['experience']) ? json_encode($data['experience']) : null, 'is_verif' => $data['is_verif'], - 'uid' => $data['uid'], // Unique identifier + 'uid' => $data['uid'], 'status' => $data['status'], 'status_name' => $data['status_name'], 'note' => $data['note'], + 'ta_id' => $data['id'], 'created_at' => now(), 'updated_at' => now(), ]; } TaskAssignment::upsert( - $task_assignments, // Data to insert/update - ['uid'], // Unique key for conflict resolution - ['name', 'username', 'email', 'phone_number', 'role', 'role_name', 'is_active', 'file', 'expertise', 'experience', 'is_verif', 'status', 'status_name', 'note', 'updated_at'] + $task_assignments, + ['uid'], + ['ta_id','name', 'username', 'email', 'phone_number', 'role', 'role_name', 'is_active', 'file', 'expertise', 'experience', 'is_verif', 'status', 'status_name', 'note', 'updated_at'] ); return true; }catch(Exception $e){ diff --git a/config/database.php b/config/database.php index 125949e..932b15a 100755 --- a/config/database.php +++ b/config/database.php @@ -79,6 +79,8 @@ return [ 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), + PDO::ATTR_TIMEOUT => 40000, + PDO::MYSQL_ATTR_INIT_COMMAND => "SET SESSION wait_timeout=40000; SET SESSION interactive_timeout=40000;" ]) : [], ], diff --git a/config/queue.php b/config/queue.php index f4f4ced..d309f44 100755 --- a/config/queue.php +++ b/config/queue.php @@ -112,7 +112,7 @@ return [ // set timeout queue 'worker' => [ - 'timeout' => 300 + 'timeout' => 40000 ] ]; diff --git a/database/migrations/2025_03_07_105725_modify_expertise_experience_in_task_assignments.php b/database/migrations/2025_03_07_105725_modify_expertise_experience_in_task_assignments.php new file mode 100644 index 0000000..4357458 --- /dev/null +++ b/database/migrations/2025_03_07_105725_modify_expertise_experience_in_task_assignments.php @@ -0,0 +1,32 @@ +json('expertise')->nullable()->change(); + $table->json('experience')->nullable()->change(); + $table->bigInteger('ta_id')->nullable()->after('id'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('task_assignments', function (Blueprint $table) { + $table->text('expertise')->nullable()->change(); + $table->text('experience')->nullable()->change(); + $table->dropColumn('ta_id'); + }); + } +};