Phestus v0.1 is just weeks away from being released! Explore the latest modules and documentation.Learn more

Lifecycle Management

Starting Phestus

Once your PhestusConfig has been created, you can create and start a Phestus instance.

Creating the Phestus instance registers the configured modules, plugins, and providers. The runtime is started by calling initialize().

import { Phestus } from '@phestus/core'

const phestus = new Phestus({
    service,
    logger,
    eventBus,

    modules: [
        queue,
        event,
    ],

    plugins: [],
})

await phestus.initialize()

The initialize() method starts the Phestus runtime and initializes the registered components in the appropriate order.

Modules and providers can use their initialize() lifecycle methods to establish connections, start consumers, register subscriptions, or perform other startup tasks.

Managing the Runtime

After initialization, the Phestus instance can be used to access registered modules, providers, and plugins.

const queue = phestus.getModule('queue')
const event = phestus.getModule('event')

You can also check the current runtime state:

const state = phestus.getState()

Shutting Down Phestus

When the application is shutting down, call shutdown() to allow Phestus and its registered components to clean up their resources.

await phestus.shutdown()

This allows modules and providers to close connections, stop consumers, remove subscriptions, and perform other cleanup tasks.

A simple application lifecycle may therefore look like:

const phestus = new Phestus(config)

await phestus.initialize()

// Application is running...

await phestus.shutdown()

Application Integration

In a long-running application, Phestus can be initialized when the application starts and shut down when the application receives a termination signal.

const phestus = new Phestus(config)

await phestus.initialize()

process.on('SIGTERM', async () => {
    await phestus.shutdown()
    process.exit(0)
})

Phestus manages the lifecycle of its own modules, providers, and plugins while the application remains responsible for determining when the runtime should start and stop.