Expire something

Run cleanup the moment a deadline passes. Invitations, trials, holds.

Use this to
  • Expire a team invitation 7 days after it was sent
  • Downgrade an account to the free plan when its 14-day trial ends
  • Release a held seat 30 minutes after a user started checkout
  • Mark an upload as failed if processing hasn't completed within 10 minutes
Code
Schedule the expiration
await dk.schedule("expire-trial", {
  key: "acct_456",
  delay: "14d",
});
When the time comes, handle the expiration
dk.handle("expire-trial", async ({ key }) => {
  const acct = await db.accounts.find(key);
  if (acct.plan !== "trial") return; // already upgraded, skip
  await db.accounts.update(key, { plan: "free" });
  await sendEmail(acct.email, "Your trial has ended");
});
Optionally cancel if they upgrade early
await dk.unschedule("expire-trial", "acct_456");
npm install delaykit