| 16 | } from './utils' |
| 17 | |
| 18 | export 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 |
| 55 | JOB_ID=${shellQuote(job.id)} |
| 56 | HOST_ID=${shellQuote(job.hostId ? `${job.hostId}` : '')} |
| 57 | SCOPE=${shellQuote(scope)} |
| 58 | COMMAND=${shellQuote(job.command)} |
| 59 | WORK_DIR=${shellQuote(workDir)} |
| 60 | RUN_DIR=${shellQuote(runDir)} |
| 61 | LOG_FILE=${shellQuote(logFile)} |
| 62 | RUN_SHELL=${shellQuote(shell)} |
| 63 | PATH=${shellQuote(envPath)} |
| 64 | export PATH |
| 65 | LOCK_DIR="$RUN_DIR/$JOB_ID.lock" |
| 66 | |
| 67 | mkdir -p "$RUN_DIR" "$(dirname "$LOG_FILE")" |
| 68 | if ! mkdir "$LOCK_DIR" 2>/dev/null; then |
| 69 | exit 0 |
| 70 | fi |
| 71 | |
| 72 | RUN_ID="$(date +%Y%m%d%H%M%S)-$$" |
| 73 | OUT_FILE="$RUN_DIR/$JOB_ID-$RUN_ID.out" |
| 74 | ERR_FILE="$RUN_DIR/$JOB_ID-$RUN_ID.err" |
| 75 | STARTED_AT=$(($(date +%s) * 1000)) |
nothing calls this directly
no outgoing calls
no test coverage detected