perf(scenario-scheduler): parallelize stale-run cleanup on startup with effect

- use Effect.forEach with concurrency:10 to fail up to 10 runs in parallel
- isolate each failRun with catchAll so one failure doesn't abort the rest
- log unexpected failRun errors per run without halting the batch
This commit is contained in:
2026-04-17 14:32:25 +03:00
parent 2a46b355db
commit a199700878
3 changed files with 73 additions and 5 deletions
+1
View File
@@ -32,6 +32,7 @@
"better-sqlite3": "^12.8.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.4",
"effect": "^3.21.0",
"jsdom": "^29.0.2",
"playwright": "^1.59.1",
"reflect-metadata": "^0.2.2",
@@ -2,6 +2,7 @@ import { Injectable, OnModuleInit } from "@nestjs/common";
import { Interval } from "@nestjs/schedule";
import { InjectRepository } from "@nestjs/typeorm";
import * as crypto from "crypto";
import { Effect } from "effect";
import type { Browser, BrowserContext, Page } from "playwright";
import { chromium } from "playwright";
import { Repository } from "typeorm";
@@ -76,11 +77,29 @@ export class ScenarioSchedulerService implements OnModuleInit {
this.logger.error(
`Run #${run.id}: marked fail due to server restart (playwright session lost)`,
);
await this.failRun(
run.id,
"Run aborted: server restarted and playwright session could not be recovered",
);
}
await Effect.runPromise(
Effect.forEach(
staleRuns,
(run) =>
Effect.tryPromise(() =>
this.failRun(
run.id,
"Run aborted: server restarted and playwright session could not be recovered",
),
).pipe(
Effect.catchAll((err) =>
Effect.sync(() =>
this.logger.error(
`Run #${run.id}: failRun threw unexpectedly — ${String(err)}`,
),
),
),
),
{ concurrency: 10 },
),
);
}
/**