* @brief Clean up abandoned or stale tasks in the system * * @note No input parameters - uses global worker PIDs * @return void - Logs results or errors * * Operation Flow: * 1. Get active worker PIDs * 2. Build SQL query with PID filter * 3. Clean tasks that are: * - Not finished (FIN
()
| 221 | * @note Logs cleanup count on success |
| 222 | */ |
| 223 | async function cleanup_task() { |
| 224 | let query_cleanup_task = TASK_QUERIES.CLEANUP_TASK; |
| 225 | let pids_for_query = get_worker_pids().join(','); |
| 226 | |
| 227 | if (__MSSQL_TEST__) { |
| 228 | pids_for_query = get_worker_pids() |
| 229 | .join(',') |
| 230 | .split(',') |
| 231 | .map(id => parseInt(id, 10)) |
| 232 | .filter(Number.isFinite); |
| 233 | logger(LOG_LEVEL.INFO, 'pids_for_query: ' + pids_for_query); |
| 234 | |
| 235 | // TODO: self-defined table is SQL Server CREATE TYPE IntList AS TABLE (value INT); |
| 236 | const table = new sql.Table(); |
| 237 | table.columns.add('value', sql.Int); |
| 238 | pids_for_query.forEach(pid => table.rows.add(pid)); |
| 239 | } |
| 240 | |
| 241 | if (pids_for_query.length > 0) { |
| 242 | if (__MSSQL_TEST__) { |
| 243 | query_cleanup_task += 'AND (PID IS NULL OR PID NOT IN (SELECT value FROM @pids))'; |
| 244 | } else { |
| 245 | query_cleanup_task += `AND (PID IS NULL OR PID NOT IN (${pids_for_query}))`; |
| 246 | } |
| 247 | } else { |
| 248 | return logger(LOG_LEVEL.ERROR, `cleanup_task pids_for_query ${pids_for_query} is empty.`, memo); |
| 249 | } |
| 250 | |
| 251 | const memo = operation_memo({}); |
| 252 | try { |
| 253 | const pool = await pool_promise; |
| 254 | const request = pool.request(); |
| 255 | if (__MSSQL_TEST__) { |
| 256 | request |
| 257 | .input('pid', sql.Int, process.pid) |
| 258 | .input('pids', table); |
| 259 | } else { |
| 260 | request |
| 261 | .input('pid', sql.Int, process.pid); |
| 262 | } |
| 263 | const result = await request.query(query_cleanup_task); |
| 264 | |
| 265 | if (result.rowsAffected[0] > 0) { |
| 266 | logger(LOG_LEVEL.INFO, `cleanup_task clean up ${result.rowsAffected[0]} records.`, memo); |
| 267 | } |
| 268 | } catch (err) { |
| 269 | logger(LOG_LEVEL.ERROR, `cleanup_task ${err}.`, memo); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @brief Query current status of a transcription task |
no test coverage detected