Cron Jobs

Schedule automated tasks like sending reminder emails with ShipAhead’s cron setup.

Tools

  • cron-job.org: External service to schedule jobs by calling your API endpoints.

Setup & Run Cron Jobs

  1. Set CRON_SECRET
    • Add an environment variable to secure your cron endpoints:
      .env
      CRON_SECRET="your-secret-key"
      
    • Use any strong key you like or generate one with a password generator.
  2. Add a Cron Job
    • Create a file in /server/jobs/, e.g., sendReminderEmails.ts:
      server/jobs/sendReminderEmails.ts
      export async function sendReminderEmails() {
        console.log('[CRON] Running job...');
        // Add your logic (DB updates, notifications, etc.)
      }
      
    • Register it in server/jobs/list.ts:
      server/jobs/list.ts
      import { sendReminderEmails } from './sendReminderEmails';
      
      const JOBS: any = { sendReminderEmails };
      
      export function getJobModules(name: string) {
        return JOBS[name] ?? null;
      }
      
  3. Schedule the Job
    • Trigger your job via the API endpoint:
      https://your-site.com/api/cron/sendReminderEmails
      
    • Create a cron-job.org task:
      • URL: same as above
      • Execution schedule: e.g., 0 8 * * * (runs daily at 8 AM)
      • HTTP Header:
        X-Cron-Secret: your-secret-key
        
  4. Verify
    • Check server logs for [CRON] messages.
    • Or test endpoint directly with curl:
      curl -H "X-Cron-Secret: your-secret-key" https://your-site.com/api/cron/sendReminderEmails
      

Why Use cron-job.org?

  • No need to install node-cron or run background workers.
  • Works with serverless platforms like Vercel, Netlify, or Cloudflare.
  • Secure with your custom X-Cron-Secret.
  • Easy to schedule and manage without touching your app’s main code.

Example: Adding a New Job

  • Just create a new file in /server/jobs/, register it in list.ts, and schedule it on cron-job.org like above.
  • You can add unlimited jobs following the same pattern.