Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1jehuang Jcode pruneOldEvents

From Leeroopedia
Knowledge Sources
Domains Telemetry, Data_Pipeline
Last Updated 2026-10-18 00:00 GMT

Overview

Concrete tool for pruning old telemetry events from D1 to reclaim space, with scheduled and emergency modes, provided by the telemetry worker.

Description

The pruneOldEvents function runs on a nightly cron schedule to delete high-volume event rows older than retention windows. The emergencyPrune function triggers when D1 size exceeds the soft limit (8 GB): retention windows are halved and rows are deleted with a rate-limit per isolate to avoid overwhelming D1. Both functions also clean up oauth_accounts entries. Emergency pruning has a cooldown to prevent repeated triggers.

Usage

  • Scheduled prune: Runs nightly via Cloudflare Workers cron trigger.
  • Emergency prune: Triggered when observeDbSize detects D1 size over the soft limit.

Code Reference

Source Location

  • Repository: jcode
  • File: telemetry-worker/src/worker.js

Signature

async function pruneOldEvents(env, options?)
async function emergencyPrune(env)

// Constants:
const D1_SOFT_LIMIT_BYTES = 8_000_000_000;
const EMERGENCY_PRUNE_COOLDOWN_MS = /* ... */;

Import

// Internal worker functions, called from the scheduled handler.
// telemetry-worker/src/worker.js

I/O Contract

Inputs

Name Type Required Description
env Env Yes /// Cloudflare Worker environment with D1 binding
options object No /// Prune options (retention windows, batch size)

Outputs

Name Type Description
D1 rows deleted /// High-volume event rows and oauth_accounts cleaned
free pages reclaimed /// D1 free-page pool recycled

Usage Examples

Scheduled Handler

export default {
  async scheduled(event, env, ctx) {
    // Nightly cron: prune old events
    ctx.waitUntil(pruneOldEvents(env));
  },
};

Related Pages

Implements Principle

Requires Environment

Uses Heuristics

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment