graceful-shutdown

javascript
// Express app with health checks and graceful shutdown
const express = require('express');
const { createServer } = require('http');

const app = express();
const server = createServer(app);

Container health checks and graceful shutdown patterns

docker kubernetes health-checks
by Ryan Nakamura 1 tab
typescript
import { Pool, PoolClient, Client, QueryResult, QueryResultRow } from 'pg';

const MAX_LIFETIME_MS = 30 * 60 * 1000;

export const pool = new Pool({
  connectionString: process.env.DATABASE_URL,

Postgres connection pooling with pg + max lifetime

node postgres connection-pooling
by codesnips 3 tabs
typescript
export type CheckResult = { name: string; status: 'up' | 'down'; durationMs: number; error?: string };
export type Check = () => Promise<void>;

function withTimeout(fn: Check, ms: number): Promise<void> {
  return new Promise((resolve, reject) => {
    const timer = setTimeout(() => reject(new Error(`timeout after ${ms}ms`)), ms);

Health checks with readiness + liveness

reliability fastify kubernetes
by codesnips 3 tabs
go
package workpool

import (
	"context"
	"sync"
)

Bounded Worker Pool Processing Jobs from a Buffered Channel in Go

go concurrency worker-pool
by codesnips 3 tabs
java
package com.example.orders;

public final class Order {

    public static final Order POISON_PILL = new Order(-1L, 0.0);

Producer/Consumer Order Processing With a Bounded BlockingQueue in Java

java concurrency blockingqueue
by codesnips 3 tabs
javascript
function createReadiness() {
  let ready = true;

  function markUnready() {
    ready = false;
  }

Graceful HTTP Server Shutdown on SIGTERM With In-Flight Request Draining in Node.js

nodejs http graceful-shutdown
by codesnips 3 tabs
go
package worker

import "context"

func (p *Pool) Submit(job Job) error {
	// Reject fast if draining, otherwise enqueue with backpressure.

Graceful Drain of In-Flight Jobs Before Worker Shutdown in Go

go graceful-shutdown concurrency
by codesnips 3 tabs
java
@Configuration
@EnableScheduling
@EnableConfigurationProperties(CleanupProperties.class)
public class SchedulingConfig {

    @Bean(destroyMethod = "shutdown")

Recurring Cleanup Job with Spring @Scheduled and a Shutdown-Aware Executor

spring spring-boot scheduling
by codesnips 3 tabs
go
package scheduler

import (
	"context"
	"log"
	"sync"

Graceful Cron-Style Scheduler in Go With Ticker and Context Cancellation

scheduler ticker context
by codesnips 3 tabs
typescript
import express, { type Express, type Request, type Response } from 'express';

export function buildApp(isShuttingDown: () => boolean): Express {
  const app = express();
  app.disable('x-powered-by');

Graceful shutdown for Node HTTP servers

reliability nodejs express
by codesnips 3 tabs
python
import queue
import time
from dataclasses import dataclass, field, replace
from typing import Any, Dict, Tuple

In-Process Threaded Background Job Queue for Sending Emails Without Redis

background-jobs threading queue
by codesnips 4 tabs
rust
use std::time::Duration;
use tokio_util::sync::CancellationToken;

pub struct Worker {
    id: usize,
    token: CancellationToken,

Graceful Task Shutdown in Tokio Using CancellationToken

tokio async cancellation
by codesnips 3 tabs