Possible to perform scheduled cron jobs in a deployed node REST API server?

Hello, I have a REST API server deployed with fly. I want to create a new record in my Mongo database at a specific time every day, which I assume isn’t possible with something like node-cron since the api server wouldn’t be running 24/7. So I followed the steps in this guide Crontab with Supercronic · Fly Docs.

Unfortunately I’m not very familiar with Docker files or what Fly does under the hood, so I’ve tried to modify the files so that in theory I could run a specific javascript file everyday:


# Latest releases available at https://github.com/aptible/supercronic/releases
ENV SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.2.1/supercronic-linux-amd64 \
    SUPERCRONIC=supercronic-linux-amd64 \

 && echo "${SUPERCRONIC_SHA1SUM}  ${SUPERCRONIC}" | sha1sum -c - \
 && chmod +x "$SUPERCRONIC" \
 && mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
 && ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic

# You might need to change this depending on where your crontab is located
COPY crontab crontab

FROM node:17-alpine as build-image
WORKDIR /usr/src/app
COPY package*.json ./
COPY tsconfig.json ./
COPY ./src ./src
RUN npm ci
RUN npx tsc

FROM node:17-alpine
WORKDIR /usr/src/app
COPY package*.json ./
COPY --from=build-image ./usr/src/app/dist ./dist
RUN npm ci --production
COPY . .
CMD [ "node", "dist/app.js" ]


app = ""
kill_signal = "SIGINT"
kill_timeout = 5
primary_region = "mia"
processes = []


  PORT = "8080"

  auto_rollback = true

  processes = ["app", "web"]
  http_checks = []
  internal_port = 8080
    processes = ["app", "web"]
  protocol = "tcp"
  script_checks = []
    hard_limit = 25
    soft_limit = 20
    type = "connections"

    force_https = true
    handlers = ["http"]
    port = 80

    handlers = ["tls", "http"]
    port = 443

    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

  # The command below is used to launch a Rails server; be sure to
  # replace with the command you're using to launch your server.
  web = "node dist/src/scripts/dailyReset.js"
  cron = "supercronic /app/crontab"

This causes the deployment to fail at this stage:
Error: failed to fetch an image or build from source: error building: failed to solve with frontend dockerfile.v0: failed to create LLB definition: no build stage in current context

And I first wanted to confirm whether what I’m trying to do was even possible, or if I’d have to create a different app specifically to perform this one scheduled job. Thank you.

Your Dockerfile needs to start with a FROM statement, but even if you got it working you would need the machine to be running for the cron job tasks to fire.

scheduled machines may be of interest.

Thank you for the reply. I would love to use scheduled machines but unfortunately I need the task to execute at a specific time everyday which seems difficult to do with how scheduled machines is set up. I’ll have to look into deploying the scheduler separately.

As a follow-up question, if I were to look into using scheduled machines, would there be a way for me to get the precise date/time when it runs? Or would I have to just grab the time when the machine starts and calculate from there?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.