@juejin-opensource/jusage 0.1.1-beta.8 → 0.1.1

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.
@@ -1,4 +1,5 @@
1
1
  import { spawnSync } from 'node:child_process';
2
+ import { homedir } from 'node:os';
2
3
  import { ensureDaemonLogDir, resolveServiceCommand } from './daemon.js';
3
4
  export const WINDOWS_TASK_NAME = 'jusage';
4
5
  /** Previous scheduled-task name; remove on start/stop so upgrades do not leave two daemons. */
@@ -26,7 +27,7 @@ export async function registerWindowsAutostart(cliBinPath, dataDir) {
26
27
  const script = `
27
28
  $ErrorActionPreference = 'Stop'
28
29
  $taskName = ${psQuote(WINDOWS_TASK_NAME)}
29
- $action = New-ScheduledTaskAction -Execute ${psQuote(nodePath)} -Argument ${psQuote(argument)}
30
+ $action = New-ScheduledTaskAction -Execute ${psQuote(nodePath)} -Argument ${psQuote(argument)} -WorkingDirectory ${psQuote(homedir())}
30
31
  $trigger = New-ScheduledTaskTrigger -AtLogOn
31
32
  $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -ExecutionTimeLimit ([TimeSpan]::Zero) -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1)
32
33
  $principal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive -RunLevel Limited
package/dist/service.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { platform } from 'node:os';
2
2
  import { DEFAULT_DATA_DIR, DEFAULT_PORT, getRunningOwner, loadConfig, runtimeKindLabel, touchStatsSince, } from '@juejin-opensource/jusage-core';
3
- import { clearPid, stopPid, waitForPid, } from './daemon.js';
3
+ import { clearPid, daemonLogPath, readDaemonLogTail, stopPid, waitForServiceReady, } from './daemon.js';
4
4
  import { isMacosAutostartRegistered, registerMacosAutostart, unregisterMacosAutostart, } from './service-macos.js';
5
5
  import { isWindowsAutostartRegistered, registerWindowsAutostart, unregisterWindowsAutostart, } from './service-windows.js';
6
6
  function assertSupportedPlatform() {
@@ -33,6 +33,19 @@ async function unregisterAutostart() {
33
33
  }
34
34
  export async function cmdServiceStart(cliBinPath, daysAgo) {
35
35
  assertSupportedPlatform();
36
+ const startedAt = Date.now();
37
+ const waitTimer = setInterval(() => {
38
+ const elapsed = Math.round((Date.now() - startedAt) / 1000);
39
+ console.log(` 仍在等待检测中(已 ${elapsed}s)…`);
40
+ }, 3_000);
41
+ try {
42
+ await cmdServiceStartBody(cliBinPath, daysAgo);
43
+ }
44
+ finally {
45
+ clearInterval(waitTimer);
46
+ }
47
+ }
48
+ async function cmdServiceStartBody(cliBinPath, daysAgo) {
36
49
  const { dir, config } = await loadConfig();
37
50
  // Seed statsSince before launchd starts `jusage start` (do not bake --days into plist).
38
51
  await touchStatsSince(dir, config, daysAgo != null ? { daysAgo } : undefined);
@@ -56,13 +69,23 @@ export async function cmdServiceStart(cliBinPath, daysAgo) {
56
69
  return;
57
70
  }
58
71
  await registerAutostart(cliBinPath, dir);
59
- const pid = await waitForPid(dir);
60
- if (pid == null) {
61
- throw new Error('自启已注册,但进程未在预期时间内启动,请查看 ~/.ai-usage/logs/daemon.log');
72
+ const port = config.serverPort || DEFAULT_PORT;
73
+ const ready = await waitForServiceReady(dir, port);
74
+ if (ready.pid == null && !ready.health) {
75
+ const logPath = daemonLogPath(dir);
76
+ const tail = await readDaemonLogTail(dir);
77
+ const hint = tail ? `\n--- daemon.log ---\n${tail}` : '';
78
+ throw new Error(`自启已注册,但进程未在预期时间内启动(/health 也未就绪),请查看 ${logPath}${hint}`);
62
79
  }
63
80
  const { config: refreshed } = await loadConfig(dir);
64
- console.log(`✓ 服务已在后台启动 (pid ${pid})`);
65
- console.log(` 面板: http://127.0.0.1:${refreshed.serverPort || DEFAULT_PORT}`);
81
+ const panelPort = refreshed.serverPort || DEFAULT_PORT;
82
+ if (ready.pid != null) {
83
+ console.log(`✓ 服务已在后台启动 (pid ${ready.pid})`);
84
+ }
85
+ else {
86
+ console.log('✓ 服务已在后台启动(面板 /health 已就绪)');
87
+ }
88
+ console.log(` 面板: http://127.0.0.1:${panelPort}`);
66
89
  console.log(` 数据: ${dir}`);
67
90
  console.log(` 开机自启: 已注册`);
68
91
  console.log(` 日志: ${dir}/logs/daemon.log`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juejin-opensource/jusage",
3
- "version": "0.1.1-beta.8",
3
+ "version": "0.1.1",
4
4
  "description": "本地优先的 AI Agent Token 用量看板 CLI(采集 + 本地面板 + 可选云端上报)",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -30,12 +30,12 @@
30
30
  "access": "public"
31
31
  },
32
32
  "dependencies": {
33
- "@juejin-opensource/jusage-core": "0.1.1-beta.8"
33
+ "@juejin-opensource/jusage-core": "0.1.1"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@types/node": "^22.13.10",
37
37
  "typescript": "^5.8.2",
38
- "@juejin-opensource/jusage-dashboard": "0.1.1-beta.8"
38
+ "@juejin-opensource/jusage-dashboard": "0.1.1"
39
39
  },
40
40
  "engines": {
41
41
  "node": ">=20"