@tama-create/sf-tools 0.0.0-stage → 1.4.0-dev.10
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/LICENSE +24 -0
- package/README.md +212 -2
- package/bin/sft.js +22 -0
- package/config/changeset-types.txt +350 -0
- package/config/install/gitmessage +6 -0
- package/config/install/release/README.md +26 -0
- package/config/install/release/deploy-target.txt +368 -0
- package/config/install/release/remove-target.txt +279 -0
- package/config/metadata-capability.txt +118 -0
- package/config/metadata-dirs.txt +79 -0
- package/config/metasync-detect.txt +52 -0
- package/config/metasync-extra.txt +30 -0
- package/config/metasync-fingerprint.txt +90 -0
- package/dist/cli.js +35 -0
- package/dist/commands/admin.js +46 -0
- package/dist/commands/check.js +20 -0
- package/dist/commands/ci.js +123 -0
- package/dist/commands/deploy.js +31 -0
- package/dist/commands/doctor.js +11 -0
- package/dist/commands/dryrun.js +17 -0
- package/dist/commands/hook.js +46 -0
- package/dist/commands/index.js +37 -0
- package/dist/commands/job.js +31 -0
- package/dist/commands/launcher.js +57 -0
- package/dist/commands/log.js +58 -0
- package/dist/commands/metasync.js +21 -0
- package/dist/commands/pr.js +32 -0
- package/dist/commands/project.js +102 -0
- package/dist/commands/push.js +16 -0
- package/dist/commands/release.js +19 -0
- package/dist/commands/start.js +67 -0
- package/dist/commands/switch-org.js +12 -0
- package/dist/commands/update-cli.js +11 -0
- package/dist/commands/version.js +18 -0
- package/dist/core/admin-clone.js +79 -0
- package/dist/core/ai-answers.js +40 -0
- package/dist/core/branches.js +88 -0
- package/dist/core/check-targets.js +170 -0
- package/dist/core/check.js +34 -0
- package/dist/core/ci-check-branch.js +150 -0
- package/dist/core/ci-login-steps.js +73 -0
- package/dist/core/ci-release-steps.js +193 -0
- package/dist/core/ci-sequence-steps.js +188 -0
- package/dist/core/ci-validate-steps.js +140 -0
- package/dist/core/cli-update-notice.js +47 -0
- package/dist/core/command.js +25 -0
- package/dist/core/create-connect.js +519 -0
- package/dist/core/create-setup.js +358 -0
- package/dist/core/create-state.js +82 -0
- package/dist/core/delegate.js +45 -0
- package/dist/core/deploy.js +66 -0
- package/dist/core/diag.js +200 -0
- package/dist/core/doctor.js +211 -0
- package/dist/core/eca.js +259 -0
- package/dist/core/exit-codes.js +12 -0
- package/dist/core/gh-identity.js +46 -0
- package/dist/core/hook-install.js +130 -0
- package/dist/core/hook.js +116 -0
- package/dist/core/identity.js +48 -0
- package/dist/core/job.js +243 -0
- package/dist/core/launcher-status.js +215 -0
- package/dist/core/lines.js +25 -0
- package/dist/core/log-auto.js +56 -0
- package/dist/core/log-box.js +109 -0
- package/dist/core/log-collector.js +370 -0
- package/dist/core/log-format.js +44 -0
- package/dist/core/log-pull.js +565 -0
- package/dist/core/log-select.js +126 -0
- package/dist/core/log-session.js +362 -0
- package/dist/core/log-start.js +421 -0
- package/dist/core/log-status.js +223 -0
- package/dist/core/log-summarize.js +127 -0
- package/dist/core/logger.js +71 -0
- package/dist/core/login-url.js +50 -0
- package/dist/core/manifest-build.js +118 -0
- package/dist/core/mask.js +70 -0
- package/dist/core/metadata-types.js +79 -0
- package/dist/core/metasync.js +684 -0
- package/dist/core/new-version.js +93 -0
- package/dist/core/org.js +115 -0
- package/dist/core/origin.js +62 -0
- package/dist/core/package-xml.js +157 -0
- package/dist/core/pr.js +217 -0
- package/dist/core/project-create.js +733 -0
- package/dist/core/project-reconnect.js +126 -0
- package/dist/core/project-status.js +246 -0
- package/dist/core/project-tier.js +374 -0
- package/dist/core/project-upgrade.js +138 -0
- package/dist/core/project-verify.js +517 -0
- package/dist/core/promote-check.js +401 -0
- package/dist/core/promote.js +141 -0
- package/dist/core/push.js +163 -0
- package/dist/core/release.js +256 -0
- package/dist/core/scoped-box.js +29 -0
- package/dist/core/start.js +155 -0
- package/dist/core/switch-org.js +49 -0
- package/dist/core/test-types.js +212 -0
- package/dist/core/toolbox.js +4 -0
- package/dist/core/update-cli.js +156 -0
- package/dist/core/update-secret.js +301 -0
- package/dist/core/update-workflows.js +132 -0
- package/dist/core/verify-gh.js +286 -0
- package/dist/core/verify-samples.js +152 -0
- package/dist/core/windows-shim.js +21 -0
- package/dist/io/capability-file.js +11 -0
- package/dist/io/create-deps.js +69 -0
- package/dist/io/doctor-deps.js +32 -0
- package/dist/io/hook-install-deps.js +11 -0
- package/dist/io/job-deps.js +39 -0
- package/dist/io/key-file.js +17 -0
- package/dist/io/keys.js +172 -0
- package/dist/io/log-deps.js +107 -0
- package/dist/io/metasync-deps.js +22 -0
- package/dist/io/open-browser.js +21 -0
- package/dist/io/package-version.js +10 -0
- package/dist/io/pinned-project.js +111 -0
- package/dist/io/promote-deps.js +28 -0
- package/dist/io/push-refs.js +20 -0
- package/dist/io/resolve.js +74 -0
- package/dist/io/run.js +201 -0
- package/dist/io/switch-org-deps.js +10 -0
- package/dist/io/temp-dir.js +24 -0
- package/dist/io/tool-versions.js +14 -0
- package/dist/io/toolbox.js +68 -0
- package/dist/io/update-cli-deps.js +75 -0
- package/dist/io/update-secret-deps.js +27 -0
- package/dist/launch.js +152 -0
- package/dist/ui/confirm.js +18 -0
- package/dist/ui/launcher.js +163 -0
- package/dist/ui/prompt.js +66 -0
- package/lib/apexlog.js +819 -0
- package/lib/metasync.js +1364 -0
- package/lib/package.json +4 -0
- package/lib/typecheck.js +547 -0
- package/package.json +44 -4
- package/templates/.forceignore +12 -0
- package/templates/.gitattributes +58 -0
- package/templates/.github/workflows/wf-metasync.yml +177 -0
- package/templates/.github/workflows/wf-release.yml +454 -0
- package/templates/.github/workflows/wf-sequence.yml +292 -0
- package/templates/.github/workflows/wf-validate.yml +389 -0
- package/templates/.prettierignore +11 -0
- package/templates/.prettierrc +17 -0
- package/templates/.vscode/extensions.json +9 -0
- package/templates/.vscode/launch.json +16 -0
- package/templates/.vscode/settings.json +39 -0
- package/templates/AGENTS.md +3 -0
- package/templates/CLAUDE.md +4 -0
- package/templates/README.md +11 -0
- package/templates/config/project-scratch-def.json +13 -0
- package/templates/eslint.config.js +55 -0
- package/templates/force-app/main/default/.gitkeep +0 -0
- package/templates/gitignore.template +62 -0
- package/templates/jest.config.js +6 -0
- package/templates/package.json +29 -0
- package/templates/scripts/apex/hello.apex +10 -0
- package/templates/scripts/soql/account.soql +6 -0
- package/templates/sf-tools/config/branches.txt +24 -0
- package/templates/sf-tools/config/metadata.txt +576 -0
- package/templates/sfdx-project.json +12 -0
- package/templates-ts/.github/workflows/wf-metasync.yml +109 -0
- package/templates-ts/.github/workflows/wf-release.yml +263 -0
- package/templates-ts/.github/workflows/wf-sequence.yml +153 -0
- package/templates-ts/.github/workflows/wf-validate.yml +185 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
// 取得役: ランチャーが開いているあいだ、同じプロセスの中で、デバッグログを組織から取り続ける(設計 6.3。別のプロセスを起こさない)。
|
|
2
|
+
// sf-log.sh collect(phases/log/collect.sh の 9c 章)の移し先。
|
|
3
|
+
// 1. 印(logs/apex/.collector: PID・STARTED・ORG・RUNTIME=node・HEARTBEAT)と名乗り(.claim)を、手元の状態のロックの中で書く。
|
|
4
|
+
// 別の環境(シェル版)が扱っていれば・同じ組織の node の取得役が生きていれば、動かない
|
|
5
|
+
// 2. 5 秒ごとに目を覚まし、次を繰り返す(collectorStep):
|
|
6
|
+
// a. 生きている合図(30 秒ごとに HEARTBEAT・.claim の AT)
|
|
7
|
+
// b. 止まる: 停止の依頼(requests/stop)・印の入れ替わり(PID が自分でない)・12 時間
|
|
8
|
+
// c. 取得(間隔(SF_LOG_INTERVAL。既定 180 秒)が来た・pull の依頼(requests/pull.<ID>)がある・前回の取り残しがある):
|
|
9
|
+
// 取得のロック(.fetch.lock)を 1 回だけ試し、取れたら 1 回の取得(log-pull.ts の fetchRound)。追いついたら拾い直し。
|
|
10
|
+
// 取得の前に集めた依頼に、done.<ID>(RESULT=ok / error)で応える。失敗は、間隔を倍に(最大 600 秒)
|
|
11
|
+
// d. 自分の TraceFlag が、まだ自分のものか(なくなった・別の sf-tools の有効な TraceFlag に引き継がれた)を確かめ、
|
|
12
|
+
// 残りが 60 分未満なら、今から 2 時間に延ばす
|
|
13
|
+
// e. 5 分より古い依頼・応答(AT で判断)を消す
|
|
14
|
+
// 3. 止まるときは、自分の印と、自分の環境の名乗りを消す(TraceFlag は消さない。消すのは、ランチャーの終わり(leave))
|
|
15
|
+
// sf の更新(sft update-cli・sf-update-cli.sh)が動いているあいだは、取らずに次に回す(sf の入れ替えの途中に、sf を呼ばない。シェル版と同じ)。
|
|
16
|
+
// 持ち主の一覧(.owners)の見張りはしない(sft と同じプロセスの中で動き、sft が終われば一緒に終わる。持ち主の出入りは log-session.ts)
|
|
17
|
+
import { fetchRound, fmtZ, lateRecheck, note, parseTime, RUNTIME_SELF, read, stateSet } from "./log-pull.js";
|
|
18
|
+
import { ID_RE, ISO_RE, kvGet, kvSet } from "./log-select.js";
|
|
19
|
+
import { countToday } from "./log-start.js";
|
|
20
|
+
import { parseTraceFlags } from "./log-status.js";
|
|
21
|
+
const APEX = "logs/apex";
|
|
22
|
+
const STATE_F = `${APEX}/.state`;
|
|
23
|
+
const STATUS_F = `${APEX}/.status`;
|
|
24
|
+
const COLLECTOR_F = `${APEX}/.collector`;
|
|
25
|
+
const CLAIM_F = `${APEX}/.claim`;
|
|
26
|
+
const LOCK_DIR = `${APEX}/.lock`;
|
|
27
|
+
const FETCH_LOCK = `${APEX}/.fetch.lock`;
|
|
28
|
+
export const REQ_DIR = `${APEX}/requests`;
|
|
29
|
+
const MIX_STALE = 1800;
|
|
30
|
+
const MAX_RUN = 43200;
|
|
31
|
+
const HB_EVERY = 30;
|
|
32
|
+
export const TICK_MS = 5000;
|
|
33
|
+
const REQ_ID_RE = /^[0-9]+_[0-9]+_[0-9]+$/;
|
|
34
|
+
const nowSec = (box) => Math.floor(box.clock.now().getTime() / 1000);
|
|
35
|
+
const isSftoolsLevel = (dl) => dl === "sf_tools_log_standard" || dl === "sf_tools_log_detail";
|
|
36
|
+
/** 取得の間隔(秒)。SF_LOG_INTERVAL(10〜3600 の整数)。それ以外は 180 */
|
|
37
|
+
export function collectInterval(box) {
|
|
38
|
+
const v = box.env("SF_LOG_INTERVAL") ?? "";
|
|
39
|
+
return /^[0-9]+$/.test(v) && Number(v) >= 10 && Number(v) <= 3600 ? Number(v) : 180;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 取得役として名乗る。戻り値: 動かす取得役 / 動かない理由(文字列)
|
|
43
|
+
* .state に組織・ユーザーがなければ、動かない
|
|
44
|
+
*/
|
|
45
|
+
export async function claimCollector(box, deps) {
|
|
46
|
+
const state = read(box, STATE_F);
|
|
47
|
+
const org = kvGet(state, "ORG");
|
|
48
|
+
const userId = kvGet(state, "USER_ID");
|
|
49
|
+
if (org === "" || !ID_RE.test(userId))
|
|
50
|
+
return ".state に組織・ユーザーがありません";
|
|
51
|
+
if (!(await deps.lock.take(LOCK_DIR, 50)))
|
|
52
|
+
return `手元の状態のロックを取れませんでした(${LOCK_DIR})`;
|
|
53
|
+
const started = nowSec(box);
|
|
54
|
+
try {
|
|
55
|
+
const claim = read(box, CLAIM_F);
|
|
56
|
+
const crt = kvGet(claim, "RUNTIME");
|
|
57
|
+
const cat = kvGet(claim, "AT");
|
|
58
|
+
if (claim !== "" && crt !== RUNTIME_SELF && (!/^[0-9]+$/.test(cat) || started - Number(cat) <= MIX_STALE))
|
|
59
|
+
return `別の環境(${crt || "不明"})が、このプロジェクトのログを扱っています`;
|
|
60
|
+
const col = read(box, COLLECTOR_F);
|
|
61
|
+
if (col !== "") {
|
|
62
|
+
const rt = kvGet(col, "RUNTIME");
|
|
63
|
+
const pid = kvGet(col, "PID");
|
|
64
|
+
const hb = kvGet(col, "HEARTBEAT");
|
|
65
|
+
if (rt === RUNTIME_SELF) {
|
|
66
|
+
if (/^[0-9]+$/.test(pid) && Number(pid) !== deps.pid && deps.pidAlive(Number(pid)))
|
|
67
|
+
return `取得役が、すでに動いています(PID ${pid}・組織 ${kvGet(col, "ORG")})`;
|
|
68
|
+
}
|
|
69
|
+
else if (!/^[0-9]+$/.test(hb) || started - Number(hb) <= MIX_STALE) {
|
|
70
|
+
return `取得役(${rt || "シェル版"})が動いています`;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
box.files.makeDir(REQ_DIR);
|
|
74
|
+
box.files.removeFile(`${REQ_DIR}/stop`);
|
|
75
|
+
box.files.writeText(COLLECTOR_F, kvSet("", { PID: String(deps.pid), STARTED: String(started), ORG: org, RUNTIME: RUNTIME_SELF, HEARTBEAT: String(started) }));
|
|
76
|
+
box.files.writeText(CLAIM_F, kvSet(claim, { RUNTIME: RUNTIME_SELF, AT: String(started) }));
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
deps.lock.drop(LOCK_DIR);
|
|
80
|
+
}
|
|
81
|
+
const interval = collectInterval(box);
|
|
82
|
+
await stateSet({ box, deps }, STATUS_F, { STATE: "collecting", ORG: org, REASON: "", WARNING: "" });
|
|
83
|
+
note(box, "INFO", `取得役: 起動しました(PID ${deps.pid}・組織 ${org}・間隔 ${interval} 秒)`);
|
|
84
|
+
return { box, deps, org, userId, interval, started, next: 0, backoff: interval, hbLast: started, quit: "" };
|
|
85
|
+
}
|
|
86
|
+
/** 生きている合図(30 秒ごと)。印が自分のものでなければ、書かない(後始末で消えた印を作り直さない) */
|
|
87
|
+
async function heartbeat(c) {
|
|
88
|
+
const now = nowSec(c.box);
|
|
89
|
+
if (now - c.hbLast < HB_EVERY)
|
|
90
|
+
return;
|
|
91
|
+
if (!(await c.deps.lock.take(LOCK_DIR, 5)))
|
|
92
|
+
return;
|
|
93
|
+
try {
|
|
94
|
+
const col = read(c.box, COLLECTOR_F);
|
|
95
|
+
if (kvGet(col, "PID") === String(c.deps.pid)) {
|
|
96
|
+
c.box.files.writeText(COLLECTOR_F, kvSet(col, { HEARTBEAT: String(now) }));
|
|
97
|
+
const claim = read(c.box, CLAIM_F);
|
|
98
|
+
if (kvGet(claim, "RUNTIME") === RUNTIME_SELF)
|
|
99
|
+
c.box.files.writeText(CLAIM_F, kvSet(claim, { AT: String(now) }));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
finally {
|
|
103
|
+
c.deps.lock.drop(LOCK_DIR);
|
|
104
|
+
}
|
|
105
|
+
c.hbLast = now;
|
|
106
|
+
}
|
|
107
|
+
/** 止まるべきか。止まるべきなら理由 */
|
|
108
|
+
function shouldQuit(c) {
|
|
109
|
+
if (c.box.files.exists(`${REQ_DIR}/stop`)) {
|
|
110
|
+
c.box.files.removeFile(`${REQ_DIR}/stop`);
|
|
111
|
+
return "停止の依頼で止まります";
|
|
112
|
+
}
|
|
113
|
+
if (kvGet(read(c.box, COLLECTOR_F), "PID") !== String(c.deps.pid))
|
|
114
|
+
return "印が入れ替わったため止まります";
|
|
115
|
+
if (nowSec(c.box) - c.started >= MAX_RUN)
|
|
116
|
+
return "12 時間で止まりました。sft をやり直すと再開します";
|
|
117
|
+
return "";
|
|
118
|
+
}
|
|
119
|
+
/** 今 届いている pull の依頼の ID。ID の形でない依頼は、ここで消す */
|
|
120
|
+
export function pullRequestIds(box) {
|
|
121
|
+
const ids = [];
|
|
122
|
+
for (const name of box.files.listDir(REQ_DIR)) {
|
|
123
|
+
if (!name.startsWith("pull."))
|
|
124
|
+
continue;
|
|
125
|
+
const id = name.slice(5);
|
|
126
|
+
if (REQ_ID_RE.test(id))
|
|
127
|
+
ids.push(id);
|
|
128
|
+
else
|
|
129
|
+
box.files.removeFile(`${REQ_DIR}/${name}`);
|
|
130
|
+
}
|
|
131
|
+
return ids.sort();
|
|
132
|
+
}
|
|
133
|
+
/** pull の依頼に応える。待つのをやめて消された依頼には、応えない */
|
|
134
|
+
function respondPulls(box, result, ids) {
|
|
135
|
+
const at = String(nowSec(box));
|
|
136
|
+
for (const id of ids) {
|
|
137
|
+
if (!box.files.exists(`${REQ_DIR}/pull.${id}`))
|
|
138
|
+
continue;
|
|
139
|
+
box.files.writeText(`${REQ_DIR}/done.${id}`, kvSet("", { RESULT: result, AT: at }));
|
|
140
|
+
box.files.removeFile(`${REQ_DIR}/pull.${id}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/** 5 分より古い依頼・応答を消す(pull が待つのをやめたあとの残り)。AT のないものも消す */
|
|
144
|
+
function pruneRequests(box) {
|
|
145
|
+
const now = nowSec(box);
|
|
146
|
+
for (const name of box.files.listDir(REQ_DIR)) {
|
|
147
|
+
if (!/^(pull|done)\./.test(name))
|
|
148
|
+
continue;
|
|
149
|
+
const at = kvGet(read(box, `${REQ_DIR}/${name}`), "AT");
|
|
150
|
+
if (!/^[0-9]+$/.test(at) || now - Number(at) > 300)
|
|
151
|
+
box.files.removeFile(`${REQ_DIR}/${name}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
async function tfQuery(c) {
|
|
155
|
+
const r = await c.box
|
|
156
|
+
.run("sf", [
|
|
157
|
+
"data",
|
|
158
|
+
"query",
|
|
159
|
+
"--use-tooling-api",
|
|
160
|
+
"--target-org",
|
|
161
|
+
c.org,
|
|
162
|
+
"--json",
|
|
163
|
+
"--query",
|
|
164
|
+
`SELECT Id, LogType, StartDate, ExpirationDate, DebugLevel.DeveloperName FROM TraceFlag WHERE TracedEntityId = '${c.userId}'`,
|
|
165
|
+
])
|
|
166
|
+
.catch(() => null);
|
|
167
|
+
if (r === null || r.code !== 0)
|
|
168
|
+
return null;
|
|
169
|
+
return parseTraceFlags(r.stdout, c.box.clock.now().getTime());
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* 自分の TraceFlag が、まだ自分のものかを確かめる(取得の回ごと)。
|
|
173
|
+
* ok = 有効(または期限が切れただけ。延長で、また有効になる)・問い合わせの失敗 / gone = なくなった(作り直さない)/
|
|
174
|
+
* yielded = 別の sf-tools の有効な TraceFlag に引き継がれた(止まる)
|
|
175
|
+
*/
|
|
176
|
+
export async function checkFlag(c) {
|
|
177
|
+
const state = read(c.box, STATE_F);
|
|
178
|
+
const tf = kvGet(state, "TRACE_FLAG_ID");
|
|
179
|
+
if (!ID_RE.test(tf) || kvGet(state, "ORG") !== c.org)
|
|
180
|
+
return "ok";
|
|
181
|
+
const rows = await tfQuery(c);
|
|
182
|
+
if (rows === null)
|
|
183
|
+
return "ok";
|
|
184
|
+
const mine = rows.find((r) => r.id === tf && r.type === "USER_DEBUG");
|
|
185
|
+
if (mine?.state === "active")
|
|
186
|
+
return "ok";
|
|
187
|
+
const other = rows.find((r) => r.id !== tf && r.type === "USER_DEBUG" && r.state === "active" && isSftoolsLevel(r.level));
|
|
188
|
+
if (other) {
|
|
189
|
+
await yieldTo(c, tf, other.id);
|
|
190
|
+
return "yielded";
|
|
191
|
+
}
|
|
192
|
+
if (mine)
|
|
193
|
+
return "ok";
|
|
194
|
+
await stateSet(c, STATUS_F, {
|
|
195
|
+
WARNING: "自分の TraceFlag が消えた・置き換わったため、新しいログは出ません(sft の起動で取り直せます)",
|
|
196
|
+
});
|
|
197
|
+
return "gone";
|
|
198
|
+
}
|
|
199
|
+
/** 別の場所の sft・sf-start に引き継がれたので、取得をやめる。組織には書き込まない(相手の TraceFlag に触れない) */
|
|
200
|
+
async function yieldTo(c, tf, other) {
|
|
201
|
+
if (!(await c.deps.lock.take(LOCK_DIR, 50)))
|
|
202
|
+
return;
|
|
203
|
+
try {
|
|
204
|
+
const state = read(c.box, STATE_F);
|
|
205
|
+
if (kvGet(state, "TRACE_FLAG_ID") !== tf || kvGet(state, "ORG") !== c.org)
|
|
206
|
+
return;
|
|
207
|
+
if (kvGet(read(c.box, COLLECTOR_F), "PID") !== String(c.deps.pid))
|
|
208
|
+
return;
|
|
209
|
+
c.box.files.writeText(STATE_F, kvSet(state, { TRACE_FLAG_ID: "" }));
|
|
210
|
+
c.box.files.writeText(STATUS_F, kvSet(read(c.box, STATUS_F), {
|
|
211
|
+
STATE: "stopped",
|
|
212
|
+
EXPIRES: "",
|
|
213
|
+
EXPIRES_AT: "",
|
|
214
|
+
BACKLOG: "",
|
|
215
|
+
WARNING: "",
|
|
216
|
+
TAKEOVER: "",
|
|
217
|
+
REASON: "別の場所の sft・sf-start に引き継がれたため",
|
|
218
|
+
}));
|
|
219
|
+
}
|
|
220
|
+
finally {
|
|
221
|
+
c.deps.lock.drop(LOCK_DIR);
|
|
222
|
+
}
|
|
223
|
+
c.quit = `別の場所の sft・sf-start(TraceFlag ${other})に引き継がれたため、止まります`;
|
|
224
|
+
}
|
|
225
|
+
/** 自分の TraceFlag を、今から 2 時間に延ばす。延ばす前に、ID・種類を確かめる(人が置き換えたものを延ばさない) */
|
|
226
|
+
export async function extendFlag(c) {
|
|
227
|
+
const tf = kvGet(read(c.box, STATE_F), "TRACE_FLAG_ID");
|
|
228
|
+
if (!ID_RE.test(tf)) {
|
|
229
|
+
await stateSet(c, STATUS_F, { WARNING: "TraceFlag がないため、延ばせません" });
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
const rows = await tfQuery(c);
|
|
233
|
+
if (rows === null)
|
|
234
|
+
return false;
|
|
235
|
+
if (!rows.some((r) => r.id === tf && r.type === "USER_DEBUG")) {
|
|
236
|
+
await stateSet(c, STATUS_F, { WARNING: "自分の TraceFlag が消えた・置き換わったため、延ばせません" });
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
const quit = shouldQuit(c); // 書き込む直前にも確かめる(消された TraceFlag を延ばし直さない)
|
|
240
|
+
if (quit !== "") {
|
|
241
|
+
c.quit = quit;
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
const nowMs = c.box.clock.now().getTime();
|
|
245
|
+
const exp = fmtZ(nowMs + 7200_000);
|
|
246
|
+
const r = await c.box
|
|
247
|
+
.run("sf", [
|
|
248
|
+
"data",
|
|
249
|
+
"update",
|
|
250
|
+
"record",
|
|
251
|
+
"--use-tooling-api",
|
|
252
|
+
"--sobject",
|
|
253
|
+
"TraceFlag",
|
|
254
|
+
"--record-id",
|
|
255
|
+
tf,
|
|
256
|
+
"--target-org",
|
|
257
|
+
c.org,
|
|
258
|
+
"--json",
|
|
259
|
+
"--values",
|
|
260
|
+
`StartDate=${fmtZ(nowMs)} ExpirationDate=${exp}`,
|
|
261
|
+
])
|
|
262
|
+
.catch(() => null);
|
|
263
|
+
if (r === null || r.code !== 0)
|
|
264
|
+
return false;
|
|
265
|
+
const d = new Date(nowMs + 7200_000);
|
|
266
|
+
const hm = `${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
|
|
267
|
+
await stateSet(c, STATUS_F, { EXPIRES: exp, EXPIRES_AT: hm, WARNING: "" });
|
|
268
|
+
note(c.box, "INFO", `取得役: TraceFlag を延ばしました(期限 ${exp})`);
|
|
269
|
+
return true;
|
|
270
|
+
}
|
|
271
|
+
const hhmm = (box) => {
|
|
272
|
+
const d = box.clock.now();
|
|
273
|
+
return `${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
|
|
274
|
+
};
|
|
275
|
+
/** 取得役の 1 回分(待たない)。止まるときは c.quit に理由が入る */
|
|
276
|
+
export async function collectorStep(c) {
|
|
277
|
+
const { box, deps } = c;
|
|
278
|
+
await heartbeat(c);
|
|
279
|
+
c.quit = shouldQuit(c);
|
|
280
|
+
if (c.quit !== "")
|
|
281
|
+
return;
|
|
282
|
+
const now = nowSec(box);
|
|
283
|
+
const pulls = pullRequestIds(box); // 取得の前に集める(取得の最中に届いた依頼は、次の取得のあとで応える)
|
|
284
|
+
if ((now >= c.next || pulls.length > 0) && !(deps.upgradeRunning?.() ?? false)) {
|
|
285
|
+
if (await deps.lock.take(FETCH_LOCK, 1)) {
|
|
286
|
+
let rc = "fail";
|
|
287
|
+
let late = "";
|
|
288
|
+
try {
|
|
289
|
+
rc = await fetchRound({ box, deps, org: c.org, userId: c.userId });
|
|
290
|
+
if (rc === "done")
|
|
291
|
+
late = await lateRecheck({ box, deps, org: c.org, userId: c.userId });
|
|
292
|
+
}
|
|
293
|
+
finally {
|
|
294
|
+
deps.lock.drop(FETCH_LOCK);
|
|
295
|
+
}
|
|
296
|
+
const cnt = String(countToday(box));
|
|
297
|
+
if (rc === "done") {
|
|
298
|
+
respondPulls(box, "ok", pulls);
|
|
299
|
+
c.backoff = c.interval;
|
|
300
|
+
c.next = nowSec(box) + c.interval;
|
|
301
|
+
await stateSet(c, STATUS_F, {
|
|
302
|
+
STATE: "collecting",
|
|
303
|
+
LAST_FETCH: hhmm(box),
|
|
304
|
+
COUNT_TODAY: cnt,
|
|
305
|
+
BACKLOG: "0",
|
|
306
|
+
WARNING: late,
|
|
307
|
+
LATE_WARNING: late,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
else if (rc === "more") {
|
|
311
|
+
c.next = 0; // 取り残しがある: 次の回にすぐ続きを取る(依頼には、追いついてから応える)
|
|
312
|
+
await stateSet(c, STATUS_F, { LAST_FETCH: hhmm(box), COUNT_TODAY: cnt, BACKLOG: "50 本以上" });
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
respondPulls(box, "error", pulls);
|
|
316
|
+
c.backoff = Math.min(c.backoff * 2, 600);
|
|
317
|
+
c.next = nowSec(box) + c.backoff;
|
|
318
|
+
await stateSet(c, STATUS_F, { WARNING: `取得に失敗しました(${c.backoff} 秒後にやり直します)` });
|
|
319
|
+
}
|
|
320
|
+
// 自分の TraceFlag の確認(取得のあと。引き継がれる前のログは、取り終えている)と、残りが 60 分未満なら延長
|
|
321
|
+
if ((await checkFlag(c)) === "ok" && c.quit === "") {
|
|
322
|
+
const exp = kvGet(read(box, STATUS_F), "EXPIRES");
|
|
323
|
+
if (ISO_RE.test(exp) && parseTime(exp) < box.clock.now().getTime() + 3600_000) {
|
|
324
|
+
if (!(await extendFlag(c)) && c.quit === "")
|
|
325
|
+
note(box, "INFO", "取得役: TraceFlag を延ばせませんでした");
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
pruneRequests(box);
|
|
331
|
+
}
|
|
332
|
+
/** 取得役の印と、自分の環境の名乗りを消す(自分のものだけ)。TraceFlag は消さない */
|
|
333
|
+
export async function releaseCollector(c) {
|
|
334
|
+
if (await c.deps.lock.take(LOCK_DIR, 50)) {
|
|
335
|
+
try {
|
|
336
|
+
if (kvGet(read(c.box, COLLECTOR_F), "PID") === String(c.deps.pid)) {
|
|
337
|
+
c.box.files.removeFile(COLLECTOR_F);
|
|
338
|
+
if (kvGet(read(c.box, CLAIM_F), "RUNTIME") === RUNTIME_SELF)
|
|
339
|
+
c.box.files.removeFile(CLAIM_F);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
finally {
|
|
343
|
+
c.deps.lock.drop(LOCK_DIR);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
note(c.box, "INFO", `取得役: ${c.quit !== "" ? c.quit : "止まりました"}`);
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* 取得役を、止まるまで(signal の中断・止まる理由)繰り返す。例外は、記録して止まる(ランチャーを止めない)。
|
|
350
|
+
* wait: 次の回まで待つ(中断で早く戻る)
|
|
351
|
+
*/
|
|
352
|
+
export async function runCollector(c, signal, wait) {
|
|
353
|
+
try {
|
|
354
|
+
for (let first = true; !signal.aborted; first = false) {
|
|
355
|
+
if (!first)
|
|
356
|
+
await wait();
|
|
357
|
+
if (signal.aborted)
|
|
358
|
+
break;
|
|
359
|
+
await collectorStep(c);
|
|
360
|
+
if (c.quit !== "")
|
|
361
|
+
break;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
catch (e) {
|
|
365
|
+
c.quit = `思わぬ失敗で止まりました(${e instanceof Error ? e.message : String(e)})`;
|
|
366
|
+
}
|
|
367
|
+
finally {
|
|
368
|
+
await releaseCollector(c);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// ログの 1 行の形(今のシェル版と同じ。AI がログを読むため: doc/rebuild-design.md 5.3・4.7)と、画面の表示の形
|
|
2
|
+
const two = (n) => String(n).padStart(2, "0");
|
|
3
|
+
/** 手元の時刻の「YYYY-MM-DD HH:MM:SS」 */
|
|
4
|
+
export function timestamp(date) {
|
|
5
|
+
const day = `${date.getFullYear()}-${two(date.getMonth() + 1)}-${two(date.getDate())}`;
|
|
6
|
+
return `${day} ${two(date.getHours())}:${two(date.getMinutes())}:${two(date.getSeconds())}`;
|
|
7
|
+
}
|
|
8
|
+
/** ログファイルに書く 1 件(末尾の改行を含む) */
|
|
9
|
+
export function fileLine(ts, level, message) {
|
|
10
|
+
if (level === "HEADER")
|
|
11
|
+
return `\n[${ts}] [=== ${message} ===]\n`;
|
|
12
|
+
if (level === "CMD")
|
|
13
|
+
return `[${ts}] [CMD] Command: ${message}\n`;
|
|
14
|
+
return `[${ts}] [${level}] ${message}\n`;
|
|
15
|
+
}
|
|
16
|
+
const colors = { HEADER: 36, INFO: 36, SUCCESS: 32, WARNING: 33, ERROR: 31, FATAL: 31 };
|
|
17
|
+
/** 画面に出す行 */
|
|
18
|
+
export function screenLines(level, message, color) {
|
|
19
|
+
const paint = (text) => {
|
|
20
|
+
const code = colors[level];
|
|
21
|
+
return color && code !== undefined ? `\u001b[${code}m${text}\u001b[0m` : text;
|
|
22
|
+
};
|
|
23
|
+
if (level === "CMD")
|
|
24
|
+
return [`> Command: ${message}`];
|
|
25
|
+
if (level === "HEADER") {
|
|
26
|
+
const rule = "-".repeat(56);
|
|
27
|
+
return [rule, paint(`>> ${message}`), rule];
|
|
28
|
+
}
|
|
29
|
+
return [paint(`[${level}] ${message}`)];
|
|
30
|
+
}
|
|
31
|
+
// 端末の色などの制御と、絵文字。範囲は今の _strip_term_codes と同じ
|
|
32
|
+
// (U+2000〜U+2FFF・U+FE0F・U+10000〜U+3FFFF。… や — もここに入る)
|
|
33
|
+
const escapeSequence = new RegExp(`${String.fromCharCode(27)}\\[[0-9;?]*[a-zA-Z]`, "g");
|
|
34
|
+
const pictographs = /[\u{2000}-\u{2FFF}]|\u{FE0F}|[\u{10000}-\u{3FFFF}]/gu;
|
|
35
|
+
export function stripTermCodes(text) {
|
|
36
|
+
return text.replace(escapeSequence, "").replace(pictographs, "");
|
|
37
|
+
}
|
|
38
|
+
/** 各行に接頭辞を付ける(空行には付けない)。最後の行に改行がなくても、改行を付ける */
|
|
39
|
+
export function prefixLines(prefix, text) {
|
|
40
|
+
if (text === "")
|
|
41
|
+
return "";
|
|
42
|
+
const lines = text.replaceAll("\r\n", "\n").replace(/\n$/, "").split("\n");
|
|
43
|
+
return lines.map((line) => (line === "" ? "\n" : `${prefix}${line}\n`)).join("");
|
|
44
|
+
}
|