TaskScheduler.js 62 KB

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