From 900fc607aac13e4260627fdb83e1b07e2b346dc4 Mon Sep 17 00:00:00 2001 From: hafifierahman Date: Sat, 12 Oct 2024 20:18:09 +0800 Subject: [PATCH] added jobs in account check --- app/Http/Controllers/AdminPageController.php | 33 ++++++++++-- app/Jobs/AccountCheckJob.php | 53 ++++++++++++++++++++ routes/web.php | 3 +- 3 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 app/Jobs/AccountCheckJob.php diff --git a/app/Http/Controllers/AdminPageController.php b/app/Http/Controllers/AdminPageController.php index 7066153..46668cf 100644 --- a/app/Http/Controllers/AdminPageController.php +++ b/app/Http/Controllers/AdminPageController.php @@ -60,6 +60,9 @@ use Illuminate\Support\Collection; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; +use App\Jobs\AccountCheckJob; +use Illuminate\Support\Str; +use Illuminate\Support\Facades\Cache; class AdminPageController extends Controller { @@ -3834,15 +3837,20 @@ class AdminPageController extends Controller $cawangan = Cawangan::on('mysql7')->simplePaginate(5); $check = app(AccountService::class); - $array_all = ['current_page' => $cawangan->currentPage()]; + $array_all = ['current_page' => $cawangan->currentPage(), 'job_ids' => []]; foreach($cawangan as $caw){ try{ - $test =$check->checkAccount($caw['kodcaw'],$request->month); - $array_all['data'][$caw['details_caw']] = $test; + $jobId = Str::uuid(); + $res = dispatch(new AccountCheckJob($caw['kodcaw'], $request->month,$jobId)); + // $array_all['data'][$caw['details_caw']] = $res; + $array_all['data'][$caw['details_caw']] = [ + 'jobId' => $jobId, + 'status' => 'queued', + ]; + $array_all['job_ids'][] = $jobId; }catch(\Exception $e){ - Log::error($caw['kodcaw']); + Log::error("Error processing: " . $caw['kodcaw']); Log::error($e->getMessage()); - Log::error($e); } } @@ -3850,4 +3858,19 @@ class AdminPageController extends Controller return response()->json($response); } + + public function getJobStatus(Request $request) + { + $jobId = $request->input('jobId'); + + // Get the job status and result from cache (or database) + $status = Cache::get("job_status_{$jobId}", 'not_found'); + $result = Cache::get("job_result_{$jobId}", []); + + // Return both the status and the result + return response()->json([ + 'status' => $status, + 'result' => $status === 'completed' ? $result : null, + ]); + } } diff --git a/app/Jobs/AccountCheckJob.php b/app/Jobs/AccountCheckJob.php new file mode 100644 index 0000000..98e1e32 --- /dev/null +++ b/app/Jobs/AccountCheckJob.php @@ -0,0 +1,53 @@ +kodcaw = $kodcaw; + $this->month = $month; + $this->jobId = $jobId; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $logger = app('job_log'); + + try { + $check = app(AccountService::class); + $test =$check->checkAccount( $this->kodcaw,$this->month); + Cache::put("job_result_{$this->jobId}", $test, Carbon::now()->addMinutes(10)); + Cache::put("job_status_{$this->jobId}", 'completed', Carbon::now()->addMinutes(10)); + } catch (\Exception $e) { + $logger->error($e); + throw $e; + } + + } +} diff --git a/routes/web.php b/routes/web.php index 273b776..2b1f87d 100644 --- a/routes/web.php +++ b/routes/web.php @@ -851,7 +851,8 @@ $router->group(['prefix'=>'admin'], function () use ($router){ $router->get('ig/bayaran_penghutang',['uses'=>'AdminPageController@SenaraiBayaranHutang']); $router->get('get/accountcheck',['uses'=>'AdminPageController@getAccountList']); - + $router->post('get/jobstatus',['uses'=>'AdminPageController@getJobStatus']); + });