change column type expertise and fix syncronize simbg service

This commit is contained in:
arifal
2025-03-07 14:04:37 +07:00
parent fbaa33ae13
commit c67aa979c2
7 changed files with 58 additions and 34 deletions

View File

@@ -34,7 +34,7 @@ class ExecuteScraping extends Command
} }
public function handle() public function handle()
{ {
SyncronizeSIMBG::dispatch(); SyncronizeSIMBG::dispatch()->onQueue('default');
Log::info("running scheduler daily scraping"); Log::info("running scheduler daily scraping");
} }
} }

View File

@@ -22,7 +22,14 @@ class SyncronizeSIMBG implements ShouldQueue
public function handle(): void public function handle(): void
{ {
$serviceSIMBG = app(ServiceSIMBG::class); try {
$serviceSIMBG->syncTaskPBG(); $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
}
} }
} }

View File

@@ -12,7 +12,7 @@ class TaskAssignment extends Model
protected $fillable = [ protected $fillable = [
'user_id', 'name', 'username', 'email', 'phone_number', 'role', 'user_id', 'name', 'username', 'email', 'phone_number', 'role',
'role_name', 'is_active', 'file', 'expertise', 'experience', '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 = [ protected $casts = [

View File

@@ -339,20 +339,12 @@ class ServiceSIMBG
$decodedResponse = json_decode($response->getContent(), true); $decodedResponse = json_decode($response->getContent(), true);
if (isset($decodedResponse['errors']['code']) && $decodedResponse['errors']['code'] === 'token_not_valid') { if (isset($decodedResponse['errors']['code']) && $decodedResponse['errors']['code'] === 'token_not_valid') {
Log::warning("Token is invalid, refreshing token...");
// Regenerate token
$initResToken = $this->getToken(); $initResToken = $this->getToken();
// Check if new token is valid
if (!empty($initResToken->original['data']['token']['access'])) { if (!empty($initResToken->original['data']['token']['access'])) {
$new_token = $initResToken->original['data']['token']['access']; $new_token = $initResToken->original['data']['token']['access'];
// **Fix: Update headers before retrying**
$headers['Authorization'] = "Bearer " . $new_token; $headers['Authorization'] = "Bearer " . $new_token;
continue;
Log::info("Token refreshed successfully, retrying API request...");
continue; // Retry with new token
} else { } else {
Log::error("Failed to refresh token"); Log::error("Failed to refresh token");
return $this->resError("Failed to refresh token"); return $this->resError("Failed to refresh token");
@@ -464,29 +456,21 @@ class ServiceSIMBG
'Authorization' => "Bearer " . $token, '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); $res = $this->service_client->get($url, $headers);
// Check if response is JsonResponse and decode it // Check if response is JsonResponse and decode it
if ($res instanceof \Illuminate\Http\JsonResponse) { if ($res instanceof \Illuminate\Http\JsonResponse) {
$decodedResponse = json_decode($res->getContent(), true); $decodedResponse = json_decode($res->getContent(), true);
// Handle invalid token case
if (isset($decodedResponse['errors']['code']) && $decodedResponse['errors']['code'] === 'token_not_valid') { if (isset($decodedResponse['errors']['code']) && $decodedResponse['errors']['code'] === 'token_not_valid') {
Log::warning("Token is invalid, refreshing token...");
// Regenerate the token
$initResToken = $this->getToken(); $initResToken = $this->getToken();
// Check if the new token is valid
if (!empty($initResToken->original['data']['token']['access'])) { if (!empty($initResToken->original['data']['token']['access'])) {
$new_token = $initResToken->original['data']['token']['access']; $new_token = $initResToken->original['data']['token']['access'];
// **Fix: Update headers with the new token**
$headers['Authorization'] = "Bearer " . $new_token; $headers['Authorization'] = "Bearer " . $new_token;
continue;
Log::info("Token refreshed successfully, retrying API request...");
continue; // Retry the request with the new token
} else { } else {
Log::error("Failed to refresh token"); Log::error("Failed to refresh token");
return $this->resError("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; break;
} }
@@ -502,7 +485,6 @@ class ServiceSIMBG
$responseData = $res->original ?? []; $responseData = $res->original ?? [];
$data = $responseData['data']['data'] ?? []; $data = $responseData['data']['data'] ?? [];
if (empty($data)) { if (empty($data)) {
Log::error("API response indicates failure", ['url' => $url, 'uuid' => $uuid, 'response' => $responseData]);
return false; return false;
} }
@@ -583,7 +565,7 @@ class ServiceSIMBG
foreach ($datas as $data) { foreach ($datas as $data) {
$task_assignments[] = [ $task_assignments[] = [
'pbg_task_uid' => $uuid, // Assuming this is a foreign key 'pbg_task_uid' => $uuid,
'user_id' => $data['user_id'], 'user_id' => $data['user_id'],
'name' => $data['name'], 'name' => $data['name'],
'username' => $data['username'], 'username' => $data['username'],
@@ -592,22 +574,23 @@ class ServiceSIMBG
'role' => $data['role'], 'role' => $data['role'],
'role_name' => $data['role_name'], 'role_name' => $data['role_name'],
'is_active' => $data['is_active'], 'is_active' => $data['is_active'],
'file' => json_encode($data['file']), // Store as JSON if it's an array 'file' => !empty($data['file']) ? json_encode($data['file']) : null,
'expertise' => $data['expertise'], 'expertise' => !empty($data['expertise']) ? json_encode($data['expertise']) : null,
'experience' => $data['experience'], 'experience' => !empty($data['experience']) ? json_encode($data['experience']) : null,
'is_verif' => $data['is_verif'], 'is_verif' => $data['is_verif'],
'uid' => $data['uid'], // Unique identifier 'uid' => $data['uid'],
'status' => $data['status'], 'status' => $data['status'],
'status_name' => $data['status_name'], 'status_name' => $data['status_name'],
'note' => $data['note'], 'note' => $data['note'],
'ta_id' => $data['id'],
'created_at' => now(), 'created_at' => now(),
'updated_at' => now(), 'updated_at' => now(),
]; ];
} }
TaskAssignment::upsert( TaskAssignment::upsert(
$task_assignments, // Data to insert/update $task_assignments,
['uid'], // Unique key for conflict resolution ['uid'],
['name', 'username', 'email', 'phone_number', 'role', 'role_name', 'is_active', 'file', 'expertise', 'experience', 'is_verif', 'status', 'status_name', 'note', 'updated_at'] ['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; return true;
}catch(Exception $e){ }catch(Exception $e){

View File

@@ -79,6 +79,8 @@ return [
'engine' => null, 'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([ 'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), 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;"
]) : [], ]) : [],
], ],

View File

@@ -112,7 +112,7 @@ return [
// set timeout queue // set timeout queue
'worker' => [ 'worker' => [
'timeout' => 300 'timeout' => 40000
] ]
]; ];

View File

@@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('task_assignments', function (Blueprint $table) {
$table->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');
});
}
};