dsh-qianchuan-toolkit 1.0.8 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # 更新日志
2
2
 
3
+ ## 1.0.9
4
+
5
+ - **巡航单实例锁(安全)**:`hourly.mjs --watch` 启动时写 `<QC_HOME>/watch.pid`,发现已有存活的巡航进程就直接退出(code 4)并提示,避免「开机自启的巡航」和「手动启动的巡航」同时提交造成重复控量。进程退出时自动清理锁文件;确认旧进程已死可手工删除该文件。
6
+ - **每日窗口守夜(新)**:新增 `assets/live-control/scripts/daily-supervisor.mjs` —— 每天 `07:00` 开始、次日 `01:00` 收工:窗口内拉起 `hourly.mjs --watch --until 01:00`,窗口外睡到下一个开始时间(**不刷新页面、不读数据、不提交动作**);巡航非 0 退出且仍在窗口内则 5 分钟后重拉。`--plan` 只打印当前判定;`--start / --end` 自定义窗口;`QC_HOME / QC_HOURLY` 可覆盖数据目录与脚本路径。用 `supervisor.pid` 记账。
7
+ - **收尾轮**:原来最后一个节点(:55)之后若下一个节点已越过收工时间就直接退出,实际常常 `00:55` 就收工。现在若离收工还有 > 90 秒,会在**收工前 45 秒补跑一轮**,窗口真正跑到 `01:00`。
8
+ - **开机自启模板随包发布**:`assets/live-control/autostart/` 提供 `start-cruise.cmd` / `stop-cruise.cmd` / `qc-cruise-autostart.vbs`(登录自启:先确保调试 Chrome 9222,再起守夜;`QC_BASE` 指定插件目录,`QC_NODE` 指定 node)。`stop-cruise.cmd` 先杀守夜再杀巡航,避免守夜 5 分钟后又把巡航拉起来。
9
+ - 守夜对**正常退出(退出码 0)**不再记成「异常退出」:窗口内正常退出的等待 60 秒再起,已到收工点的直接睡到下一个开始时间。
10
+
3
11
  ## 1.0.8
4
12
 
5
13
  - **判定顺序修正(重要)**:原来看到行上不是「+」(⏸ 或找不到一键调速组)就先去点弹窗、查日志、报异常,**没有先算这小时该不该控**。现在固定为「刷新 → 读数据 → 先算要不要动」:未在投 / 没花钱 / 小时消耗未超过 `minCost` / ROI 达标一律就此结束,绝不为「看看是不是在控量」去点弹窗查日志。修掉 2026-11-20 16:15 那轮「消耗仅 83.17 元(门槛 200)却排查后报异常轮空」。
package/README.md CHANGED
@@ -95,3 +95,15 @@ docs/notify.sample.json 通知配置示例
95
95
  ## 许可
96
96
 
97
97
  MIT
98
+
99
+ ## 每日窗口与开机自启(可选)
100
+
101
+ `assets/live-control/scripts/daily-supervisor.mjs` 负责「每天 07:00 开始、次日 01:00 收工」的循环:
102
+
103
+ ```
104
+ QC_HOME="<数据目录>" node assets/live-control/scripts/daily-supervisor.mjs # 常驻守夜
105
+ QC_HOME="<数据目录>" node assets/live-control/scripts/daily-supervisor.mjs --plan # 只看当前是否在窗口内
106
+ node assets/live-control/scripts/daily-supervisor.mjs --start 08:00 --end 23:30 # 自定义窗口
107
+ ```
108
+
109
+ 开机自启模板在 `assets/live-control/autostart/`:`qc-cruise-autostart.vbs`(登录后确保调试 Chrome 再起守夜,需 `QC_BASE` 指向本包目录)、`start-cruise.cmd`、`stop-cruise.cmd`(先杀守夜再杀巡航)。详见该目录 README。
@@ -0,0 +1,50 @@
1
+ ' Qianchuan cruise autostart (login) - shipped with dsh-qianchuan-toolkit
2
+ ' Requires QC_BASE = the plugin checkout root that contains assets\live-control\scripts\start-qc.cmd
3
+ ' 1) open the debug Chrome (port 9222) 2) wait for the port 3) start the cruise supervisor
4
+ Dim sh, fso, base, startupCmd, cruiseCmd, delayMs, env
5
+ Set sh = CreateObject("WScript.Shell")
6
+ Set fso = CreateObject("Scripting.FileSystemObject")
7
+ Set env = sh.Environment("PROCESS")
8
+ base = env("QC_BASE")
9
+ If Len(base) = 0 Then WScript.Quit 9
10
+ startupCmd = base & "\assets\live-control\scripts\start-qc.cmd"
11
+ cruiseCmd = base & "\assets\live-control\autostart\start-cruise.cmd"
12
+
13
+ delayMs = 45000
14
+ If Len(env("QC_START_DELAY")) > 0 Then delayMs = CLng(env("QC_START_DELAY")) * 1000
15
+ If delayMs > 0 Then WScript.Sleep delayMs
16
+
17
+ If Not fso.FileExists(startupCmd) Then WScript.Quit 1
18
+ If Not fso.FileExists(cruiseCmd) Then WScript.Quit 2
19
+
20
+ If Not CdpUp() Then sh.Run "cmd /c """ & startupCmd & """", 0, False
21
+ If Not WaitCdp(180) Then
22
+ sh.Run "cmd /c """ & cruiseCmd & """", 0, False
23
+ WScript.Quit 3
24
+ End If
25
+ sh.Run "cmd /c """ & cruiseCmd & """", 0, False
26
+
27
+ Function CdpUp()
28
+ Dim xhr
29
+ CdpUp = False
30
+ On Error Resume Next
31
+ Set xhr = CreateObject("MSXML2.XMLHTTP")
32
+ xhr.Open "GET", "http://127.0.0.1:9222/json/version", False
33
+ xhr.Send
34
+ If Err.Number = 0 And xhr.Status = 200 Then CdpUp = True
35
+ Err.Clear
36
+ On Error GoTo 0
37
+ End Function
38
+
39
+ Function WaitCdp(seconds)
40
+ Dim t0
41
+ t0 = Now
42
+ Do
43
+ If CdpUp() Then
44
+ WaitCdp = True
45
+ Exit Function
46
+ End If
47
+ WScript.Sleep 3000
48
+ Loop While DateDiff("s", t0, Now) < seconds
49
+ WaitCdp = False
50
+ End Function
@@ -0,0 +1,15 @@
1
+ @echo off
2
+ setlocal
3
+ REM Start the qianchuan cruise supervisor shipped with dsh-qianchuan-toolkit.
4
+ REM Daily window: 07:00 -> next day 01:00 (override with QC_START / QC_END).
5
+ if not defined QC_HOME set "QC_HOME=%USERPROFILE%\.qianchuan-cruise"
6
+ set "SCRIPT=%~dp0..\scripts\daily-supervisor.mjs"
7
+ rem Prefer an explicit node; otherwise use the one on PATH.
8
+ if defined QC_NODE if exist "%QC_NODE%" set "NODE=%QC_NODE%"
9
+ if not defined NODE set "NODE=node"
10
+ if not exist "%SCRIPT%" ( echo [ERROR] daily-supervisor.mjs not found: %SCRIPT% & exit /b 2 )
11
+ if not exist "%QC_HOME%" mkdir "%QC_HOME%" >nul 2>nul
12
+ if defined QC_START set "ARGS=--start %QC_START%"
13
+ if defined QC_END set "ARGS=%ARGS% --end %QC_END%"
14
+ "%NODE%" "%SCRIPT%" %ARGS% >> "%QC_HOME%\watch.out.log" 2>&1
15
+ exit /b %errorlevel%
@@ -0,0 +1,18 @@
1
+ @echo off
2
+ setlocal
3
+ if not defined QC_HOME set "QC_HOME=%USERPROFILE%\.qianchuan-cruise"
4
+ echo Stopping qianchuan cruise ...
5
+ if exist "%QC_HOME%\supervisor.pid" (
6
+ set /p SPID=<"%QC_HOME%\supervisor.pid"
7
+ taskkill /PID %SPID% /F >nul 2>nul
8
+ echo supervisor pid %SPID% killed
9
+ del /q "%QC_HOME%\supervisor.pid" >nul 2>nul
10
+ ) else ( echo no supervisor.pid found )
11
+ if exist "%QC_HOME%\watch.pid" (
12
+ set /p WPID=<"%QC_HOME%\watch.pid"
13
+ taskkill /PID %WPID% /F >nul 2>nul
14
+ echo cruise pid %WPID% killed
15
+ del /q "%QC_HOME%\watch.pid" >nul 2>nul
16
+ ) else ( echo no watch.pid found )
17
+ echo Done.
18
+ exit /b 0
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * daily-supervisor.mjs —— 每天固定窗口巡航:07:00 开始,次日 01:00 收工
4
+ *
5
+ * node daily-supervisor.mjs 常驻:窗口内跑 hourly.mjs --watch --until 01:00,收工后睡到次日 07:00
6
+ * node daily-supervisor.mjs --plan 只打印当前判定(不启动巡航)
7
+ * node daily-supervisor.mjs --start 07:00 --end 01:00
8
+ *
9
+ * 为什么需要它:hourly.mjs 的 --start-at / --until 是「单次」语义,
10
+ * 07:00 起、01:00 止这种跨零点的循环窗口,用外层守夜进程算时间最清楚。
11
+ */
12
+ import { spawn } from 'node:child_process'
13
+ import path from 'node:path'
14
+ import fs from 'node:fs'
15
+ import { fileURLToPath } from 'node:url'
16
+
17
+ const here = path.dirname(fileURLToPath(import.meta.url))
18
+ const HOURLY = process.env.QC_HOURLY || path.join(here, 'hourly.mjs')
19
+ if (!fs.existsSync(HOURLY)) { console.error('找不到 hourly.mjs:' + HOURLY + '(可用 QC_HOURLY 指定)'); process.exit(2) }
20
+ const HOME = process.env.QC_HOME || path.join(here, '..', 'prod')
21
+ const argv = process.argv.slice(2)
22
+ const arg = (k, d) => { const i = argv.indexOf('--' + k); return i >= 0 && argv[i + 1] ? argv[i + 1] : d }
23
+ const has = (k) => argv.includes('--' + k)
24
+
25
+ const START = arg('start', '07:00') // 每天开始
26
+ const END = arg('end', '01:00') // 次日收工
27
+ const PLAN_ONLY = has('plan')
28
+ const RETRY_MS = 5 * 60 * 1000
29
+
30
+ const hm = (s) => { const m = String(s).match(/^(\d{1,2}):(\d{2})$/); if (!m) throw new Error('时间格式应为 HH:MM:' + s); return Number(m[1]) * 3600000 + Number(m[2]) * 60000 }
31
+ const START_OFF = hm(START)
32
+ const END_OFF = hm(END)
33
+
34
+ const log = (...a) => console.log(new Date().toLocaleString('zh-CN'), ...a)
35
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
36
+
37
+ // 当天 00:00 的毫秒
38
+ function midnight(now = Date.now()) { const d = new Date(now); d.setHours(0, 0, 0, 0); return d.getTime() }
39
+
40
+ // 窗口:今天 START 点 -> 今天 START 点 + (END - START 若为负则加一天)
41
+ function windowOfDay(dayStart) {
42
+ const start = dayStart + START_OFF
43
+ let end = dayStart + END_OFF
44
+ if (end <= start) end += 86400000 // 跨零点:07:00 -> 次日 01:00
45
+ return { start, end }
46
+ }
47
+
48
+ function nextWindow(now = Date.now()) {
49
+ const d0 = midnight(now)
50
+ for (let i = -1; i <= 2; i++) {
51
+ const w = windowOfDay(d0 + i * 86400000)
52
+ if (now >= w.start && now < w.end) return { ...w, inWindow: true }
53
+ }
54
+ for (let i = 0; i <= 2; i++) {
55
+ const w = windowOfDay(d0 + i * 86400000)
56
+ if (w.start > now) return { ...w, inWindow: false }
57
+ }
58
+ return { ...windowOfDay(d0 + 86400000), inWindow: false }
59
+ }
60
+
61
+ function fmt(ms) { return new Date(ms).toLocaleString('zh-CN') }
62
+
63
+ async function runOnce() {
64
+ const args = ['--watch', '--max-rounds', '1000', '--until', END]
65
+ log('启动巡航:hourly.mjs ' + args.join(' '))
66
+ const out = fs.openSync(path.join(HOME, 'watch.out.log'), 'a')
67
+ const child = spawn(process.execPath, [HOURLY, ...args], { stdio: ['ignore', out, out], env: { ...process.env, QC_HOME: HOME } })
68
+ log('巡航 pid ' + child.pid)
69
+ const code = await new Promise((res) => child.on('exit', (c, s) => res(c == null ? -1 : c)))
70
+ try { fs.closeSync(out) } catch { /* 忽略 */ }
71
+ log('巡航退出,退出码 ' + code)
72
+ return code
73
+ }
74
+
75
+ if (PLAN_ONLY) {
76
+ const now = Date.now(); const w = nextWindow(now)
77
+ console.log(JSON.stringify({ now: fmt(now), start: fmt(w.start), end: fmt(w.end), inWindow: w.inWindow, startAt: START, endAt: END }, null, 1))
78
+ process.exit(0)
79
+ }
80
+
81
+ try { fs.writeFileSync(path.join(HOME, 'supervisor.pid'), String(process.pid)) } catch { /* 忽略 */ }
82
+ process.on('exit', () => { try { if (String(fs.readFileSync(path.join(HOME, 'supervisor.pid'), 'utf8')).trim() === String(process.pid)) fs.unlinkSync(path.join(HOME, 'supervisor.pid')) } catch { /* 忽略 */ } })
83
+ log('守夜进程启动:每天 ' + START + ' 开始,次日 ' + END + ' 收工(数据目录 ' + HOME + ')')
84
+ for (;;) {
85
+ const w = nextWindow()
86
+ if (!w.inWindow) {
87
+ log('不在窗口内,睡到 ' + fmt(w.start))
88
+ await sleep(Math.max(0, w.start - Date.now()))
89
+ continue
90
+ }
91
+ log('现在在窗口内(' + fmt(w.start) + ' ~ ' + fmt(w.end) + '),开始巡航')
92
+ const code = await runOnce()
93
+ const still = nextWindow()
94
+ if (!still.inWindow) continue // 已到收工点:回到循环顶部睡到下一个开始时间
95
+ if (code === 0) { // 正常退出(例如跑满 --max-rounds)且还在窗口内:稍等再起,避免空转
96
+ log('正常退出但仍在窗口内,60 秒后重新启动巡航')
97
+ await sleep(60000)
98
+ continue
99
+ }
100
+ log('异常退出(退出码 ' + code + ')且仍在窗口内,' + Math.round(RETRY_MS / 60000) + ' 分钟后重试')
101
+ await sleep(RETRY_MS)
102
+ }
@@ -41,6 +41,8 @@ function deadlineOf(hhmm) {
41
41
  return d.getTime()
42
42
  }
43
43
  const DEADLINE = UNTIL ? deadlineOf(UNTIL) : 0
44
+ const START_AT = arg('start-at', '') // 例:--start-at 07:00 等到 07:00 才开始跑第一轮
45
+ const START_AT_MS = START_AT ? deadlineOf(START_AT) : 0
44
46
 
45
47
  const P = paths()
46
48
  const pad = (n) => String(n).padStart(2, '0')
@@ -606,6 +608,26 @@ if (has('doctor')) {
606
608
  const groups = groupByAccount(cfg).filter((g) => !ONLY || g.account.key === ONLY || g.account.aavid === ONLY)
607
609
  if (!groups.length) { console.error('没有可跑的监控目标(先 node targets.mjs add 或改 targets.json)'); process.exit(2) }
608
610
 
611
+ // ---- 单实例锁:开机自启的巡航可能和手动启动的巡航撞车,两个进程同时提交会重复控量 ----
612
+ if (WATCH) {
613
+ const lockFile = path.join(P.home, 'watch.pid')
614
+ try {
615
+ const old = Number(String(fs.readFileSync(lockFile, 'utf8')).trim())
616
+ if (old && old !== process.pid) {
617
+ let alive = false
618
+ try { process.kill(old, 0); alive = true } catch (e) { alive = e.code === 'EPERM' }
619
+ if (alive) { console.error('已有一个巡航在跑(pid ' + old + '),本次不重复启动;确认那个进程已死可删除 ' + lockFile); process.exit(4) }
620
+ }
621
+ } catch { /* 没有锁文件或读不了,继续 */ }
622
+ try { fs.mkdirSync(P.home, { recursive: true }); fs.writeFileSync(lockFile, String(process.pid)) } catch (e) { console.error('写锁文件失败(已忽略):' + e.message) }
623
+ process.on('exit', () => { try { if (String(fs.readFileSync(lockFile, 'utf8')).trim() === String(process.pid)) fs.unlinkSync(lockFile) } catch { /* 忽略 */ } })
624
+ }
625
+
626
+ if (START_AT_MS) {
627
+ const ms = Math.max(0, START_AT_MS - Date.now())
628
+ log('定时启动:等到 ' + new Date(START_AT_MS).toLocaleString('zh-CN') + ' 才开始巡航(约 ' + Math.round(ms / 60000) + ' 分钟后)')
629
+ await sleep(ms)
630
+ }
609
631
  log('本轮 ' + groups.length + ' 个账号 / ' + groups.reduce((s, g) => s + g.targets.length, 0) + ' 个目标' + (DRY ? ' [dry-run]' : '') + (REPORT_ALL ? ' [每轮推送]' : '') + (DEADLINE ? ' [跑到 ' + new Date(DEADLINE).toLocaleString('zh-CN') + ']' : ''))
610
632
  if (REPORT_ALL && WATCH) {
611
633
  const r0 = resolveRule(cfg, groups[0].targets[0])
@@ -649,6 +671,14 @@ try {
649
671
  if (REPORT_ALL || recs.some((r) => (r.anomalies || []).length)) await reportRound(i, recs, next)
650
672
  if (last) break
651
673
  if (DEADLINE && next > DEADLINE) {
674
+ const remain = DEADLINE - Date.now()
675
+ // 收尾轮:最后一个 :15/:30/:45/:55 节点已越过收工时间,但离收工还有余量时,补跑一轮(收工前 45 秒)
676
+ if (remain > 90000) {
677
+ const fin = DEADLINE - 45000
678
+ log('最后节点已超出收工时间,改在 ' + new Date(fin).toLocaleString('zh-CN') + ' 加跑收尾轮(收工 ' + new Date(DEADLINE).toLocaleString('zh-CN') + ')')
679
+ await sleep(Math.max(0, fin - Date.now()))
680
+ continue
681
+ }
652
682
  log('已到收工时间 ' + new Date(DEADLINE).toLocaleString('zh-CN') + ',巡航结束')
653
683
  if (REPORT_ALL) await notify({ dir: P.home, title: '千川巡航:已收工', text: '本轮后到达停止时间 ' + new Date(DEADLINE).toLocaleString('zh-CN') + ',巡航结束。', level: 'info', event: 'stop', say: (s) => log(' notify: ' + s) }).catch(() => {})
654
684
  break
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-qianchuan-toolkit",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "type": "module",
5
5
  "description": "DSH skill bundle for 巨量千川: 直播盯盘自动控量 (qianchuan-live-control) + 乘方计划复制 (qianchuan-plan-copy).",
6
6
  "license": "MIT",