JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Member-only story

How I Automated My Weekly Newsletter using Blitz.js and CRON

John Grisham
JavaScript in Plain English
4 min readAug 24, 2021

--

Photo by Brett Jordan on Unsplash

I recently discovered Blitz.js and love how quickly the framework lets you build an application but my favorite part is the command-line tool. Blitz.js has a console that can be used to run predefined queries and mutations via a node REPL environment which is great for manually running tasks from the command line, but less good when ran inside of CRON job since it will take over whatever process it’s running on.

I have a newsletter that collects news stories and I wanted to automate sending this newsletter every other week. I needed a CRON solution initiated via a bash script that wasn’t using expect commands so I started looking into the child_process module.

In case you’re new to Medium you can support my writing by signing up here:

Clearing old data

Blitz.js is built upon Next.js so hosting is flexible but for me, the winner is Render.com. I’m not affiliated with them but of the hosting platforms I’ve tried, they have been the cheapest and easiest to set up. This post won’t go in-depth with how to use Render for hosting, just be aware that is the host where I set up my CRON job.

Render offers a simple CRON service. I want mine to do two things once a week.

  1. Delete records from my database that are more than 6 days old
  2. Run my newsletter auto-mail mutation if the week is odd (every other week)

My bash script will be the entry point and will initiate these processes.

I use ts-node to make calls to other Typescript files here which will execute the core logic that I want to accomplish. Let’s take a look at the first one.

--

--

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Responses (1)