Events

Lifecycle and runtime events for contexts and routes.

Subscribing to events

Use context.on(event, handler) to subscribe. Handlers receive { ts, context, details }.

import { context } from '@routecraft/routecraft'
import routes from './routes'

const ctx = context()
  .routes(routes)
  .build()

ctx.on('contextStarting', ({ ts }) => {
  console.log('Context starting at', ts)
})

ctx.on('routeStarted', ({ details: { route } }) => {
  console.log(`Route ${route.definition.id} started`)
})

ctx.on('error', ({ details: { error } }) => {
  console.error('Error occurred:', error)
})

await ctx.start()

Event signature

All events follow { ts, context, details } where:

  • ts: ISO timestamp string for when the event occurred
  • context: The CraftContext instance
  • details: Event-specific data (varies by event)

Context events

EventDescriptionDetails
contextStartingContext is beginning startup{}
contextStartedContext has completed startup{}
contextStoppingContext is beginning shutdown{ reason }
contextStoppedContext has fully stopped{}

Route events

EventDescriptionDetails
routeRegisteredRoute has been registered{ route }
routeStartingRoute is about to start{ route }
routeStartedRoute has started successfully{ route }
routeStoppingRoute is stopping{ route, reason, exchange? }
routeStoppedRoute has stopped{ route, exchange? }

System events

EventDescriptionDetails
errorAny error occurred in the system{ error, route?, exchange? }

For monitoring patterns and plugin examples, see /docs/introduction/monitoring and /docs/reference/plugins.