UNPKG

flexbiz-server

Version:

Flexible Server

23 lines (22 loc) 11.5 kB
const {onAfterCommit}=require("./sessionContext"),_=require("lodash"),moment=require("moment"),debouncers=new Map,DEBOUNCE_WAIT_MS=500,_startWorkerLoop=async($id_app$$,$job_type$$,$processingFunction$$)=>{Logger.debug(`[optimizeBookkeeping][${$job_type$$}] 1. App ${$id_app$$} start worker loop...`);try{const $TaskModel$$=global.getModel("sys_job_queue_book_keeping");if($TaskModel$$)if(Logger.debug(`[optimizeBookkeeping][${$job_type$$}] 2. Model found. Trying to acquire lock...`),await $TaskModel$$.findOneAndUpdate({id_app:$id_app$$, job_type:$job_type$$,status_task:{$ne:"PROCESSING"}},{$set:{status_task:"PROCESSING",updated_at:new Date}},{new:!0,useFindAndModify:!1})){Logger.debug(`[optimizeBookkeeping][${$job_type$$}] 3. \u2705 \u0110\u00e3 chi\u1ebfm lock (PROCESSING). B\u1eaft \u0111\u1ea7u x\u1eed l\u00fd...`);for(var $checkStatus_hasMoreWork$$=!0;$checkStatus_hasMoreWork$$;){Logger.debug(`[optimizeBookkeeping][${$job_type$$}] 4. \u0110ang l\u1ea5y task t\u1eeb Pending...`);const $currentDoc$$=await $TaskModel$$.findOne({id_app:$id_app$$, job_type:$job_type$$}).lean(),$newProcessing$$=_.union($currentDoc$$.processing_dates||[],$currentDoc$$.pending_dates||[]),$task$$=await $TaskModel$$.findOneAndUpdate({id_app:$id_app$$,job_type:$job_type$$},{$set:{processing_dates:$newProcessing$$,pending_dates:[],updated_at:new Date}},{new:!0,useFindAndModify:!1}).lean();if(!$task$$||!$task$$.processing_dates||$task$$.processing_dates.length===0){Logger.debug(`[optimizeBookkeeping][${$job_type$$}] 5. H\u1ebft vi\u1ec7c (Processing empty). D\u1eebng.`); await $TaskModel$$.updateOne({id_app:$id_app$$,job_type:$job_type$$},{$set:{status_task:"IDLE"}});break}const $allPendingDates$$=$task$$.processing_dates,$datesToProcess$$=$allPendingDates$$.slice(0,30);Logger.debug(`[optimizeBookkeeping][${$job_type$$}] 6. Th\u1ef1c thi Business Logic v\u1edbi ${$datesToProcess$$.length}/${$allPendingDates$$.length} ng\u00e0y (Chunking)...`);const $startBatchTime$$=Date.now();await $processingFunction$$($id_app$$,$datesToProcess$$);const $executionTime$$=Date.now()- $startBatchTime$$;Logger.debug(`[optimizeBookkeeping][${$job_type$$}] 7. X\u1eed l\u00fd xong batch trong ${$executionTime$$}ms.`);await $TaskModel$$.updateOne({_id:$task$$._id},{$pullAll:{processing_dates:$datesToProcess$$},$set:{updated_at:new Date}});const $checkNext$$=await $TaskModel$$.findOne({id_app:$id_app$$,job_type:$job_type$$}).lean();$checkNext$$&&$checkNext$$.pending_dates&&$checkNext$$.pending_dates.length>0?(Logger.debug(`[optimizeBookkeeping][${$job_type$$}] 8. C\u00f3 data m\u1edbi chen ngang. Loop ti\u1ebfp...`), $checkStatus_hasMoreWork$$=!0):$checkNext$$.processing_dates.length>0?(Logger.debug(`[optimizeBookkeeping][${$job_type$$}] 8. V\u1eabn c\u00f2n d\u01b0 data processing (l\u1ed7i insert?). Loop ti\u1ebfp...`),$checkStatus_hasMoreWork$$=!0):(Logger.debug(`[optimizeBookkeeping][${$job_type$$}] 8. Ho\u00e0n t\u1ea5t to\u00e0n b\u1ed9. Set IDLE.`),$checkStatus_hasMoreWork$$=!1,await $TaskModel$$.updateOne({id_app:$id_app$$,job_type:$job_type$$},{$set:{status_task:"IDLE"}}))}}else $checkStatus_hasMoreWork$$= await $TaskModel$$.findOne({id_app:$id_app$$,job_type:$job_type$$}).lean(),Logger.debug(`[optimizeBookkeeping][${$job_type$$}] 3. Kh\u00f4ng chi\u1ebfm \u0111\u01b0\u1ee3c lock. Status hi\u1ec7n t\u1ea1i: ${$checkStatus_hasMoreWork$$?$checkStatus_hasMoreWork$$.status_task:"Not Found"}. B\u1ecf qua.`);else Logger.error("[optimizeBookkeeping][FATAL] Kh\u00f4ng t\u00ecm th\u1ea5y Model 'sys_job_queue_book_keeping'. Ki\u1ec3m tra l\u1ea1i khai b\u00e1o schema.")}catch($e$$){Logger.error("[optimizeBookkeeping][FATAL ERROR] L\u1ed7i t\u1ea1i worker loop:", $e$$);try{const $TaskModel$$=global.getModel("sys_job_queue_book_keeping");$TaskModel$$&&await $TaskModel$$.updateOne({id_app:$id_app$$,job_type:$job_type$$},{$set:{status_task:"IDLE"}})}catch($errReset$$){Logger.error("[optimizeBookkeeping]",$errReset$$)}}},_processSokhoBatch=async($id_app$$,$dateArray$$)=>{Logger.debug("[optimizeBook] [_processSokhoBatch] B\u1eaft \u0111\u1ea7u x\u1eed l\u00fd batch",$id_app$$,$dateArray$$);var $data_sokho_data_sokho_tc_sokho$$=global.getModel("sokho");const $sokhotc$$= global.getModel("sokhotc");var $allDates_maxDate$$=$dateArray$$.map($d$$=>moment($d$$,"YYYY-MM-DD"));const $minDate$$=moment.min($allDates_maxDate$$).startOf("day").toDate();$allDates_maxDate$$=moment.max($allDates_maxDate$$).endOf("day").toDate();$id_app$$={id_app:$id_app$$,ngay_ct:{$gte:$minDate$$,$lte:$allDates_maxDate$$}};await $sokhotc$$.deleteMany($id_app$$);$data_sokho_data_sokho_tc_sokho$$=await $data_sokho_data_sokho_tc_sokho$$.find($id_app$$).lean();Logger.debug(`[optimizeBook] [_processSokhoBatch] \u0111\u00e3 t\u00ecm \u0111\u01b0\u1ee3c ${$data_sokho_data_sokho_tc_sokho$$.length} sokho v\u1edbi \u0111i\u1ec1u ki\u1ec7n`, JSON.stringify($id_app$$,null,2));$data_sokho_data_sokho_tc_sokho$$&&$data_sokho_data_sokho_tc_sokho$$.length!==0&&($data_sokho_data_sokho_tc_sokho$$=await $data_sokho_data_sokho_tc_sokho$$.asyncGroupBy("id_app ma_dvcs ma_ct ngay_ct ma_vt ma_dvt ma_lo han_sd ma_tt1 ma_tt2 ma_tt3 ma_kho ma_nt ty_gia ma_sp ma_cp ma_cong_doan".split(" "),"sl_nhap sl_xuat sl_nhap_qd sl_xuat_qd tien_hang_nt tien_hang tien_ck_nt tien_ck tien_phi_nt tien_phi tien_cp_nt tien_cp tien_hang_nk tien_hang_nk_nt tien_thue_nk tien_thue_nk_nt tien_nhap_nt tien_nhap tien_xuat tien_xuat_nt tien tien_nt tien_hang_ct_nt tien_hang_ct tien_ct tien_ct_nt tien_thue_nt tien_thue sl_sp".split(" ")), await $sokhotc$$.insertMany($data_sokho_data_sokho_tc_sokho$$),Logger.debug(`[optimizeBook] [_processSokhoBatch] \u0111\u00e3 t\u1ea1o ${$data_sokho_data_sokho_tc_sokho$$.length} sokhotc cho kho\u1ea3ng th\u1eddi gian`,$dateArray$$))},_processSocaiBatch=async($data_socaitc_id_app$$,$dateArray$$)=>{Logger.debug("[optimizeBook] [_processSocaiBatch] b\u1eaft \u0111\u1ea7u x\u1eed l\u00fd batch",$data_socaitc_id_app$$,$dateArray$$);var $raw_data_socai_socai$$=global.getModel("socai");const $socaitc$$= global.getModel("socaitc"),$socaitt$$=global.getModel("socaitt");var $allDates$jscomp$1_maxDate$$=$dateArray$$.map($d$$=>moment($d$$,"YYYY-MM-DD")),$minDate$jscomp$1_query$$=moment.min($allDates$jscomp$1_maxDate$$).startOf("day").toDate();$allDates$jscomp$1_maxDate$$=moment.max($allDates$jscomp$1_maxDate$$).endOf("day").toDate();$minDate$jscomp$1_query$$={id_app:$data_socaitc_id_app$$,ngay_ct:{$gte:$minDate$jscomp$1_query$$,$lte:$allDates$jscomp$1_maxDate$$}};await Promise.all([$socaitc$$.deleteMany($minDate$jscomp$1_query$$), $socaitt$$.deleteMany($minDate$jscomp$1_query$$)]);$raw_data_socai_socai$$=await $raw_data_socai_socai$$.find($minDate$jscomp$1_query$$).lean();Logger.debug(`[optimizeBook] [_processSocaiBatch] \u0111\u00e3 t\u00ecm \u0111\u01b0\u1ee3c ${$raw_data_socai_socai$$.length} socai v\u1edbi \u0111i\u1ec1u ki\u1ec7n`,JSON.stringify($minDate$jscomp$1_query$$,null,2));if($raw_data_socai_socai$$&&$raw_data_socai_socai$$.length!==0){await $raw_data_socai_socai$$.asyncJoinModel2($data_socaitc_id_app$$,"account", {where:{tk_no:"tk"},fields:{tk_cn_no:"tk_cn"}});await $raw_data_socai_socai$$.asyncJoinModel2($data_socaitc_id_app$$,"account",{where:{tk_co:"tk"},fields:{tk_cn_co:"tk_cn"}});for(var $data_socaitt_item$$ of $raw_data_socai_socai$$)$data_socaitt_item$$.tk_cn_no||($data_socaitt_item$$.ma_kh_no=""),$data_socaitt_item$$.tk_cn_co||($data_socaitt_item$$.ma_kh_co="");$data_socaitc_id_app$$=await $raw_data_socai_socai$$.asyncGroupBy("id_app ma_dvcs ma_ct ma_gd ngay_ct tk_no tk_co ma_kh_no ma_kh_co ma_kho ma_nt ty_gia ma_bp id_lenhsx ma_phi ma_hd ma_dt ma_nv ma_sp ma_cp ma_kenh ma_cong_doan".split(" "), ["tien","tien_nt"]);$data_socaitt_item$$=await $raw_data_socai_socai$$.asyncGroupBy("id_app ma_dvcs ma_ct ma_gd ngay_ct tk_no tk_co ma_nt ty_gia ma_bp ma_phi ma_hd ma_dt ma_nv".split(" "),["tien","tien_nt"]);await Promise.all([$data_socaitc_id_app$$.length>0?$socaitc$$.insertMany($data_socaitc_id_app$$):[],$data_socaitt_item$$.length>0?$socaitt$$.insertMany($data_socaitt_item$$):[]]);Logger.debug(`[optimizeBook] [_processSocaiBatch] \u0111\u00e3 t\u1ea1o ${$data_socaitc_id_app$$.length} socaitc cho kho\u1ea3ng th\u1eddi gian`, $dateArray$$);Logger.debug(`[optimizeBook] [_processSocaiBatch] \u0111\u00e3 t\u1ea1o ${$data_socaitt_item$$.length} socaitt cho kho\u1ea3ng th\u1eddi gian`,$dateArray$$)}},_triggerWorker=($id_app$$,$job_type$$,$processFn$$)=>{Logger.debug(`[DebounceTrigger] \u23f0 Debounce fired for ${$id_app$$} - ${$job_type$$}. Calling loop...`);try{_startWorkerLoop($id_app$$,$job_type$$,$processFn$$)}catch($err$$){Logger.error("[DebounceTrigger] \u274c Error calling worker loop:",$err$$)}},_enqueueTask=async($id_app$$, $job_type$$,$m_den_ngay_tu_ngay$$,$den_ngay$$,$processFn$$)=>{var $currentDate_m_tu_ngay$$=moment($m_den_ngay_tu_ngay$$);$m_den_ngay_tu_ngay$$=moment($den_ngay$$||$m_den_ngay_tu_ngay$$);let $datesToAdd$$=[];for($currentDate_m_tu_ngay$$=$currentDate_m_tu_ngay$$.clone().startOf("day");$currentDate_m_tu_ngay$$.isSameOrBefore($m_den_ngay_tu_ngay$$,"day");)$datesToAdd$$.push($currentDate_m_tu_ngay$$.format("YYYY-MM-DD")),$currentDate_m_tu_ngay$$.add(1,"day");const $debounceKey$$=`${$id_app$$}_${$job_type$$}`; Logger.debug(`[optimizeBookkeeping][_enqueueTask] 1. Chu\u1ea9n b\u1ecb th\u00eam task cho ${$id_app$$}`);onAfterCommit(async()=>{try{await global.getModel("sys_job_queue_book_keeping").updateOne({id_app:$id_app$$,job_type:$job_type$$},{$addToSet:{pending_dates:{$each:$datesToAdd$$}},$set:{updated_at:new Date}},{upsert:!0});Logger.debug(`[optimizeBookkeeping][_enqueueTask] 2. \u0110\u00e3 push DB ${$datesToAdd$$.length} ng\u00e0y. Setup Debounce...`);if(!debouncers.has($debounceKey$$)){Logger.debug(`[optimizeBookkeeping][_enqueueTask] 3. T\u1ea1o m\u1edbi Debouncer cho Key: ${$debounceKey$$}`); const $debouncedFunc$$=_.debounce(()=>{_triggerWorker($id_app$$,$job_type$$,$processFn$$)},DEBOUNCE_WAIT_MS,{leading:!1,trailing:!0});debouncers.set($debounceKey$$,$debouncedFunc$$)}const $runDebounce$$=debouncers.get($debounceKey$$);$runDebounce$$&&($runDebounce$$(),Logger.debug(`[optimizeBookkeeping][_enqueueTask] 4. \u0110\u00e3 g\u1ecdi Debounce (Ch\u1edd ${DEBOUNCE_WAIT_MS}ms)...`))}catch($e$$){Logger.error(`[optimizeBookkeeping] \u274c L\u1ed7i queue ${$job_type$$} app ${$id_app$$}:`,$e$$)}}, "job_queue_trigger");return Promise.resolve()};exports.createSokhoTC=async function($id_app$$,$tu_ngay$$,$den_ngay$$){return _enqueueTask($id_app$$,"SOKHO_TC",$tu_ngay$$,$den_ngay$$,_processSokhoBatch)};exports.createSocaiTC=async function($id_app$$,$tu_ngay$$,$den_ngay$$){return _enqueueTask($id_app$$,"SOCAI_TC",$tu_ngay$$,$den_ngay$$,_processSocaiBatch)}; exports.recoverBackgroundTasks=async()=>{try{Logger.info("[optimizeBookkeeping][Recovery] \u0110ang qu\u00e9t job n\u1ec1n ch\u01b0a ho\u00e0n th\u00e0nh...");const $tasks$$=await global.getModel("sys_job_queue_book_keeping").find({$or:[{processing_dates:{$not:{$size:0}}},{pending_dates:{$not:{$size:0}}}]}).lean();for(let $task$$ of $tasks$$)Logger.info(`[Recovery] Kh\u00f4i ph\u1ee5c ${$task$$.job_type} cho ${$task$$.id_app}`),await global.getModel("sys_job_queue_book_keeping").updateOne({_id:$task$$._id}, [{$set:{pending_dates:{$setUnion:["$pending_dates","$processing_dates"]},processing_dates:[],status_task:"RECOVERING"}}]),$task$$.job_type==="SOKHO_TC"?_startWorkerLoop($task$$.id_app,"SOKHO_TC",_processSokhoBatch):$task$$.job_type==="SOCAI_TC"&&_startWorkerLoop($task$$.id_app,"SOCAI_TC",_processSocaiBatch)}catch($e$$){Logger.error("[Recovery] L\u1ed7i:",$e$$)}};