Skip to content

Commit

Permalink
Prefix all shell scripts with job id and remove cleanup after executi…
Browse files Browse the repository at this point in the history
…on to prevent Text file busy errors
  • Loading branch information
firecow committed Aug 14, 2023
1 parent dde0363 commit 1a04b88
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
25 changes: 18 additions & 7 deletions src/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class Job {
private _jobNamePad: number | null = null;

private _containersToClean: string[] = [];
private _filesToRm: string[] = [];
private _startTime?: [number, number];
private _endTime?: [number, number];

Expand Down Expand Up @@ -534,6 +535,12 @@ export class Job {
}
}

const rmPromises = [];
for (const file of this._filesToRm) {
rmPromises.push(fs.rm(file, {recursive: true, force: true}));
}
await Promise.all(rmPromises);

const fileVariablesDir = this.fileVariablesDir;
try {
await fs.rm(fileVariablesDir, {recursive: true, force: true});
Expand Down Expand Up @@ -650,7 +657,7 @@ export class Job {
dockerCmd += `--add-host=${extraHost} `;
}

const entrypointFile = `${cwd}/${stateDir}/scripts/image_entry/${safeJobName}`;
const entrypointFile = `${cwd}/${stateDir}/scripts/image_entry/${safeJobName}_${this.jobId}`;
if (this.imageEntrypoint) {
if (this.imageEntrypoint[0] == "") {
dockerCmd += "--entrypoint '' ";
Expand All @@ -660,6 +667,7 @@ export class Job {
await fs.chmod(entrypointFile, "0755");
dockerCmd += "--entrypoint '/gcl-entry' ";
await fs.appendFile(entrypointFile, " \"$@\"\n");
this._filesToRm.push(entrypointFile);
}
}

Expand Down Expand Up @@ -706,14 +714,16 @@ export class Job {

cmd += "exit 0\n";

await fs.outputFile(`${cwd}/${stateDir}/scripts/${safeJobName}`, cmd, "utf-8");
await fs.chmod(`${cwd}/${stateDir}/scripts/${safeJobName}`, "0755");
const jobScriptFile = `${cwd}/${stateDir}/scripts/${safeJobName}_${this.jobId}`;
await fs.outputFile(jobScriptFile, cmd, "utf-8");
await fs.chmod(jobScriptFile, "0755");
this._filesToRm.push(jobScriptFile);

if (this.imageName) {
await Utils.spawn(["docker", "cp", `${stateDir}/scripts/${safeJobName}`, `${this._containerId}:/gcl-cmd`], cwd);
await Utils.spawn(["docker", "cp", `${stateDir}/scripts/${safeJobName}_${this.jobId}`, `${this._containerId}:/gcl-cmd`], cwd);
}
if (this.imageEntrypoint && this.imageEntrypoint[0] != "") {
await Utils.spawn(["docker", "cp", `${stateDir}/scripts/image_entry/${safeJobName}`, `${this._containerId}:/gcl-entry`], cwd);
await Utils.spawn(["docker", "cp", `${stateDir}/scripts/image_entry/${safeJobName}_${this.jobId}`, `${this._containerId}:/gcl-entry`], cwd);
}

const cp = execa(this._containerId ? `docker start --attach -i ${this._containerId}` : "bash", {
Expand Down Expand Up @@ -744,7 +754,7 @@ export class Job {
if (this.imageName) {
cp.stdin?.end("/gcl-cmd");
} else {
cp.stdin?.end(`./${stateDir}/scripts/${safeJobName}`);
cp.stdin?.end(`./${stateDir}/scripts/${safeJobName}_${this.jobId}`);
}
});

Expand Down Expand Up @@ -1042,7 +1052,7 @@ export class Job {
}

const serviceEntrypoint = service.entrypoint;
const serviceEntrypointFile = `${cwd}/${stateDir}/scripts/services_entry/${safeJobName}_${serviceNameWithoutVersion}_${serviceIndex}`;
const serviceEntrypointFile = `${cwd}/${stateDir}/scripts/services_entry/${safeJobName}_${serviceNameWithoutVersion}_${serviceIndex}_${this.jobId}`;
if (serviceEntrypoint) {
if (serviceEntrypoint[0] == "") {
dockerCmd += "--entrypoint '' ";
Expand All @@ -1051,6 +1061,7 @@ export class Job {
await fs.appendFile(serviceEntrypointFile, `${serviceEntrypoint.join(" ")}`);
await fs.appendFile(serviceEntrypointFile, " \"$@\"\n");
await fs.chmod(serviceEntrypointFile, "0755");
this._filesToRm.push(serviceEntrypointFile);
dockerCmd += "--entrypoint '/gcl-entry' ";
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test-cases/parallel-matrix/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pre-job:
script:
- echo "Pre-job"

# @NoArtifactsToSource
build-job:
stage: build
image: alpine:edge
script:
- echo "NAME:'${NAME}' $CI_NODE_INDEX/$CI_NODE_TOTAL"
- echo "NAME:'${NAME}' $CI_NODE_INDEX/$CI_NODE_TOTAL"
Expand All @@ -21,9 +21,9 @@ build-job:
- NAME: [foo, bar]
- NAME: beb

# @NoArtifactsToSource
test-job:
stage: test
image: alpine:edge
needs: [build-job, pre-job]
script:
- echo "NAME:'${NAME}' TIER:'${TIER}' $CI_NODE_INDEX/$CI_NODE_TOTAL"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ test.concurrent("parallel-matrix - ensure successful", async () => {
await handler({
cwd: "tests/test-cases/parallel-matrix",
maxJobNameLength: 0,
shellIsolation: true,
}, writeStreams);

const expected = [
Expand All @@ -35,6 +36,7 @@ test.concurrent("parallel-matrix <test-job>", async () => {
await handler({
cwd: "tests/test-cases/parallel-matrix",
jobs: ["test-job"],
shellIsolation: true,
}, writeStreams);

const expected = [
Expand All @@ -59,6 +61,7 @@ test.concurrent("parallel-matrix 'test-job [beb]'", async () => {
await handler({
cwd: "tests/test-cases/parallel-matrix",
job: ["test-job: [beb]"],
shellIsolation: true,
}, writeStreams);

expect(writeStreams.stdoutLines).toEqual(expect.arrayContaining([
Expand Down

0 comments on commit 1a04b88

Please sign in to comment.