summaryrefslogtreecommitdiff
path: root/u/process/signals.ts
diff options
context:
space:
mode:
Diffstat (limited to 'u/process/signals.ts')
-rw-r--r--u/process/signals.ts49
1 files changed, 0 insertions, 49 deletions
diff --git a/u/process/signals.ts b/u/process/signals.ts
deleted file mode 100644
index c4feb7a..0000000
--- a/u/process/signals.ts
+++ /dev/null
@@ -1,49 +0,0 @@
-import {
- Either,
- IEither,
- IMetric,
- ITraceable,
- LogMetricTrace,
- LogMetricTraceSupplier,
- Mapper,
- Metric,
- Optional,
- ResultMetric,
- SideEffect,
- TraceUtil,
-} from '@emprespresso/pengueno';
-
-export const SigIntMetric = Metric.fromName('SigInt').asResult();
-export const SigTermMetric = Metric.fromName('SigTerm').asResult();
-
-export interface Closeable<TFailure> {
- readonly close: SideEffect<SideEffect<TFailure | undefined>>;
-}
-
-export class Signals {
- public static async awaitClose<E extends Error>(
- t: ITraceable<Closeable<E>, LogMetricTraceSupplier>,
- ): Promise<IEither<Error, void>> {
- const success: IEither<Error, void> = Either.right(<void>undefined);
- return new Promise<IEither<Error, void>>((res) => {
- const metricizedInterruptHandler = (metric: ResultMetric) => (err: Error | undefined) =>
- t
- .flatMap(TraceUtil.withMetricTrace(metric))
- .peek((_t) => _t.trace.trace('closing'))
- .move(
- Optional.from(err)
- .map((e) => Either.left<Error, void>(e))
- .orSome(() => success)
- .get(),
- )
- .flatMap(TraceUtil.traceResultingEither(metric))
- .map((e) => res(e.get()))
- .peek((_t) => _t.trace.trace('finished'))
- .get();
- const sigintCloser = metricizedInterruptHandler(SigIntMetric);
- const sigtermCloser = metricizedInterruptHandler(SigTermMetric);
- process.on('SIGINT', () => t.flatMap(TraceUtil.withTrace('SIGINT')).get().close(sigintCloser));
- process.on('SIGTERM', () => t.flatMap(TraceUtil.withTrace('SIGTERM')).get().close(sigtermCloser));
- });
- }
-}