forked from planetarium/Corvette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runHelpers.ts
102 lines (92 loc) · 2.8 KB
/
runHelpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {
type AmqpConnection,
type AmqpConnectOptions,
connect as connectAmqp,
} from "amqp/mod.ts";
import { parseOptions } from "amqp/src/amqp_connect_options.ts";
import type { Chain } from "npm:viem";
import type Prisma from "./prisma-shim.ts";
import { PrismaClient } from "./prisma-shim.ts";
import { Awaitable } from "./concurrencyUtils.ts";
import {
AmqpBrokerUrlEnvKey,
ChainDefinitionUrlEnvKey,
combinedEnv,
DatabaseUrlEnvKey,
} from "./envUtils.ts";
import { importESOrJson } from "./moduleUtils.ts";
export type CleanupFunction = () => Awaitable<void>;
type Runnable = {
runningPromise: Awaitable<unknown>;
cleanup?: CleanupFunction;
};
export async function block(signal?: AbortSignal) {
let intervalHandle: number;
if (signal) signal.onabort = () => clearInterval(intervalHandle);
return await new Promise((resolve) => {
intervalHandle = setInterval(resolve, 0x7fffffff);
});
}
export async function runAndCleanup(func: () => Awaitable<Runnable>) {
let cleanup: CleanupFunction | undefined;
try {
const { runningPromise, cleanup: abort } = await func();
cleanup = abort;
await runningPromise;
} finally {
if (cleanup) await cleanup();
}
}
export async function runWithPrisma(
func: (prisma: PrismaClient) => Awaitable<Runnable>,
optionsArg?: Prisma.PrismaClientOptions,
) {
optionsArg = optionsArg || {};
const prisma = new PrismaClient({
datasources: {
db: {
url: combinedEnv[DatabaseUrlEnvKey],
},
},
...optionsArg,
});
try {
await runAndCleanup(() => func(prisma));
} finally {
await prisma.$disconnect();
}
}
export async function runWithAmqp(
func: (amqpConnection: AmqpConnection) => Awaitable<Runnable>,
): Promise<void>;
export async function runWithAmqp(
func: (amqpConnection: AmqpConnection) => Awaitable<Runnable>,
options?: AmqpConnectOptions,
): Promise<void>;
export async function runWithAmqp(
func: (amqpConnection: AmqpConnection) => Awaitable<Runnable>,
uri?: string,
): Promise<void>;
export async function runWithAmqp(
func: (amqpConnection: AmqpConnection) => Awaitable<Runnable>,
optionsOrUrl?: AmqpConnectOptions | string,
): Promise<void> {
const defaultOptions = parseOptions(combinedEnv[AmqpBrokerUrlEnvKey]);
const options = optionsOrUrl === undefined
? defaultOptions
: typeof optionsOrUrl === "string"
? parseOptions(optionsOrUrl)
: parseOptions({ ...defaultOptions, ...optionsOrUrl });
const amqpConnection = await connectAmqp(options);
try {
await runAndCleanup(() => func(amqpConnection));
} finally {
await amqpConnection.close();
}
}
export async function runWithChainDefinition(
func: (chain: Chain) => Awaitable<Runnable>,
) {
const chain = await importESOrJson(combinedEnv[ChainDefinitionUrlEnvKey]);
await runAndCleanup(() => func(chain));
}