Cron job makes build take forever

Hey,

my site is moonxmind.app (or moonxmind.netlify.app).

I set up a cron job that sends a post request once a day to OneSignal to send a Web Push Message.

The thing is: Everything works fine but the status of my project stays “Building”. Now, I can even cancle the build process (after cron.js is running) and it still works.

cron.js:

import cron from 'node-cron';
import fetch from 'node-fetch';

cron.schedule('* 15 * * *', async () => {
    try {
        const response = await fetch('https://onesignal.com/api/v1/notifications', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Basic **********************************'
            },
            body: JSON.stringify({
                app_id: '**********************************',
                contents: { en: 'Sieh dir an, was der Mond gerade mit uns macht!' },
                headings: { en: 'Aktuelle Zeitqualität' },
                included_segments: ['All']
            })

        });

        const data = await response.json();

        console.log('Push notification sent:', data);
    } catch (error) {
        console.error('Failed to send push notification:', error);
    }
});

Why does the build take forever here?

2:38:51 PM: build-image version: a89f4ad50402ec92ae85e9b83fc50dcfb293df2e (focal)
2:38:51 PM: buildbot version: a89f4ad50402ec92ae85e9b83fc50dcfb293df2e
2:38:51 PM: Fetching cached dependencies
2:38:51 PM: Starting to download cache of 116.6MB
2:38:52 PM: Finished downloading cache in 1.021s
2:38:52 PM: Starting to extract cache
2:38:53 PM: Finished extracting cache in 811ms
2:38:53 PM: Finished fetching cache in 1.889s
2:38:53 PM: Starting to prepare the repo for build
2:38:53 PM: Preparing Git Reference refs/heads/main
2:38:54 PM: Parsing package.json dependencies
2:38:55 PM: Different functions path detected, going to use the one specified in the Netlify configuration file: 'functions' versus '' in the Netlify UI
2:38:55 PM: Different build command detected, going to use the one specified in the Netlify configuration file: 'npm run build && npm run cron' versus 'npm run build' in the Netlify UI
2:38:56 PM: Starting to install dependencies
2:38:56 PM: Using PHP version 8.0
2:38:56 PM: Python version set to 3.8
2:38:56 PM: Attempting Ruby version 2.7.2, read from environment
2:38:57 PM: Using Ruby version 2.7.2
2:38:57 PM: Started restoring cached go cache
2:38:57 PM: Finished restoring cached go cache
2:38:57 PM: Installing Go version 1.19.5 (requested 1.19.5)
2:39:02 PM: go version go1.19.5 linux/amd64
2:39:02 PM: go version go1.19.5 linux/amd64
2:39:02 PM: go version go1.19.5 linux/amd64
2:39:02 PM: go version go1.19.5 linux/amd64
2:39:02 PM: Started restoring cached Node.js version
2:39:03 PM: Finished restoring cached Node.js version
2:39:03 PM: v16.19.1 is already installed.
2:39:04 PM: Now using node v16.19.1 (npm v8.19.3)
2:39:04 PM: Enabling Node.js Corepack
2:39:04 PM: Started restoring cached build plugins
2:39:04 PM: Finished restoring cached build plugins
2:39:04 PM: Started restoring cached corepack dependencies
2:39:04 PM: Finished restoring cached corepack dependencies
2:39:04 PM: No npm workspaces detected
2:39:04 PM: Started restoring cached node modules
2:39:04 PM: Finished restoring cached node modules
2:39:04 PM: Installing npm packages using npm version 8.19.3
2:39:05 PM: added 3 packages, removed 5 packages, changed 1 package, and audited 271 packages in 482ms
2:39:05 PM: 54 packages are looking for funding
2:39:05 PM:   run `npm fund` for details
2:39:05 PM: found 0 vulnerabilities
2:39:05 PM: npm packages installed
2:39:05 PM: Install dependencies script success
2:39:05 PM: Starting build script
2:39:05 PM: Using stage install dependencies
2:39:05 PM: go version go1.19.5 linux/amd64
2:39:06 PM: Detected 1 framework(s)
2:39:06 PM: "svelte-kit" at version "1.7.1"
2:39:06 PM: Section completed: initializing
2:39:07 PM: ​
2:39:07 PM:   Netlify Build                                                 
2:39:07 PM: ────────────────────────────────────────────────────────────────
2:39:07 PM: ​
2:39:07 PM: ❯ Version
2:39:07 PM:   @netlify/build 29.7.1
2:39:07 PM: ​
2:39:07 PM: ❯ Flags
2:39:07 PM:   baseRelDir: true
2:39:07 PM:   buildId: 641b04e85f953200080bbac3
2:39:07 PM:   deployId: 641b04e85f953200080bbac5
2:39:07 PM: ​
2:39:07 PM: ❯ Current directory
2:39:07 PM:   /opt/build/repo
2:39:07 PM: ​
2:39:07 PM: ❯ Config file
2:39:07 PM:   /opt/build/repo/netlify.toml
2:39:07 PM: ​
2:39:07 PM: ❯ Context
2:39:07 PM:   production
2:39:07 PM: ​
2:39:07 PM:   1. build.command from netlify.toml                            
2:39:07 PM: ────────────────────────────────────────────────────────────────
2:39:07 PM: ​
2:39:07 PM: $ npm run build && npm run cron
2:39:08 PM: > astroapp@0.0.1 build
2:39:08 PM: > vite build
2:39:08 PM: vite v4.1.1 building SSR bundle for production...
2:39:08 PM: transforming...
2:39:10 PM: ✓ 81 modules transformed.
2:39:10 PM: 1:39:10 PM [vite-plugin-svelte] ssr compile done.
2:39:10 PM: package 	files	 time	   avg
2:39:10 PM: astroapp	   21	0.28s	13.2ms
2:39:10 PM: rendering chunks...
2:39:10 PM: vite v4.1.1 building for production...
2:39:10 PM: transforming...
2:39:12 PM: ✓ 86 modules transformed.
2:39:12 PM: 1:39:12 PM [vite-plugin-svelte] dom compile done.
2:39:12 PM: package 	files	 time	   avg
2:39:12 PM: astroapp	   21	0.60s	28.3ms
2:39:12 PM: rendering chunks...
2:39:12 PM: computing gzip size...
2:39:12 PM: .svelte-kit/output/client/_app/version.json                                                          0.03 kB
2:39:12 PM: .svelte-kit/output/client/vite-manifest.json                                                         9.41 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/assets/montserrat-v25-latin-500-ed121b1a.woff2             12.82 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/assets/montserrat-v25-latin-700-f31b8056.woff2             12.85 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/assets/_layout-08dda681.css                                14.84 kB │ gzip:  3.68 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/1-b7d39e2d.js                                        0.09 kB │ gzip:  0.10 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/2-9f50b4bc.js                                        0.10 kB │ gzip:  0.10 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/0-406ec623.js                                        0.10 kB │ gzip:  0.10 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/9-f30f660d.js                                        0.10 kB │ gzip:  0.11 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/8-b5375625.js                                        0.10 kB │ gzip:  0.11 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/6-67e35cf8.js                                        0.10 kB │ gzip:  0.11 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/5-ae6778a3.js                                        0.11 kB │ gzip:  0.11 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/4-0eb94cd5.js                                        0.11 kB │ gzip:  0.11 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/3-af98d76b.js                                        0.11 kB │ gzip:  0.11 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/10-5fd61b29.js                                       0.11 kB │ gzip:  0.11 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/7-293ed398.js                                        0.11 kB │ gzip:  0.12 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/navigation-7624ab62.js                               0.20 kB │ gzip:  0.16 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/index-9cd64e4b.js                                    0.26 kB │ gzip:  0.20 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/stores-65413a06.js                                   0.27 kB │ gzip:  0.19 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/components/error.svelte-1f63ac5f.js                         0.80 kB │ gzip:  0.50 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/singletons-54c5f893.js                               2.19 kB │ gzip:  1.16 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/components/pages/info/_title_/_page.svelte-823857e1.js      4.65 kB │ gzip:  2.12 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/components/pages/installation/_page.svelte-a428785e.js      5.13 kB │ gzip:  2.11 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/components/pages/lexikon/_word_/_page.svelte-d558c710.js    5.54 kB │ gzip:  2.44 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/components/pages/lexikon/_page.svelte-f5e08095.js           5.93 kB │ gzip:  2.51 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/components/pages/kalender/_date_/_page.svelte-3b480e99.js   6.29 kB │ gzip:  2.58 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/ActionBar-c94fc603.js                                8.03 kB │ gzip:  2.98 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/components/pages/kontakt/_page.svelte-b34dcd4e.js           8.82 kB │ gzip:  3.51 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/components/pages/kalender/_page.svelte-9fa5fba4.js         10.25 kB │ gzip:  3.76 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/rich-text-html-renderer.es5-4e0bb50a.js             10.49 kB │ gzip:  3.31 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/index-3294c1ff.js                                   13.69 kB │ gzip:  5.69 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/components/pages/_layout.svelte-d7f82b20.js                17.98 kB │ gzip:  7.08 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/components/pages/einstellungen/_page.svelte-a46db74e.js    20.16 kB │ gzip:  6.25 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/components/pages/_page.svelte-17e7f61c.js                  20.86 kB │ gzip:  5.87 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/start-a642b5ee.js                                          30.90 kB │ gzip: 11.44 kB
2:39:12 PM: .svelte-kit/output/client/_app/immutable/chunks/index-23fb3f72.js                                   77.26 kB │ gzip: 27.79 kB
2:39:12 PM: vite v4.1.1 building for production...
2:39:12 PM: transforming...
2:39:12 PM: ✓ 2 modules transformed.
2:39:12 PM: rendering chunks...
2:39:12 PM: computing gzip size...
2:39:12 PM: .svelte-kit/output/client/service-worker.js  2.88 kB │ gzip: 0.99 kB
2:39:12 PM: .svelte-kit/output/server/vite-manifest.json                                              4.67 kB
2:39:12 PM: .svelte-kit/output/server/_app/immutable/assets/montserrat-v25-latin-500-ed121b1a.woff2  12.82 kB
2:39:12 PM: .svelte-kit/output/server/_app/immutable/assets/montserrat-v25-latin-700-f31b8056.woff2  12.85 kB
2:39:12 PM: .svelte-kit/output/server/_app/immutable/assets/_layout-f80339c3.css                     22.32 kB
2:39:12 PM: .svelte-kit/output/server/internal.js                                                     0.19 kB
2:39:12 PM: .svelte-kit/output/server/chunks/stores.js                                                0.47 kB
2:39:12 PM: .svelte-kit/output/server/entries/fallbacks/error.svelte.js                               0.47 kB
2:39:12 PM: .svelte-kit/output/server/chunks/index.js                                                 1.32 kB
2:39:12 PM: .svelte-kit/output/server/entries/pages/installation/_page.svelte.js                      2.18 kB
2:39:12 PM: .svelte-kit/output/server/chunks/ActionBar.js                                             2.35 kB
2:39:12 PM: .svelte-kit/output/server/entries/pages/_layout.server.js                                 2.94 kB
2:39:12 PM: .svelte-kit/output/server/entries/pages/lexikon/_page.svelte.js                           3.01 kB
2:39:12 PM: .svelte-kit/output/server/entries/pages/lexikon/_word_/_page.svelte.js                    3.86 kB
2:39:12 PM: .svelte-kit/output/server/chunks/index2.js                                                3.88 kB
2:39:12 PM: .svelte-kit/output/server/entries/pages/info/_title_/_page.svelte.js                      4.04 kB
2:39:12 PM: .svelte-kit/output/server/chunks/index3.js                                                4.12 kB
2:39:12 PM: .svelte-kit/output/server/entries/pages/kontakt/_page.svelte.js                           4.27 kB
2:39:12 PM: .svelte-kit/output/server/entries/pages/kalender/_date_/_page.svelte.js                   5.47 kB
2:39:12 PM: .svelte-kit/output/server/chunks/internal.js                                              5.79 kB
2:39:12 PM: .svelte-kit/output/server/entries/pages/einstellungen/_page.svelte.js                     6.08 kB
2:39:12 PM: .svelte-kit/output/server/entries/pages/kalender/_page.svelte.js                          7.12 kB
2:39:12 PM: .svelte-kit/output/server/entries/pages/_layout.svelte.js                                10.08 kB
2:39:12 PM: .svelte-kit/output/server/entries/pages/_page.svelte.js                                  17.78 kB
2:39:12 PM: .svelte-kit/output/server/index.js                                                       78.32 kB
2:39:12 PM: Run npm run preview to preview your production build locally.
2:39:12 PM: 
2:39:12 PM: > Using @sveltejs/adapter-netlify
2:39:12 PM:   ✔ done
2:39:12 PM: > astroapp@0.0.1 cron
2:39:12 PM: > node cron.js

Cheers,

Nico

I have 0 familiarity with Cron.

I recommend you give this guide a read:

This issue is the build command and method used for the cron job.

npm run cron is going to keep running once the build is finished.

If you want a Cron-like function, then use a Scheduled Function