Skip to main content

dispose

@rotorsoft/act-root


@rotorsoft/act-root / act/src / dispose

Function: dispose()

dispose(disposer?): (code?) => Promise<void>

Defined in: libs/act/src/ports.ts:177

Registers resource cleanup functions for graceful shutdown.

Disposers are called automatically when the process exits (SIGINT, SIGTERM) or when manually triggered. They execute in reverse registration order, allowing proper cleanup of dependent resources.

Act automatically disposes registered stores and adapters. Use this function to register additional cleanup for your own resources (database connections, file handles, timers, etc.).

Parameters

disposer?

Disposer

Async function to call during cleanup

Returns

Function to manually trigger disposal and exit

(code?): Promise<void>

Parameters

code?

"ERROR" | "EXIT"

Returns

Promise<void>

Examples

import { dispose } from "@rotorsoft/act";

const redis = createRedisClient();

dispose(async () => {
console.log("Closing Redis connection...");
await redis.quit();
});

// On SIGINT/SIGTERM, Redis will be cleaned up automatically
import { dispose } from "@rotorsoft/act";

const db = connectDatabase();
dispose(async () => {
console.log("Closing database...");
await db.close();
});

const cache = connectCache();
dispose(async () => {
console.log("Closing cache...");
await cache.disconnect();
});

// On exit: cache closes first, then database
import { dispose } from "@rotorsoft/act";

const shutdown = dispose(async () => {
await cleanup();
});

// Manually trigger cleanup and exit
process.on("SIGUSR2", async () => {
console.log("Manual shutdown requested");
await shutdown("EXIT");
});
import { dispose } from "@rotorsoft/act";

dispose(async () => {
try {
await expensiveCleanup();
} catch (error) {
console.error("Cleanup failed:", error);
// Error doesn't prevent other disposers from running
}
});

See