MCPcopy
hub / github.com/xpf0000/FlyEnv / UnixSystemScheduler

Class UnixSystemScheduler

src/fork/module/Cron/UnixSystemScheduler.ts:18–355  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16} from './utils'
17
18export class UnixSystemScheduler {
19 constructor(private cronRoot: string) {}
20
21 private runLogPath(jobId: string): string {
22 return runLogPath(this.cronRoot, jobId)
23 }
24
25 private taskScriptPath(jobId: string): string {
26 return taskScriptPath(this.cronRoot, jobId, 'sh')
27 }
28
29 private resolveShell(): string {
30 const fallback = isMacOS() ? '/bin/zsh' : '/bin/bash'
31 const shell = process.env.SHELL || fallback
32 return existsSync(shell) ? shell : fallback
33 }
34
35 private async resolvePath(): Promise<string> {
36 try {
37 const env = (await EnvSync.sync()) ?? {}
38 return `${env.PATH || env.Path || process.env.PATH || ''}`
39 } catch {
40 return process.env.PATH || ''
41 }
42 }
43
44 private async writeWrapper(job: CronJob): Promise<string> {
45 const file = this.taskScriptPath(job.id)
46 const workDir = job.workDir || homePath()
47 const runDir = join(this.cronRoot, 'tmp')
48 const logFile = this.runLogPath(job.id)
49 const shell = this.resolveShell()
50 const envPath = await this.resolvePath()
51 const scope = job.scope ?? (job.hostId ? 'host' : 'global')
52 const shellHostId = '${HOST_ID:-null}'
53
54 const content = `#!/bin/sh
55JOB_ID=${shellQuote(job.id)}
56HOST_ID=${shellQuote(job.hostId ? `${job.hostId}` : '')}
57SCOPE=${shellQuote(scope)}
58COMMAND=${shellQuote(job.command)}
59WORK_DIR=${shellQuote(workDir)}
60RUN_DIR=${shellQuote(runDir)}
61LOG_FILE=${shellQuote(logFile)}
62RUN_SHELL=${shellQuote(shell)}
63PATH=${shellQuote(envPath)}
64export PATH
65LOCK_DIR="$RUN_DIR/$JOB_ID.lock"
66
67mkdir -p "$RUN_DIR" "$(dirname "$LOG_FILE")"
68if ! mkdir "$LOCK_DIR" 2>/dev/null; then
69 exit 0
70fi
71
72RUN_ID="$(date +%Y%m%d%H%M%S)-$$"
73OUT_FILE="$RUN_DIR/$JOB_ID-$RUN_ID.out"
74ERR_FILE="$RUN_DIR/$JOB_ID-$RUN_ID.err"
75STARTED_AT=$(($(date +%s) * 1000))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected