run-process.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env node
  2. /**
  3. * run-process.js
  4. * 接收两个参数:ip 数组 (JSON)、脚本名
  5. * 异步根据每个 ip 执行脚本;执行前先 adb connect 该设备,确保连接成功再跑流程。
  6. *
  7. * 调用示例:node run-process.js '["192.168.2.5","192.168.2.6"]' 'RedNoteAIThumbsUp'
  8. */
  9. const path = require('path')
  10. const fs = require('fs')
  11. const { execSync } = require('child_process')
  12. const projectRoot = path.resolve(path.join(__dirname, '..'))
  13. const staticRoot = process.env.STATIC_ROOT ? path.resolve(process.env.STATIC_ROOT) : path.join(projectRoot, 'static')
  14. const config = require(path.join(projectRoot, 'configs', 'config.js'))
  15. const adbPath = config.adbPath?.path
  16. ? path.resolve(projectRoot, config.adbPath.path)
  17. : path.join(projectRoot, 'lib', 'scrcpy-adb', 'adb.exe')
  18. const ADB_PORT = 5555
  19. const ipListJson = process.argv[2]
  20. const scriptName = process.argv[3]
  21. const folderPath = path.resolve(path.join(staticRoot, 'process', scriptName))
  22. const logFilePath = path.join(folderPath, 'log.txt')
  23. const ipList = JSON.parse(ipListJson)
  24. let shouldStop = false
  25. function appendLog(text) {
  26. try {
  27. fs.appendFileSync(logFilePath, text, 'utf8')
  28. } catch (e) {}
  29. }
  30. function logLine(msg) {
  31. const line = `[${new Date().toISOString()}] ${msg}\n`
  32. process.stdout.write(line)
  33. }
  34. const { parseWorkflow } = require('./ef-compiler/ef-compiler.js')
  35. const actions = parseWorkflow(JSON.parse(fs.readFileSync(path.join(folderPath, 'process.json'), 'utf8'))).actions
  36. const { executeActionSequence } = require('./ef-compiler/ef-compiler.js')
  37. const resolution = { width: 1080, height: 1920 }
  38. function sleep(ms) {
  39. return new Promise((resolve) => setTimeout(resolve, ms))
  40. }
  41. /** 对指定 IP 执行 adb connect,与 bat-tool/adb-connect-test 行为一致:可重试、短延迟,确保设备就绪 */
  42. async function ensureDeviceConnected(ip, port, logLineFn) {
  43. const deviceId = `${ip}:${port}`
  44. const maxTries = 3
  45. const delayMs = 2000
  46. for (let i = 0; i < maxTries; i++) {
  47. try {
  48. const out = execSync(`"${adbPath}" connect ${deviceId}`, { encoding: 'utf-8' }).trim()
  49. if (out.includes('connected') || out.includes('already connected')) return true
  50. } catch (e) {
  51. // execSync 抛错(如 adb 未找到)时不再重试
  52. if (logLineFn) logLineFn(`Connect attempt ${i + 1}/${maxTries}: ${(e.stderr || e.message || '').trim() || 'failed'}`)
  53. }
  54. if (i < maxTries - 1) {
  55. if (logLineFn) logLineFn(`Wait ${delayMs / 1000}s, retry connect ${deviceId} ...`)
  56. await sleep(delayMs)
  57. }
  58. }
  59. return false
  60. }
  61. /** 启动执行:遍历 ip 列表并异步执行脚本;任一台失败则停止全部并返回失败设备 IP;log.txt 仅写入报错与带 log:true 的 echo */
  62. async function start() {
  63. logLine(`Process "${scriptName}" start, devices: ${ipList.length}`)
  64. let failedIp = null
  65. const runOne = async (ip) => {
  66. if (shouldStop) return { ip, success: false, stopped: true }
  67. const deviceId = ip.includes(':') ? ip : `${ip}:${ADB_PORT}`
  68. const [deviceIp, devicePort] = deviceId.includes(':') ? deviceId.split(':') : [ip, ADB_PORT]
  69. logLine(`Connecting ${deviceId} ...`)
  70. const connected = await ensureDeviceConnected(deviceIp, devicePort, logLine)
  71. if (!connected) {
  72. logLine(`Failed to connect ${deviceId}`)
  73. return { ip, success: false }
  74. }
  75. logLine(`Running on ${deviceId}`)
  76. const result = await executeActionSequence(actions, deviceId, folderPath, resolution, 1000, null, () => shouldStop)
  77. if (!result.success) {
  78. if (!failedIp) { failedIp = ip; shouldStop = true }
  79. }
  80. return { ip, success: result.success }
  81. }
  82. const results = await Promise.all(ipList.map(ip => runOne(ip)))
  83. const output = failedIp
  84. ? { success: false, failedIp, results }
  85. : { success: true, results }
  86. logLine(failedIp ? `Process finished with failed device: ${failedIp}` : 'Process finished successfully.')
  87. const resultLine = JSON.stringify(output) + '\n'
  88. process.stdout.write(resultLine)
  89. process.exit(failedIp ? 1 : 0)
  90. }
  91. /** 停止执行(SIGTERM/SIGINT 时调用) */
  92. function stop() {
  93. shouldStop = true
  94. }
  95. process.on('SIGTERM', () => { stop(); process.exit(130) })
  96. process.on('SIGINT', () => { stop(); process.exit(130) })
  97. start()