TaskScheduler.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. const crypto = require('crypto')
  2. const bcryptjs = require('bcryptjs')
  3. const config = require('../../config.json')
  4. const db = require('../../plugin/DataBase/db')
  5. const Redis = require('../../plugin/DataBase/Redis')
  6. const Logger = require('../Logger')
  7. const TASK_STATUS = {
  8. PENDING: 'pending',
  9. ASSIGNED: 'assigned',
  10. RUNNING: 'running',
  11. SUCCESS: 'success',
  12. FAILED: 'failed',
  13. CANCELLED: 'cancelled'
  14. }
  15. const REPORT_LOG_EVENTS = new Set([
  16. 'request_result',
  17. 'progress_snapshot',
  18. 'grab_success',
  19. 'grab_fail'
  20. ])
  21. class TaskScheduler {
  22. constructor(options = {}) {
  23. this.leaseMs = options.leaseMs || config.qk?.leaseMs || 90 * 1000
  24. this.heartbeatTtlSeconds = options.heartbeatTtlSeconds || config.qk?.heartbeatTtlSeconds || 45
  25. this.pullLockTtlSeconds = options.pullLockTtlSeconds || 5
  26. this.staleTaskMs = options.staleTaskMs || config.qk?.staleTaskMs || 60 * 60 * 1000
  27. this.memPerSlotMb = options.memPerSlotMb || config.qk?.memPerSlotMb || 3072
  28. this.memReserveMb = options.memReserveMb || config.qk?.memReserveMb || 1024
  29. this.maxSlotsCap = options.maxSlotsCap || config.qk?.maxSlotsCap || 10
  30. this.logger = options.logger || new Logger()
  31. }
  32. calculateMaxSlots(profile = {}) {
  33. const freeMb = Math.max(0, Number(profile.free_mem_mb) || 0)
  34. const totalMb = Math.max(0, Number(profile.total_mem_mb) || 0)
  35. const threads = Math.max(1, Number(profile.cpu_threads) || 1)
  36. const allocatableFreeMb = Math.max(0, freeMb - this.memReserveMb)
  37. const allocatableTotalMb = Math.max(0, totalMb - this.memReserveMb)
  38. const byFreeMem = Math.floor(allocatableFreeMb / this.memPerSlotMb)
  39. const byTotalMem = Math.floor(allocatableTotalMb / this.memPerSlotMb)
  40. const byCpu = Math.floor(threads * 0.8)
  41. const fallbackMem = totalMb > 0 ? byTotalMem : 1
  42. const byMem = freeMb > 0 ? Math.min(byFreeMem, byTotalMem) : fallbackMem
  43. return Math.max(1, Math.min(50, this.maxSlotsCap, byMem, byCpu))
  44. }
  45. resolveClientMaxSlots(client, payload = {}) {
  46. const fromPayload = this.calculateMaxSlots({
  47. free_mem_mb: payload.free_mem_mb ?? client.free_mem_mb,
  48. total_mem_mb: payload.total_mem_mb ?? client.total_mem_mb,
  49. cpu_threads: payload.cpu_threads ?? client.cpu_threads
  50. })
  51. const reported = Number(payload.max_slots || client.max_slots || 0)
  52. if (!reported) return fromPayload
  53. return Math.max(1, Math.min(reported, fromPayload))
  54. }
  55. getClientAvailableSlots(client, payload = {}) {
  56. const maxSlots = this.resolveClientMaxSlots(client, payload)
  57. const currentSlots = Math.max(0, Number(payload.current_slots ?? client.current_slots ?? 0))
  58. return Math.max(0, maxSlots - currentSlots)
  59. }
  60. async countOnlineClients() {
  61. const now = Date.now()
  62. const threshold = now - this.heartbeatTtlSeconds * 1000
  63. const rows = await db.query(
  64. `SELECT COUNT(*) AS total FROM qk_client
  65. WHERE enabled = 1 AND online = 1
  66. AND last_heartbeat_at IS NOT NULL AND last_heartbeat_at >= ?`,
  67. [threshold]
  68. )
  69. return Number(rows?.[0]?.total || 0)
  70. }
  71. safeStringify(obj) {
  72. const seen = new WeakSet()
  73. return JSON.stringify(obj, (key, value) => {
  74. if (typeof value === 'object' && value !== null) {
  75. if (seen.has(value)) return '[Circular]'
  76. seen.add(value)
  77. }
  78. return value
  79. })
  80. }
  81. sanitizeForLog(payload) {
  82. if (!payload || typeof payload !== 'object') {
  83. return payload
  84. }
  85. const copy = Array.isArray(payload) ? [...payload] : { ...payload }
  86. for (const key of ['password', 'pass', 'password_enc', 'client_secret']) {
  87. if (key in copy) {
  88. copy[key] = '***'
  89. }
  90. }
  91. return copy
  92. }
  93. buildLogPrefix(tag, ctx = {}) {
  94. const parts = ['[QK]', `[${tag}]`]
  95. if (ctx.taskId) parts.push(`[taskId=${ctx.taskId}]`)
  96. if (ctx.clientId) parts.push(`[clientId=${ctx.clientId}]`)
  97. if (ctx.uuid) parts.push(`[uuid=${ctx.uuid}]`)
  98. return parts.join('')
  99. }
  100. logInfo(tag, message, ctx = {}, data = null) {
  101. const prefix = this.buildLogPrefix(tag, ctx)
  102. const suffix = data != null ? ` ${this.safeStringify(this.sanitizeForLog(data))}` : ''
  103. this.logger.info(`${prefix} ${message}${suffix}`)
  104. }
  105. logWarn(tag, message, ctx = {}, data = null) {
  106. const prefix = this.buildLogPrefix(tag, ctx)
  107. const suffix = data != null ? ` ${this.safeStringify(this.sanitizeForLog(data))}` : ''
  108. this.logger.warn(`${prefix} ${message}${suffix}`)
  109. }
  110. logError(tag, message, ctx = {}, err = null) {
  111. const prefix = this.buildLogPrefix(tag, ctx)
  112. const suffix = err ? ` ${err.stack || err}` : ''
  113. this.logger.error(`${prefix} ${message}${suffix}`)
  114. }
  115. getPasswordKey() {
  116. const source = process.env.QK_PASSWORD_KEY || config.qk?.passwordAesKey || config.database?.password || 'runforge-qk-default-key'
  117. return crypto.createHash('sha256').update(String(source)).digest()
  118. }
  119. encryptPassword(password) {
  120. const iv = crypto.randomBytes(16)
  121. const cipher = crypto.createCipheriv('aes-256-cbc', this.getPasswordKey(), iv)
  122. let encrypted = cipher.update(String(password), 'utf8', 'base64')
  123. encrypted += cipher.final('base64')
  124. return `${iv.toString('base64')}:${encrypted}`
  125. }
  126. decryptPassword(encrypted) {
  127. const [ivText, payload] = String(encrypted || '').split(':')
  128. if (!ivText || !payload) {
  129. return ''
  130. }
  131. const decipher = crypto.createDecipheriv('aes-256-cbc', this.getPasswordKey(), Buffer.from(ivText, 'base64'))
  132. let decrypted = decipher.update(payload, 'base64', 'utf8')
  133. decrypted += decipher.final('utf8')
  134. return decrypted
  135. }
  136. normalizeArray(value) {
  137. if (Array.isArray(value)) {
  138. return value.map(item => String(item).trim()).filter(Boolean)
  139. }
  140. if (typeof value === 'string') {
  141. const trimmed = value.trim()
  142. if (!trimmed) {
  143. return []
  144. }
  145. try {
  146. const parsed = JSON.parse(trimmed)
  147. if (Array.isArray(parsed)) {
  148. return parsed.map(item => String(item).trim()).filter(Boolean)
  149. }
  150. } catch (_) {
  151. return trimmed.split(/[\n,,]/).map(item => item.trim()).filter(Boolean)
  152. }
  153. }
  154. return []
  155. }
  156. serializeTask(row, includeSecret = false) {
  157. const result = { ...row }
  158. result.enable_ggxxk = Number(result.enable_ggxxk) === 1
  159. result.courses = this.normalizeArray(result.courses)
  160. result.course_groups = this.normalizeArray(result.course_groups)
  161. if (typeof result.result_json === 'string' && result.result_json) {
  162. try {
  163. result.result_json = JSON.parse(result.result_json)
  164. } catch (_) {}
  165. }
  166. if (includeSecret) {
  167. result.password = this.decryptPassword(result.password_enc)
  168. }
  169. delete result.password_enc
  170. return result
  171. }
  172. async logTask(taskId, clientId, event, message = '', payload = null) {
  173. const sql = 'INSERT INTO qk_task_log (task_id, client_id, event, message, payload_json, create_time) VALUES (?, ?, ?, ?, ?, ?)'
  174. await db.query(sql, [
  175. taskId,
  176. clientId || null,
  177. event,
  178. message || '',
  179. payload ? JSON.stringify(payload) : null,
  180. Date.now()
  181. ])
  182. this.logInfo('taskLog', message || event, { taskId, clientId }, payload ? { event, ...this.sanitizeForLog(payload) } : { event })
  183. }
  184. serializeReportLog(row) {
  185. const result = { ...row }
  186. if (typeof result.payload_json === 'string' && result.payload_json) {
  187. try {
  188. result.payload_json = JSON.parse(result.payload_json)
  189. } catch (_) {}
  190. }
  191. return result
  192. }
  193. buildReportMessage(payload = {}) {
  194. if (payload.message) {
  195. return String(payload.message)
  196. }
  197. if (payload.error_msg) {
  198. return String(payload.error_msg)
  199. }
  200. if (payload.error) {
  201. return String(payload.error)
  202. }
  203. if (payload.success === true) {
  204. return payload.label ? `${payload.label} 成功` : '请求成功'
  205. }
  206. return payload.label ? `${payload.label} 失败` : '请求失败'
  207. }
  208. async assertClientTaskAccess(clientId, taskId) {
  209. const rows = await db.query(
  210. 'SELECT id, status, assigned_client_id FROM qk_task WHERE id = ?',
  211. [taskId]
  212. )
  213. if (!rows || rows.length === 0) {
  214. throw new Error('任务不存在')
  215. }
  216. const task = rows[0]
  217. if (task.assigned_client_id !== clientId) {
  218. throw new Error('任务不属于当前客户端')
  219. }
  220. if (![TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING].includes(task.status)) {
  221. throw new Error('任务当前状态不可上报')
  222. }
  223. return task
  224. }
  225. async reportProgress(clientId, clientSecret, payload = {}) {
  226. const client = await this.authenticateClient(clientId, clientSecret)
  227. if (!client) {
  228. throw new Error('客户端凭证无效')
  229. }
  230. const taskId = Number(payload.task_id || payload.id)
  231. if (!taskId) {
  232. throw new Error('缺少任务 ID')
  233. }
  234. const event = String(payload.event || 'request_result')
  235. if (!REPORT_LOG_EVENTS.has(event) || event === 'grab_success' || event === 'grab_fail') {
  236. throw new Error('不支持的上报类型')
  237. }
  238. const task = await this.assertClientTaskAccess(clientId, taskId)
  239. const message = this.buildReportMessage(payload)
  240. const now = Date.now()
  241. await this.logTask(taskId, clientId, event, message, payload)
  242. const updates = ['update_time = ?']
  243. const params = [now]
  244. if (task.status === TASK_STATUS.ASSIGNED) {
  245. updates.push('status = ?')
  246. params.push(TASK_STATUS.RUNNING)
  247. }
  248. if (payload.success !== true && message) {
  249. updates.push('error_msg = ?')
  250. params.push(message)
  251. }
  252. params.push(taskId, clientId)
  253. await db.query(
  254. `UPDATE qk_task SET ${updates.join(', ')} WHERE id = ? AND assigned_client_id = ?`,
  255. params
  256. )
  257. this.logInfo('reportProgress', '客户端上报抢课进度', { taskId, clientId }, {
  258. event,
  259. success: payload.success === true,
  260. message
  261. })
  262. return { task_id: taskId, event, message }
  263. }
  264. async createTask(uuid, payload) {
  265. const courses = this.normalizeArray(payload.courses || payload.COURSES)
  266. const courseGroups = this.normalizeArray(payload.course_groups || payload.COURSE_GROUPS)
  267. const intervalMs = Number(payload.interval_ms || payload.INTERVAL_MS || 500)
  268. if (!courses.length && !courseGroups.length) {
  269. throw new Error('至少需要填写一门课程或一个课程分组')
  270. }
  271. if (!Number.isFinite(intervalMs) || intervalMs < 200 || intervalMs > 10000) {
  272. throw new Error('抢课间隔需在 200-10000ms 之间')
  273. }
  274. const time = Date.now()
  275. const sql = `INSERT INTO qk_task
  276. (create_user, name, jx0502zbid, student_num, password_enc, courses, course_groups, enable_ggxxk, interval_ms, status, create_time, update_time)
  277. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
  278. const result = await db.query(sql, [
  279. uuid,
  280. payload.name,
  281. payload.jx0502zbid || payload.id,
  282. payload.student_num || payload.user,
  283. this.encryptPassword(payload.password || payload.pass),
  284. JSON.stringify(courses),
  285. JSON.stringify(courseGroups),
  286. payload.enable_ggxxk || payload.ENABLE_GGXXK ? 1 : 0,
  287. intervalMs,
  288. TASK_STATUS.PENDING,
  289. time,
  290. time
  291. ])
  292. if (!result || result.affectedRows <= 0) {
  293. throw new Error('创建抢课任务失败')
  294. }
  295. await this.logTask(result.insertId, null, 'created', '用户提交抢课任务')
  296. this.logInfo('createTask', '抢课任务已创建', { taskId: result.insertId, uuid }, {
  297. name: payload.name,
  298. student_num: payload.student_num || payload.user,
  299. jx0502zbid: payload.jx0502zbid || payload.id,
  300. courses_count: courses.length,
  301. course_groups_count: courseGroups.length,
  302. interval_ms: intervalMs,
  303. enable_ggxxk: !!(payload.enable_ggxxk || payload.ENABLE_GGXXK)
  304. })
  305. return result.insertId
  306. }
  307. async updateTask(uuid, taskId, payload) {
  308. const rows = await db.query('SELECT status FROM qk_task WHERE id = ? AND create_user = ?', [taskId, uuid])
  309. if (!rows || rows.length === 0) {
  310. throw new Error('任务不存在')
  311. }
  312. if (![TASK_STATUS.PENDING, TASK_STATUS.FAILED, TASK_STATUS.CANCELLED].includes(rows[0].status)) {
  313. throw new Error('任务已被客户端领取,暂不能修改')
  314. }
  315. const courses = this.normalizeArray(payload.courses || payload.COURSES)
  316. const courseGroups = this.normalizeArray(payload.course_groups || payload.COURSE_GROUPS)
  317. const intervalMs = Number(payload.interval_ms || payload.INTERVAL_MS || 500)
  318. if (!courses.length && !courseGroups.length) {
  319. throw new Error('至少需要填写一门课程或一个课程分组')
  320. }
  321. const passwordSql = payload.password || payload.pass ? ', password_enc = ?' : ''
  322. const params = [
  323. payload.name,
  324. payload.jx0502zbid || payload.id,
  325. payload.student_num || payload.user,
  326. JSON.stringify(courses),
  327. JSON.stringify(courseGroups),
  328. payload.enable_ggxxk || payload.ENABLE_GGXXK ? 1 : 0,
  329. intervalMs,
  330. TASK_STATUS.PENDING,
  331. Date.now()
  332. ]
  333. if (passwordSql) {
  334. params.splice(3, 0, this.encryptPassword(payload.password || payload.pass))
  335. }
  336. params.push(taskId, uuid)
  337. const sql = `UPDATE qk_task SET name = ?, jx0502zbid = ?, student_num = ?${passwordSql}, courses = ?, course_groups = ?, enable_ggxxk = ?, interval_ms = ?, status = ?, update_time = ?, assigned_client_id = NULL, lease_expire_at = NULL, result_json = NULL, error_msg = NULL, finished_time = NULL WHERE id = ? AND create_user = ?`
  338. const result = await db.query(sql, params)
  339. if (!result || result.affectedRows <= 0) {
  340. throw new Error('更新抢课任务失败')
  341. }
  342. await this.logTask(taskId, null, 'updated', '用户更新抢课任务')
  343. this.logInfo('updateTask', '抢课任务已更新并重新进入待分配队列', { taskId, uuid }, {
  344. name: payload.name,
  345. student_num: payload.student_num || payload.user,
  346. courses_count: courses.length,
  347. course_groups_count: courseGroups.length,
  348. interval_ms: intervalMs,
  349. password_changed: !!(payload.password || payload.pass)
  350. })
  351. }
  352. async listUserTasks(uuid) {
  353. const rows = await db.query('SELECT * FROM qk_task WHERE create_user = ? ORDER BY create_time DESC', [uuid])
  354. return (rows || []).map(row => this.serializeTask(row))
  355. }
  356. async getTaskDetail(uuid, taskId) {
  357. const rows = await db.query('SELECT * FROM qk_task WHERE id = ? AND create_user = ?', [taskId, uuid])
  358. if (!rows || rows.length === 0) {
  359. return null
  360. }
  361. const logs = await db.query('SELECT client_id, event, message, payload_json, create_time FROM qk_task_log WHERE task_id = ? ORDER BY create_time DESC LIMIT 100', [taskId])
  362. return {
  363. task: this.serializeTask(rows[0]),
  364. logs: (logs || []).map(log => {
  365. if (typeof log.payload_json === 'string' && log.payload_json) {
  366. try {
  367. log.payload_json = JSON.parse(log.payload_json)
  368. } catch (_) {}
  369. }
  370. return log
  371. })
  372. }
  373. }
  374. async cancelTask(uuid, taskId) {
  375. const result = await db.query(
  376. 'UPDATE qk_task SET status = ?, update_time = ?, finished_time = ? WHERE id = ? AND create_user = ? AND status IN (?, ?)',
  377. [TASK_STATUS.CANCELLED, Date.now(), Date.now(), taskId, uuid, TASK_STATUS.PENDING, TASK_STATUS.FAILED]
  378. )
  379. if (!result || result.affectedRows <= 0) {
  380. throw new Error('任务不存在或当前状态不可取消')
  381. }
  382. await this.logTask(taskId, null, 'cancelled', '用户取消抢课任务')
  383. this.logInfo('cancelTask', '用户已取消抢课任务', { taskId, uuid })
  384. }
  385. async getAdminTask(taskId) {
  386. const rows = await db.query(
  387. `SELECT t.*, u.username, u.avatar
  388. FROM qk_task t
  389. LEFT JOIN users u ON u.uuid COLLATE utf8mb4_general_ci = t.create_user COLLATE utf8mb4_general_ci
  390. WHERE t.id = ?`,
  391. [taskId]
  392. )
  393. if (!rows || rows.length === 0) {
  394. return null
  395. }
  396. return this.serializeTask(rows[0], true)
  397. }
  398. async decrementClientSlots(clientId, now = Date.now()) {
  399. if (!clientId) return
  400. await db.query(
  401. 'UPDATE qk_client SET current_slots = IF(current_slots > 0, current_slots - 1, 0), update_time = ? WHERE client_id = ?',
  402. [now, clientId]
  403. )
  404. }
  405. async adminUpdateTask(taskId, payload) {
  406. const rows = await db.query('SELECT * FROM qk_task WHERE id = ?', [taskId])
  407. if (!rows || rows.length === 0) {
  408. throw new Error('任务不存在')
  409. }
  410. const task = rows[0]
  411. if (task.status === TASK_STATUS.SUCCESS) {
  412. throw new Error('已成功的任务不可编辑')
  413. }
  414. const courses = this.normalizeArray(payload.courses || payload.COURSES)
  415. const courseGroups = this.normalizeArray(payload.course_groups || payload.COURSE_GROUPS)
  416. const intervalMs = Number(payload.interval_ms || payload.INTERVAL_MS || task.interval_ms || 500)
  417. if (!courses.length && !courseGroups.length) {
  418. throw new Error('至少需要填写一门课程或一个课程分组')
  419. }
  420. if (!Number.isFinite(intervalMs) || intervalMs < 200 || intervalMs > 10000) {
  421. throw new Error('抢课间隔需在 200-10000ms 之间')
  422. }
  423. const now = Date.now()
  424. const wasAssigned = [TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING].includes(task.status)
  425. if (wasAssigned) {
  426. await this.decrementClientSlots(task.assigned_client_id, now)
  427. }
  428. const passwordSql = payload.password || payload.pass ? ', password_enc = ?' : ''
  429. const params = [
  430. payload.name,
  431. payload.jx0502zbid || payload.id,
  432. payload.student_num || payload.user,
  433. JSON.stringify(courses),
  434. JSON.stringify(courseGroups),
  435. payload.enable_ggxxk || payload.ENABLE_GGXXK ? 1 : 0,
  436. intervalMs,
  437. TASK_STATUS.PENDING,
  438. now
  439. ]
  440. if (passwordSql) {
  441. params.splice(3, 0, this.encryptPassword(payload.password || payload.pass))
  442. }
  443. params.push(taskId)
  444. const sql = `UPDATE qk_task SET name = ?, jx0502zbid = ?, student_num = ?${passwordSql}, courses = ?, course_groups = ?, enable_ggxxk = ?, interval_ms = ?, status = ?, update_time = ?, assigned_client_id = NULL, lease_expire_at = NULL, assigned_at = NULL, exclude_client_id = NULL, result_json = NULL, error_msg = NULL, finished_time = NULL WHERE id = ?`
  445. const result = await db.query(sql, params)
  446. if (!result || result.affectedRows <= 0) {
  447. throw new Error('更新抢课任务失败')
  448. }
  449. await this.logTask(taskId, task.assigned_client_id, 'admin_updated', wasAssigned ? '管理员更新任务并收回重新排队' : '管理员更新抢课任务')
  450. this.logInfo('adminUpdateTask', '管理员已更新抢课任务', { taskId }, {
  451. name: payload.name,
  452. student_num: payload.student_num || payload.user,
  453. released_from_client: wasAssigned ? task.assigned_client_id : null
  454. })
  455. }
  456. async adminCancelTask(taskId) {
  457. const rows = await db.query('SELECT id, status, assigned_client_id FROM qk_task WHERE id = ?', [taskId])
  458. if (!rows || rows.length === 0) {
  459. throw new Error('任务不存在')
  460. }
  461. const task = rows[0]
  462. if (![TASK_STATUS.PENDING, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING, TASK_STATUS.FAILED].includes(task.status)) {
  463. throw new Error('当前状态不可取消')
  464. }
  465. const now = Date.now()
  466. if ([TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING].includes(task.status)) {
  467. await this.decrementClientSlots(task.assigned_client_id, now)
  468. }
  469. const result = await db.query(
  470. `UPDATE qk_task SET status = ?, assigned_client_id = NULL, lease_expire_at = NULL, assigned_at = NULL,
  471. exclude_client_id = NULL, update_time = ?, finished_time = ? WHERE id = ? AND status IN (?, ?, ?, ?)`,
  472. [TASK_STATUS.CANCELLED, now, now, taskId, TASK_STATUS.PENDING, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING, TASK_STATUS.FAILED]
  473. )
  474. if (!result || result.affectedRows <= 0) {
  475. throw new Error('取消抢课任务失败')
  476. }
  477. await this.logTask(taskId, task.assigned_client_id, 'admin_cancelled', '管理员取消抢课任务')
  478. this.logInfo('adminCancelTask', '管理员已取消抢课任务', { taskId })
  479. }
  480. async adminRetryTask(taskId) {
  481. const rows = await db.query('SELECT id, status FROM qk_task WHERE id = ?', [taskId])
  482. if (!rows || rows.length === 0) {
  483. throw new Error('任务不存在')
  484. }
  485. if (![TASK_STATUS.FAILED, TASK_STATUS.CANCELLED].includes(rows[0].status)) {
  486. throw new Error('仅失败或已取消的任务可重试')
  487. }
  488. const now = Date.now()
  489. const result = await db.query(
  490. `UPDATE qk_task SET status = ?, assigned_client_id = NULL, lease_expire_at = NULL, assigned_at = NULL,
  491. exclude_client_id = NULL, result_json = NULL, error_msg = NULL, finished_time = NULL, update_time = ?
  492. WHERE id = ? AND status IN (?, ?)`,
  493. [TASK_STATUS.PENDING, now, taskId, TASK_STATUS.FAILED, TASK_STATUS.CANCELLED]
  494. )
  495. if (!result || result.affectedRows <= 0) {
  496. throw new Error('重试抢课任务失败')
  497. }
  498. await this.logTask(taskId, null, 'admin_retry', '管理员将任务重新加入待分配队列')
  499. this.logInfo('adminRetryTask', '管理员已重试抢课任务', { taskId })
  500. }
  501. async authenticateClient(clientId, clientSecret) {
  502. if (!clientId || !clientSecret) {
  503. this.logWarn('authClient', '客户端认证失败:缺少凭证', { clientId: clientId || 'unknown' })
  504. return null
  505. }
  506. const rows = await db.query('SELECT * FROM qk_client WHERE client_id = ? AND enabled = 1', [clientId])
  507. if (!rows || rows.length !== 1) {
  508. this.logWarn('authClient', '客户端认证失败:客户端不存在或已禁用', { clientId })
  509. return null
  510. }
  511. if (!bcryptjs.compareSync(String(clientSecret), rows[0].client_secret_hash)) {
  512. this.logWarn('authClient', '客户端认证失败:密钥不匹配', { clientId })
  513. return null
  514. }
  515. return rows[0]
  516. }
  517. async enrollOrAuthenticateClient(clientId, clientSecret, payload = {}) {
  518. const existing = await this.authenticateClient(clientId, clientSecret)
  519. if (existing) {
  520. return existing
  521. }
  522. if (!clientId || !clientSecret || !String(clientId).startsWith('qk-cli-')) {
  523. throw new Error('客户端凭证无效')
  524. }
  525. const rows = await db.query('SELECT * FROM qk_client WHERE client_id = ?', [clientId])
  526. if (rows && rows.length > 0) {
  527. throw new Error('客户端凭证无效')
  528. }
  529. const time = Date.now()
  530. const label = payload.label || payload.hostname || `auto-${clientId}`
  531. try {
  532. const result = await db.query(
  533. 'INSERT INTO qk_client (client_id, client_secret_hash, label, create_time, update_time) VALUES (?, ?, ?, ?, ?)',
  534. [clientId, bcryptjs.hashSync(String(clientSecret), 10), label, time, time]
  535. )
  536. if (!result || result.affectedRows <= 0) {
  537. throw new Error('客户端自动注册失败')
  538. }
  539. } catch (err) {
  540. if (err?.code === 'ER_DUP_ENTRY') {
  541. const raced = await this.authenticateClient(clientId, clientSecret)
  542. if (raced) {
  543. return raced
  544. }
  545. }
  546. throw err
  547. }
  548. const created = await db.query('SELECT * FROM qk_client WHERE client_id = ? AND enabled = 1', [clientId])
  549. if (!created || created.length !== 1) {
  550. throw new Error('客户端自动注册失败')
  551. }
  552. this.logInfo('enrollClient', '抢课客户端已自动注册', { clientId }, { label })
  553. return created[0]
  554. }
  555. async registerClient(clientId, clientSecret, payload = {}) {
  556. const client = await this.enrollOrAuthenticateClient(clientId, clientSecret, payload)
  557. const maxSlots = this.resolveClientMaxSlots(client, payload)
  558. const currentSlots = Math.max(0, Number(payload.current_slots || 0))
  559. const time = Date.now()
  560. await db.query(
  561. 'UPDATE qk_client SET label = COALESCE(NULLIF(?, \'\'), label), max_slots = ?, current_slots = ?, hostname = ?, os_username = ?, cpu_model = ?, cpu_threads = ?, total_mem_mb = ?, free_mem_mb = ?, platform = ?, last_heartbeat_at = ?, online = 1, update_time = ? WHERE client_id = ?',
  562. [
  563. payload.label || '',
  564. maxSlots,
  565. currentSlots,
  566. payload.hostname || null,
  567. payload.os_username || null,
  568. payload.cpu_model || null,
  569. payload.cpu_threads || null,
  570. payload.total_mem_mb || null,
  571. payload.free_mem_mb || null,
  572. payload.platform || null,
  573. time,
  574. time,
  575. clientId
  576. ]
  577. )
  578. await Redis.set(`qk:client:hb:${clientId}`, String(time), { EX: this.heartbeatTtlSeconds })
  579. this.logInfo('registerClient', '抢课客户端已注册/上线', { clientId }, {
  580. label: payload.label || client.label,
  581. max_slots: maxSlots,
  582. current_slots: currentSlots,
  583. hostname: payload.hostname,
  584. platform: payload.platform,
  585. cpu_threads: payload.cpu_threads,
  586. total_mem_mb: payload.total_mem_mb,
  587. free_mem_mb: payload.free_mem_mb
  588. })
  589. return { client_id: clientId, max_slots: maxSlots, current_slots: currentSlots }
  590. }
  591. async heartbeat(clientId, clientSecret, payload = {}) {
  592. const client = await this.authenticateClient(clientId, clientSecret)
  593. if (!client) {
  594. throw new Error('客户端凭证无效')
  595. }
  596. const runningTasks = Array.isArray(payload.running_tasks) ? payload.running_tasks.map(Number).filter(Boolean) : []
  597. const currentSlots = Math.max(0, Number(payload.current_slots ?? runningTasks.length))
  598. const maxSlots = this.resolveClientMaxSlots(client, payload)
  599. const now = Date.now()
  600. await db.query(
  601. 'UPDATE qk_client SET max_slots = ?, current_slots = ?, hostname = ?, os_username = ?, cpu_model = ?, cpu_threads = ?, total_mem_mb = ?, free_mem_mb = ?, platform = ?, last_heartbeat_at = ?, online = 1, update_time = ? WHERE client_id = ?',
  602. [
  603. maxSlots,
  604. currentSlots,
  605. payload.hostname || null,
  606. payload.os_username || null,
  607. payload.cpu_model || null,
  608. payload.cpu_threads || null,
  609. payload.total_mem_mb || null,
  610. payload.free_mem_mb || null,
  611. payload.platform || null,
  612. now,
  613. now,
  614. clientId
  615. ]
  616. )
  617. await Redis.set(`qk:client:hb:${clientId}`, String(now), { EX: this.heartbeatTtlSeconds })
  618. await Redis.set(`qk:client:slots:${clientId}`, String(currentSlots), { EX: this.heartbeatTtlSeconds })
  619. if (runningTasks.length > 0) {
  620. const leaseExpireAt = now + this.leaseMs
  621. const placeholders = runningTasks.map(() => '?').join(',')
  622. await db.query(
  623. `UPDATE qk_task SET status = ?, lease_expire_at = ?, update_time = ? WHERE assigned_client_id = ? AND id IN (${placeholders}) AND status IN (?, ?)`,
  624. [TASK_STATUS.RUNNING, leaseExpireAt, now, clientId, ...runningTasks, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING]
  625. )
  626. this.logInfo('heartbeat', '客户端心跳续租运行中任务', { clientId }, {
  627. running_tasks: runningTasks,
  628. lease_expire_at: leaseExpireAt,
  629. current_slots: currentSlots,
  630. max_slots: maxSlots
  631. })
  632. }
  633. await this.releaseOrphanedClientTasks(clientId, runningTasks, now)
  634. return { client_id: clientId, current_slots: currentSlots, max_slots: maxSlots }
  635. }
  636. async releaseOrphanedClientTasks(clientId, runningTasks, now = Date.now()) {
  637. const assigned = await db.query(
  638. 'SELECT id FROM qk_task WHERE assigned_client_id = ? AND status IN (?, ?)',
  639. [clientId, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING]
  640. )
  641. const runningSet = new Set((runningTasks || []).map(Number).filter(Boolean))
  642. let released = 0
  643. for (const row of assigned || []) {
  644. if (runningSet.has(row.id)) continue
  645. const result = await db.query(
  646. 'UPDATE qk_task SET status = ?, assigned_client_id = NULL, lease_expire_at = NULL, assigned_at = NULL, update_time = ? WHERE id = ? AND assigned_client_id = ? AND status IN (?, ?)',
  647. [TASK_STATUS.PENDING, now, row.id, clientId, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING]
  648. )
  649. if (result && result.affectedRows > 0) {
  650. released += 1
  651. await this.logTask(row.id, clientId, 'released', '客户端未继续执行,任务已释放回队列')
  652. this.logInfo('releaseOrphaned', '释放未在运行的已分配任务', { taskId: row.id, clientId })
  653. }
  654. }
  655. return released
  656. }
  657. async reclaimTasks(clientId, clientSecret) {
  658. const client = await this.authenticateClient(clientId, clientSecret)
  659. if (!client) {
  660. throw new Error('客户端凭证无效')
  661. }
  662. const now = Date.now()
  663. const leaseExpireAt = now + this.leaseMs
  664. const rows = await db.query(
  665. 'SELECT * FROM qk_task WHERE assigned_client_id = ? AND status IN (?, ?) ORDER BY create_time ASC',
  666. [clientId, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING]
  667. )
  668. if (!rows || rows.length === 0) {
  669. return []
  670. }
  671. for (const row of rows) {
  672. await db.query(
  673. 'UPDATE qk_task SET status = ?, lease_expire_at = ?, update_time = ? WHERE id = ? AND assigned_client_id = ? AND status IN (?, ?)',
  674. [TASK_STATUS.RUNNING, leaseExpireAt, now, row.id, clientId, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING]
  675. )
  676. await this.logTask(row.id, clientId, 'reclaimed', '客户端重连后回收任务继续执行')
  677. }
  678. this.logInfo('reclaimTasks', `客户端回收 ${rows.length} 个进行中任务`, { clientId }, {
  679. task_ids: rows.map(row => row.id),
  680. lease_expire_at: leaseExpireAt
  681. })
  682. return rows.map(row => this.serializeTask({
  683. ...row,
  684. assigned_client_id: clientId,
  685. lease_expire_at: leaseExpireAt,
  686. status: TASK_STATUS.RUNNING
  687. }, true))
  688. }
  689. async releaseTasks(clientId, clientSecret, payload = {}) {
  690. const client = await this.authenticateClient(clientId, clientSecret)
  691. if (!client) {
  692. throw new Error('客户端凭证无效')
  693. }
  694. const now = Date.now()
  695. const taskIds = Array.isArray(payload.task_ids)
  696. ? payload.task_ids.map(Number).filter(Boolean)
  697. : []
  698. let rows = []
  699. if (taskIds.length > 0) {
  700. const placeholders = taskIds.map(() => '?').join(',')
  701. rows = await db.query(
  702. `SELECT id FROM qk_task WHERE assigned_client_id = ? AND id IN (${placeholders}) AND status IN (?, ?)`,
  703. [clientId, ...taskIds, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING]
  704. )
  705. } else {
  706. rows = await db.query(
  707. 'SELECT id FROM qk_task WHERE assigned_client_id = ? AND status IN (?, ?)',
  708. [clientId, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING]
  709. )
  710. }
  711. let released = 0
  712. for (const row of rows || []) {
  713. const result = await db.query(
  714. 'UPDATE qk_task SET status = ?, assigned_client_id = NULL, lease_expire_at = NULL, assigned_at = NULL, update_time = ? WHERE id = ? AND assigned_client_id = ? AND status IN (?, ?)',
  715. [TASK_STATUS.PENDING, now, row.id, clientId, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING]
  716. )
  717. if (result && result.affectedRows > 0) {
  718. released += 1
  719. await this.logTask(row.id, clientId, 'released', '客户端主动释放任务')
  720. }
  721. }
  722. if (released > 0) {
  723. this.logInfo('releaseTasks', `客户端释放 ${released} 个任务`, { clientId }, {
  724. task_ids: (rows || []).map(row => row.id)
  725. })
  726. }
  727. return { released, task_ids: (rows || []).map(row => row.id) }
  728. }
  729. async pullTasks(clientId, clientSecret, count) {
  730. const client = await this.authenticateClient(clientId, clientSecret)
  731. if (!client) {
  732. throw new Error('客户端凭证无效')
  733. }
  734. const availableSlots = this.getClientAvailableSlots(client)
  735. const safeCount = Math.max(0, Math.min(50, Number(count || 0), availableSlots))
  736. if (safeCount <= 0) {
  737. this.logWarn('pullTasks', '客户端无可用槽位或拉取数量无效,已忽略', { clientId }, {
  738. count,
  739. available_slots: availableSlots,
  740. max_slots: this.resolveClientMaxSlots(client),
  741. current_slots: client.current_slots,
  742. free_mem_mb: client.free_mem_mb
  743. })
  744. return []
  745. }
  746. const lockKey = `qk:pull:lock:${clientId}`
  747. const locked = await Redis.set(lockKey, '1', { NX: true, EX: this.pullLockTtlSeconds })
  748. if (!locked) {
  749. this.logWarn('pullTasks', '拉取任务被并发锁拦截,本次跳过', { clientId }, { count: safeCount })
  750. return []
  751. }
  752. const conn = await db.connect()
  753. try {
  754. await conn.beginTransaction()
  755. const [rows] = await conn.execute(
  756. `SELECT * FROM qk_task
  757. WHERE status = ?
  758. AND (exclude_client_id IS NULL OR exclude_client_id <> ?)
  759. ORDER BY create_time ASC LIMIT ${safeCount} FOR UPDATE`,
  760. [TASK_STATUS.PENDING, clientId]
  761. )
  762. const now = Date.now()
  763. const leaseExpireAt = now + this.leaseMs
  764. for (const row of rows) {
  765. await conn.execute(
  766. 'UPDATE qk_task SET status = ?, assigned_client_id = ?, lease_expire_at = ?, assigned_at = ?, exclude_client_id = NULL, update_time = ? WHERE id = ?',
  767. [TASK_STATUS.ASSIGNED, clientId, leaseExpireAt, now, now, row.id]
  768. )
  769. }
  770. await conn.commit()
  771. if (rows.length > 0) {
  772. await db.query(
  773. 'UPDATE qk_client SET current_slots = current_slots + ?, update_time = ? WHERE client_id = ?',
  774. [rows.length, now, clientId]
  775. )
  776. }
  777. for (const row of rows) {
  778. await this.logTask(row.id, clientId, 'assigned', '任务已分配给客户端')
  779. }
  780. if (rows.length > 0) {
  781. this.logInfo('pullTasks', `已分配 ${rows.length} 个抢课任务`, { clientId }, {
  782. task_ids: rows.map(row => row.id),
  783. lease_expire_at: leaseExpireAt,
  784. requested_count: safeCount
  785. })
  786. }
  787. return rows.map(row => this.serializeTask({ ...row, assigned_client_id: clientId, lease_expire_at: leaseExpireAt, status: TASK_STATUS.ASSIGNED }, true))
  788. } catch (err) {
  789. await conn.rollback()
  790. this.logError('pullTasks', '拉取并分配任务失败,事务已回滚', { clientId }, err)
  791. throw err
  792. } finally {
  793. await Redis.del(lockKey)
  794. }
  795. }
  796. async reportResult(clientId, clientSecret, payload = {}) {
  797. const client = await this.authenticateClient(clientId, clientSecret)
  798. if (!client) {
  799. throw new Error('客户端凭证无效')
  800. }
  801. const taskId = Number(payload.task_id || payload.id)
  802. const success = payload.success === true || payload.status === TASK_STATUS.SUCCESS
  803. const status = success ? TASK_STATUS.SUCCESS : TASK_STATUS.FAILED
  804. const now = Date.now()
  805. const resultJson = payload.result ? JSON.stringify(payload.result) : JSON.stringify({
  806. course: payload.course || '',
  807. message: payload.message || ''
  808. })
  809. const result = await db.query(
  810. 'UPDATE qk_task SET status = ?, result_json = ?, error_msg = ?, update_time = ?, finished_time = ?, lease_expire_at = NULL WHERE id = ? AND assigned_client_id = ? AND status IN (?, ?)',
  811. [status, resultJson, success ? null : (payload.error_msg || payload.message || '抢课失败'), now, now, taskId, clientId, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING]
  812. )
  813. if (!result || result.affectedRows <= 0) {
  814. this.logWarn('reportResult', '任务结果上报被拒绝:任务不存在或不属于当前客户端', { taskId, clientId }, {
  815. success,
  816. status
  817. })
  818. throw new Error('任务不存在或不属于当前客户端')
  819. }
  820. await db.query(
  821. `UPDATE qk_client SET current_slots = IF(current_slots > 0, current_slots - 1, 0), total_completed = total_completed + 1, total_success = total_success + ?, update_time = ? WHERE client_id = ?`,
  822. [success ? 1 : 0, now, clientId]
  823. )
  824. await this.logTask(taskId, clientId, success ? 'grab_success' : 'grab_fail', payload.message || payload.error_msg || '', payload.result || payload)
  825. this.logInfo('reportResult', success ? '抢课成功' : '抢课失败', { taskId, clientId }, {
  826. success,
  827. message: payload.message || payload.error_msg || '',
  828. course: payload.course || payload.result?.course || '',
  829. result: payload.result || null
  830. })
  831. return { task_id: taskId, status }
  832. }
  833. async listClients() {
  834. const rows = await db.query('SELECT id, client_id, label, enabled, max_slots, current_slots, hostname, os_username, cpu_model, cpu_threads, total_mem_mb, free_mem_mb, platform, last_heartbeat_at, online, total_completed, total_success, create_time, update_time FROM qk_client ORDER BY update_time DESC')
  835. return rows || []
  836. }
  837. async deleteClient(clientId) {
  838. const result = await db.query('UPDATE qk_client SET enabled = 0, online = 0, update_time = ? WHERE client_id = ?', [Date.now(), clientId])
  839. if (!result || result.affectedRows <= 0) {
  840. throw new Error('客户端不存在')
  841. }
  842. this.logInfo('deleteClient', '抢课客户端已禁用', { clientId })
  843. }
  844. async listAdminTasks(filters = {}) {
  845. const pagesize = Math.max(1, Math.min(100, Number(filters.pagesize || 20)))
  846. const current = Math.max(1, Number(filters.current || 1))
  847. const where = ['1 = 1']
  848. const params = []
  849. const countParams = []
  850. if (filters.status) {
  851. where.push('t.status = ?')
  852. params.push(filters.status)
  853. countParams.push(filters.status)
  854. }
  855. if (filters.client_id) {
  856. where.push('t.assigned_client_id = ?')
  857. params.push(filters.client_id)
  858. countParams.push(filters.client_id)
  859. }
  860. if (filters.student_num) {
  861. where.push('t.student_num LIKE ?')
  862. params.push(`%${filters.student_num}%`)
  863. countParams.push(`%${filters.student_num}%`)
  864. }
  865. if (filters.username) {
  866. where.push('u.username COLLATE utf8mb4_general_ci LIKE (CONVERT(? USING utf8mb4) COLLATE utf8mb4_general_ci)')
  867. params.push(`%${filters.username}%`)
  868. countParams.push(`%${filters.username}%`)
  869. }
  870. const whereSql = where.join(' AND ')
  871. const offset = (current - 1) * pagesize
  872. const countRows = await db.query(
  873. `SELECT COUNT(*) AS total FROM qk_task t
  874. LEFT JOIN users u ON u.uuid COLLATE utf8mb4_general_ci = t.create_user COLLATE utf8mb4_general_ci
  875. WHERE ${whereSql}`,
  876. countParams
  877. )
  878. const rows = await db.query(
  879. `SELECT t.*, u.username, u.avatar
  880. FROM qk_task t
  881. LEFT JOIN users u ON u.uuid COLLATE utf8mb4_general_ci = t.create_user COLLATE utf8mb4_general_ci
  882. WHERE ${whereSql}
  883. ORDER BY t.create_time DESC
  884. LIMIT ${pagesize} OFFSET ${offset}`,
  885. params
  886. )
  887. return {
  888. list: (rows || []).map(row => this.serializeTask(row, true)),
  889. total: countRows?.[0]?.total || 0,
  890. current,
  891. pagesize
  892. }
  893. }
  894. async listAdminReports(filters = {}) {
  895. const pagesize = Math.max(1, Math.min(100, Number(filters.pagesize || 20)))
  896. const current = Math.max(1, Number(filters.current || 1))
  897. const where = ['l.event IN (?, ?, ?, ?)']
  898. const params = ['request_result', 'progress_snapshot', 'grab_success', 'grab_fail']
  899. const countParams = ['request_result', 'progress_snapshot', 'grab_success', 'grab_fail']
  900. if (filters.task_id) {
  901. where.push('l.task_id = ?')
  902. params.push(Number(filters.task_id))
  903. countParams.push(Number(filters.task_id))
  904. }
  905. if (filters.client_id) {
  906. where.push('l.client_id = ?')
  907. params.push(String(filters.client_id))
  908. countParams.push(String(filters.client_id))
  909. }
  910. if (filters.event) {
  911. where.push('l.event = ?')
  912. params.push(String(filters.event))
  913. countParams.push(String(filters.event))
  914. }
  915. if (filters.start_time) {
  916. where.push('l.create_time >= ?')
  917. params.push(Number(filters.start_time))
  918. countParams.push(Number(filters.start_time))
  919. }
  920. if (filters.end_time) {
  921. where.push('l.create_time <= ?')
  922. params.push(Number(filters.end_time))
  923. countParams.push(Number(filters.end_time))
  924. }
  925. if (filters.student_num) {
  926. where.push('t.student_num LIKE ?')
  927. params.push(`%${filters.student_num}%`)
  928. countParams.push(`%${filters.student_num}%`)
  929. }
  930. if (filters.name || filters.task_name) {
  931. where.push('t.name LIKE ?')
  932. params.push(`%${filters.name || filters.task_name}%`)
  933. countParams.push(`%${filters.name || filters.task_name}%`)
  934. }
  935. if (filters.username) {
  936. where.push('u.username COLLATE utf8mb4_general_ci LIKE (CONVERT(? USING utf8mb4) COLLATE utf8mb4_general_ci)')
  937. params.push(`%${filters.username}%`)
  938. countParams.push(`%${filters.username}%`)
  939. }
  940. if (filters.client_label) {
  941. where.push('c.label LIKE ?')
  942. params.push(`%${filters.client_label}%`)
  943. countParams.push(`%${filters.client_label}%`)
  944. }
  945. const whereSql = where.join(' AND ')
  946. const offset = (current - 1) * pagesize
  947. const joinSql = `
  948. FROM qk_task_log l
  949. LEFT JOIN qk_task t ON t.id = l.task_id
  950. LEFT JOIN qk_client c ON c.client_id = l.client_id
  951. LEFT JOIN users u ON u.uuid COLLATE utf8mb4_general_ci = t.create_user COLLATE utf8mb4_general_ci`
  952. const countRows = await db.query(
  953. `SELECT COUNT(*) AS total ${joinSql} WHERE ${whereSql}`,
  954. countParams
  955. )
  956. const rows = await db.query(
  957. `SELECT l.id, l.task_id, l.client_id, l.event, l.message, l.payload_json, l.create_time,
  958. t.name AS task_name, t.student_num, t.status AS task_status,
  959. c.label AS client_label, u.username, u.avatar
  960. ${joinSql}
  961. WHERE ${whereSql}
  962. ORDER BY l.create_time DESC
  963. LIMIT ${pagesize} OFFSET ${offset}`,
  964. params
  965. )
  966. return {
  967. list: (rows || []).map(row => this.serializeReportLog(row)),
  968. total: countRows?.[0]?.total || 0,
  969. current,
  970. pagesize
  971. }
  972. }
  973. async reassignStaleRunningTasks() {
  974. const onlineCount = await this.countOnlineClients()
  975. if (onlineCount <= 1) {
  976. return 0
  977. }
  978. const now = Date.now()
  979. const staleBefore = now - this.staleTaskMs
  980. const rows = await db.query(
  981. `SELECT id, assigned_client_id FROM qk_task
  982. WHERE status IN (?, ?)
  983. AND assigned_at IS NOT NULL
  984. AND assigned_at < ?`,
  985. [TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING, staleBefore]
  986. )
  987. let count = 0
  988. for (const row of rows || []) {
  989. const result = await db.query(
  990. `UPDATE qk_task
  991. SET status = ?, assigned_client_id = NULL, lease_expire_at = NULL,
  992. assigned_at = NULL, exclude_client_id = ?, update_time = ?
  993. WHERE id = ? AND status IN (?, ?)`,
  994. [TASK_STATUS.PENDING, row.assigned_client_id, now, row.id, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING]
  995. )
  996. if (result && result.affectedRows > 0) {
  997. count += 1
  998. if (row.assigned_client_id) {
  999. await db.query(
  1000. 'UPDATE qk_client SET current_slots = IF(current_slots > 0, current_slots - 1, 0), update_time = ? WHERE client_id = ?',
  1001. [now, row.assigned_client_id]
  1002. )
  1003. }
  1004. await this.logTask(row.id, row.assigned_client_id, 'reassigned', '任务超过1小时未成功,已收回并等待分配给其他客户端')
  1005. this.logWarn('reassignStale', '长时间未成功任务已收回', {
  1006. taskId: row.id,
  1007. clientId: row.assigned_client_id
  1008. }, { online_clients: onlineCount })
  1009. }
  1010. }
  1011. return count
  1012. }
  1013. async requeueExpiredTasks() {
  1014. const now = Date.now()
  1015. const rows = await db.query(
  1016. 'SELECT id, assigned_client_id FROM qk_task WHERE status IN (?, ?) AND lease_expire_at IS NOT NULL AND lease_expire_at < ?',
  1017. [TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING, now]
  1018. )
  1019. let count = 0
  1020. for (const row of rows || []) {
  1021. const result = await db.query(
  1022. 'UPDATE qk_task SET status = ?, assigned_client_id = NULL, lease_expire_at = NULL, assigned_at = NULL, update_time = ? WHERE id = ? AND status IN (?, ?)',
  1023. [TASK_STATUS.PENDING, now, row.id, TASK_STATUS.ASSIGNED, TASK_STATUS.RUNNING]
  1024. )
  1025. if (result && result.affectedRows > 0) {
  1026. count++
  1027. if (row.assigned_client_id) {
  1028. await db.query(
  1029. 'UPDATE qk_client SET current_slots = IF(current_slots > 0, current_slots - 1, 0), update_time = ? WHERE client_id = ?',
  1030. [now, row.assigned_client_id]
  1031. )
  1032. }
  1033. await this.logTask(row.id, row.assigned_client_id, 'reassigned', '租约过期,任务已重新进入待分配队列')
  1034. this.logWarn('requeueExpired', '租约过期,任务已重新入队', {
  1035. taskId: row.id,
  1036. clientId: row.assigned_client_id
  1037. })
  1038. }
  1039. }
  1040. const offlineResult = await db.query(
  1041. 'UPDATE qk_client SET online = 0, current_slots = 0, update_time = ? WHERE online = 1 AND (last_heartbeat_at IS NULL OR last_heartbeat_at < ?)',
  1042. [now, now - this.heartbeatTtlSeconds * 1000]
  1043. )
  1044. const offlineCount = offlineResult?.affectedRows || 0
  1045. const staleCount = await this.reassignStaleRunningTasks()
  1046. if (count > 0 || offlineCount > 0 || staleCount > 0) {
  1047. this.logInfo('requeueExpired', '租约巡检完成', {}, {
  1048. expired_tasks: (rows || []).length,
  1049. requeued: count,
  1050. stale_reassigned: staleCount,
  1051. clients_marked_offline: offlineCount
  1052. })
  1053. }
  1054. return count + staleCount
  1055. }
  1056. }
  1057. module.exports = {
  1058. TaskScheduler,
  1059. TASK_STATUS
  1060. }