Cleanup constructor

const Cleanup()

Marks a method to be invoked after a pod is destroyed.

Jetleaf automatically detects methods annotated with @Cleanup and calls them after pod shutdown or context close.

This is typically used for:

  • Releasing resources (database connections, sockets, file handles).
  • Stopping background workers.
  • Flushing in-memory caches.

Rules

  • Must be a no-arg method.
  • Return type should be void or Future<void>.
  • Only applied to methods (not classes or fields).

Example — Synchronous Cleanup

@Service()
class CacheManager {
  final _cache = <String, String>{};

  void put(String key, String value) => _cache[key] = value;

  @Cleanup()
  void clearCache() {
    print("Clearing cache after shutdown...");
    _cache.clear();
  }
}

Example — Asynchronous Cleanup

@Service()
class WorkerPool {
  final _workers = <Worker>[];

  @Cleanup()
  Future<void> shutdownWorkers() async {
    for (final worker in _workers) {
      await worker.stop();
    }
  }
}

Implementation

const Cleanup();