I have a NodeJS
app consisting of a REST API and an overnight maintenance (cron
) job. Currently running on Debian Linux.
What is the best practice do Dockerize it?
What is the best way to achieve this?
Use a node cron package. I added some code example from a real project. Every hour I scrape a website. You can set time and place your recurring task inside cron.schedule
. You can use it a separate project or combine with your api code.
import { Scraper } from './controller/scraper.js';
import cron from 'node-cron'
cron.schedule('0 0 */1 * * *', () => {
console.log('running a task every hour');
const scraper = new Scraper()
const data = scraper.getData()
});
Docker official website doesn't advice running process managers with containers. But you can use if you need. I use pm2 as a process manager. It can be used for running an app for some intervals and it can restart your app if it crashes.
Links that will help you:
My use case:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With