Skip to content

secondsky/claude-skills / plugins/cloudflare-durable-objects/skills/cloudflare-durable-objects

cloudflare-durable-objects

Verified

Cloudflare Durable Objects for stateful coordination and real-time apps. Use for chat, multiplayer games, WebSocket hibernation, or encountering class export, migration, alarm errors.

FileKindSize
SKILL.mdskill md21.6 KB
references/advanced-sql-patterns.mdmarkdown15.8 KB
references/alarms-api.mdmarkdown6.5 KB
references/best-practices.mdmarkdown8.5 KB
references/common-patterns.mdmarkdown16.7 KB
references/data-modeling.mdmarkdown16.9 KB
references/error-codes.mdmarkdown7.7 KB
references/gradual-deployments.mdmarkdown13.9 KB
references/migration-cheatsheet.mdmarkdown3.2 KB
references/migrations-guide.mdmarkdown7.3 KB
references/monitoring-debugging.mdmarkdown18.2 KB
references/performance-optimization.mdmarkdown16.2 KB
references/rpc-metadata.mdmarkdown16.9 KB
references/rpc-patterns.mdmarkdown6.5 KB
references/security-best-practices.mdmarkdown15.9 KB
references/state-api-reference.mdmarkdown6.0 KB
references/stubs-routing.mdmarkdown10.6 KB
references/top-errors.mdmarkdown10.2 KB
references/typescript-config.mdmarkdown8.3 KB
references/vitest-testing.mdmarkdown18.1 KB
references/websocket-hibernation.mdmarkdown6.3 KB
references/wrangler-commands.mdmarkdown3.4 KB
scripts/check-versions.shexecutablescript593 B
scripts/migration-generator.shexecutablescript13.4 KB
scripts/setup-vitest-do.shexecutablescript10.2 KB
scripts/validate-do-config.shexecutablescript10.0 KB
templates/alarms-api-do.tsscript6.1 KB
templates/basic-do.tsscript2.9 KB
templates/gradual-deployment-config.jsoncother10.4 KB
templates/location-hints.tsscript6.8 KB
templates/multi-do-coordination.tsscript7.8 KB
templates/package.jsonmanifest384 B
templates/rpc-metadata-do.tsscript10.8 KB
templates/rpc-vs-fetch.tsscript6.0 KB
templates/state-api-patterns.tsscript5.8 KB
templates/ttl-cleanup-do.tsscript17.6 KB
templates/vitest-do-test.tsscript12.3 KB
templates/websocket-hibernation-do.tsscript6.0 KB
templates/wrangler-do-config.jsoncother2.0 KB

templates/alarms-api-do.ts

View on GitHub
/**
 * Alarms API Example: Batcher
 *
 * Demonstrates:
 * - storage.setAlarm() to schedule future tasks
 * - alarm() handler method
 * - Guaranteed at-least-once execution
 * - Retry behavior
 * - Idempotent alarm patterns
 */

import { DurableObject, DurableObjectState } from 'cloudflare:workers';

interface Env {
  BATCHER: DurableObjectNamespace<Batcher>;
  // Example: API to send batch to
  // API_ENDPOINT: string;
}

interface AlarmInfo {
  retryCount: number;
  isRetry: boolean;
}

export class Batcher extends DurableObject<Env> {
  buffer: string[];

  constructor(ctx: DurableObjectState, env: Env) {
    super(ctx, env);

    // Restore buffer from storage
    ctx.blockConcurrencyWhile(async () => {
      this.buffer = await ctx.storage.get<string[]>('buffer') || [];
      console.log(`Batcher constructor: restored ${this.buffer.length} items`);
    });
  }

  /**
   * Add item to batch
   */
  async addItem(item: string): Promise<void> {
    this.buffer.push(item);
    await this.ctx.storage.put('buffer', this.buffer);

    // Schedule alarm for 10 seconds from now (if not already set)
    const currentAlarm = await this.ctx.storage.getAlarm();

    if (currentAlarm === null) {
      // No alarm set - schedule one
      await this.ctx.storage.setAlarm(Date.now() + 10000);
      console.log(`Alarm scheduled for ${new Date(Date.now() + 10000).toISOString()}`);
    } else {
      console.log(`Alarm already scheduled for ${new Date(currentAlarm).toISOString()}`);
    }
  }

  /**
   * Alarm handler - called when alarm fires
   *
   * CRITICAL:
   * - Guaranteed at-least-once execution
   * - Retried up to 6 times with exponential backoff (2s, 4s, 8s, ...)
   * - Implement idempotent operations
   */
  async alarm(alarmInfo: AlarmInfo): Promise<void> {
    console.log(`Alarm fired (retry: ${alarmInfo.isRetry}, count: ${alarmInfo.retryCount})`);

    // Reload buffer from storage (may have changed since constructor)
    this.buffer = await this.ctx.storage.get<string[]>('buffer') || [];

    if (this.buffer.length === 0) {
      console.log('No items to process');
      return;  // Alarm will be deleted automatically
    }

    // Limit retries
    if (alarmInfo.retryCount > 3) {
      console.error('Alarm failed after 3 retries, giving up');
      // Still clear buffer to avoid infinite retries
      this.buffer = [];
      await this.ctx.storage.put('buffer', []);
      return;
    }

    try {
      // Process batch (idempotent operation)
      await this.processBatch(this.buffer);

      // Clear buffer after successful processing
      this.buffer = [];
      await this.ctx.storage.put('buffer', []);

      console.log('Batch processed successfully');

      // Alarm is automatically deleted after successful execution

    } catch (error) {
      console.error('Batch processing failed:', error);
      // Throwing error will trigger retry
      throw error;
    }
  }

  /**
   * Process batch - idempotent operation
   */
  private async processBatch(items: string[]): Promise<void> {
    console.log(`Processing batch of ${items.length} items:`, items);

    // Example: Send to external API
    // const response = await fetch(this.env.API_ENDPOINT, {
    //   method: 'POST',
    //   headers: { 'content-type': 'application/json' },
    //   body: JSON.stringify({ items }),
    // });
    //
    // if (!response.ok) {
    //   throw new Error(`API error: ${response.status}`);
    // }

    // Simulate processing
    await new Promise(resolve => setTimeout(resolve, 100));
  }

  /**
   * Get current alarm time (if set)
   */
  async getAlarmTime(): Promise<number | null> {
    return await this.ctx.storage.getAlarm();
  }

  /**
   * Cancel alarm
   */
  async cancelAlarm(): Promise<void> {
    await this.ctx.storage.deleteAlarm();
    console.log('Alarm cancelled');
  }
}

// CRITICAL: Export the class
export default Batcher;

/**
 * Alternative pattern: Periodic cleanup with alarms
 */
export class PeriodicCleaner extends DurableObject {
  constructor(ctx: DurableObjectState, env: any) {
    super(ctx, env);

    // Schedule alarm on first run
    ctx.blockConcurrencyWhile(async () => {
      const alarm = await ctx.storage.getAlarm();
      if (alarm === null) {
        // Schedule first cleanup in 1 hour
        await ctx.storage.setAlarm(Date.now() + 3600000);
      }
    });
  }

  /**
   * Periodic cleanup alarm
   */
  async alarm(): Promise<void> {
    console.log('Running periodic cleanup');

    try {
      // Cleanup expired data
      await this.cleanup();

      // Schedule next cleanup in 1 hour
      await this.ctx.storage.setAlarm(Date.now() + 3600000);

    } catch (error) {
      console.error('Cleanup failed:', error);
      throw error;  // Will retry
    }
  }

  private async cleanup(): Promise<void> {
    const now = Date.now();
    const oneDayAgo = now - (24 * 60 * 60 * 1000);

    // Get all keys
    const map = await this.ctx.storage.list();

    // Delete old entries
    const keysToDelete: string[] = [];

    for (const [key, value] of map.entries()) {
      if (typeof value === 'object' && value !== null && 'timestamp' in value) {
        if ((value as any).timestamp < oneDayAgo) {
          keysToDelete.push(key);
        }
      }
    }

    if (keysToDelete.length > 0) {
      await this.ctx.storage.delete(keysToDelete);
      console.log(`Deleted ${keysToDelete.length} old entries`);
    }
  }
}

/**
 * Alternative pattern: Reminder/notification with alarms
 */
export class ReminderDO extends DurableObject {
  async setReminder(message: string, fireAt: Date): Promise<void> {
    // Store reminder data
    await this.ctx.storage.put('reminder', { message, fireAt: fireAt.getTime() });

    // Schedule alarm
    await this.ctx.storage.setAlarm(fireAt);

    console.log(`Reminder set for ${fireAt.toISOString()}`);
  }

  async alarm(): Promise<void> {
    const reminder = await this.ctx.storage.get<{ message: string; fireAt: number }>('reminder');

    if (reminder) {
      console.log(`REMINDER: ${reminder.message}`);

      // Send notification (e.g., via email, webhook, etc.)
      // await this.sendNotification(reminder.message);

      // Clear reminder
      await this.ctx.storage.delete('reminder');
    }
  }
}
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall