Skip to content

Commit

Permalink
fix: upgrade listr2 prevent OOM errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tomastrajan committed Apr 19, 2023
1 parent 82b0e7c commit fb5836f
Show file tree
Hide file tree
Showing 10 changed files with 583 additions and 198 deletions.
2 changes: 0 additions & 2 deletions lib/batch/tasks/init-job-repo.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
cloneRepo,
pullLatest,
} from '../../services/git.service';
import { wait } from '../../utils/wait';
import { Context } from '../../interface';
import {
directoryExists,
Expand Down Expand Up @@ -37,7 +36,6 @@ export function initJobRepo(job: string): ListrTask {
}
process.chdir(repoPath);
} catch (error: any) {
await wait();
throw error;
}
},
Expand Down
4 changes: 1 addition & 3 deletions lib/batch/tasks/init-job-state.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { ListrTask } from 'listr2';

import { directoryExists } from '../../services/fs.service';
import { Context } from '../../interface';
import { wait } from '../../utils/wait';

export const initJobStateTask: ListrTask = {
title: 'Init job state',
task: async (ctx: Context, task) => {
await task.task.pause(200);
// reset job CTX state
ctx.control = { skipEverySubsequentTask: false };
ctx.results = { checks: {} };
Expand All @@ -17,8 +17,6 @@ export const initJobStateTask: ListrTask = {
process.chdir('../../');
}

await wait();

task.title = `${task.title} successful`;
},
};
10 changes: 6 additions & 4 deletions lib/batch/tasks/run-job.task.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ListrTask } from 'listr2';

import { wait } from '../../utils/wait';
import { Context } from '../../interface';
import { writeJson } from '../../services/fs.service';
import { getRepoNameFromUrl } from '../../services/git.service';
Expand All @@ -14,11 +13,14 @@ import { initJobStateTask } from './init-job-state.task';
import { finalizeJobTaskFactory } from './finalize-job.task';
import { batchSaveProjectJsonTaskFactory } from './batch-save-project-json.task';

export function runJobTaskFactory(job: string): ListrTask {
export function runJobTaskFactory(
job: string,
index: number,
total: number
): ListrTask {
return {
title: `${getRepoNameFromUrl(job)}`,
title: `${index} / ${total} - ${getRepoNameFromUrl(job)}`,
rollback: async (ctx: Context, task) => {
await wait();
// update batch state
if (!ctx.options.preserveQueue) {
ctx.batch.failed.push(job);
Expand Down
4 changes: 3 additions & 1 deletion lib/batch/tasks/run-jobs.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export const runJobsTask: ListrTask = {
skip: (ctx: Context) => ctx.control.skipEverySubsequentTask,
task: async (ctx: Context, task) =>
task.newListr(
ctx.batch.queue.map((queuedJob) => runJobTaskFactory(queuedJob)),
ctx.batch.queue.map((queuedJob, index) =>
runJobTaskFactory(queuedJob, index + 1, ctx.batch.queue.length)
),
{
exitOnError: false,
exitAfterRollback: false,
Expand Down
3 changes: 3 additions & 0 deletions lib/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export interface Context {
};
handledCheckFailures: Error[];
batch: Batch;
debug: {
[key: string]: any;
};
}

export interface Batch {
Expand Down
8 changes: 5 additions & 3 deletions lib/tasks/save-project-api.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ListrTask } from 'listr2';
import * as api from '../services/api.service';
import { Context } from '../interface';
import { getHumanReadableFileSize } from '../services/fs.service';
import { wait } from '../utils/wait';

export const saveProjectApiTask: ListrTask = {
title: 'Save project results (Omniboard.dev)',
Expand All @@ -19,10 +18,13 @@ export const saveProjectApiTask: ListrTask = {
}
},
task: async (ctx, task) => {
await wait();
return api.uploadProject(ctx.results).then(() => {
const resultsLength = Buffer.byteLength(
JSON.stringify(ctx.results),
'utf8'
);
task.title = `${task.title} successful, ${getHumanReadableFileSize(
Buffer.byteLength(JSON.stringify(ctx.results), 'utf8')
resultsLength
)}`;
});
},
Expand Down
9 changes: 7 additions & 2 deletions lib/utils/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ export const runner = async (
results: { checks: {} },
handledCheckFailures: [],
batch: { queue: [], completed: [], failed: [] },
debug: {},
};
await new Listr(tasks, {
rendererFallback: () => options.verbose,
fallbackRenderer: 'verbose',
rendererOptions: {
collapse: false,
showTimer: true,
formatOutput: 'wrap',
},
renderer: options.silent ? 'silent' : 'default',
renderer: options.silent
? 'silent'
: options.verbose
? 'verbose'
: 'default',
})
.run(context)
.then(() => {
Expand Down
4 changes: 0 additions & 4 deletions lib/utils/wait.ts

This file was deleted.

Loading

0 comments on commit fb5836f

Please sign in to comment.