Update each user in nodejs app based on time interval - node.js

I have a node.js express app with MongoDB database and what I want is to be able to update the database based on time
Interval. For example
Increase a number field automatically every 7 days for each user based on an activation date set on each document.
Please how do I achieve this?

You can use cron jobs for that.
A cron job used for scheduling tasks to be executed sometime in the future at specific time interval.
Find out more here:
You will schedule a cron job that will run once in a day and you will add your code to be executed.

The What are Database Triggers? documentation indicates triggers, however it's not clear if that available on the free Community Edition, or only on their hosted Atlas offering. If you can't figure out a Mongo trigger solution, you would have to code a controller in Node.js based on .setInterval to fetch user creation date(s) then insert whatever update(s) you want on the user(s).
Types of Database Triggers MongoDB provides three types of triggers:
Database Triggers: Perform some type of action when a document is updated, added, or removed.
Scheduled Triggers: Scheduled actions that occur at a specific time or set interval. MongoDB Atlas leverages an intuitive approach to
scheduling triggers using the CRON expression language. These types of
triggers can be as simple as running a daily clean-up process of
temporary records, to generate a minute-by-minute report on
transaction anomalies.
Authentication Triggers (Realm only): Actions that happen when creating or deleting a user or on login into MongoDB. These types of
triggers are for performing user maintenance or user audit tracking
for Realm apps.

Related

Adding schedule job to nodejs microservicse best practice

We are using microservicse approach in our backend
We have a nodejs service which provide a REST endpoint that grap some data from mongodb and apply some business logic to it.
We would need to add a schedule job every 15 min to sync the mongodb data with some 3rd party data source.
The question here is - dose adding to this microservicse a schedule job that would do that, consider anti pattern?
I was thinking from the other point of having a service that just do the sync job will create some over engineering for simple thing, another repo, build cycle deployment etc hardware, complicated maintenance etc
Would love to hear more thoughts around it
You can use an AWS CloudWatch event rule to schedule CloudWatch to generate an event every 15 minutes. Make a Lambda function a target of the CloudWatch event so it executes every 15 minutes to sync your data. Be aware of the VPC/NAT issues if calling your 3rd party resources from Lambda if they are external to your VPC/account.
Ideally, if it is like an ETL job, you can offload it to a Lambda function (if you are using AWS) or a serverless function to do the same.
Also, look into MongoDB Stitch that can do something similar.

Schedule Nodemailer email based on info in database

I am creating an application that stores events and sends reminder emails to people who signed up 1 hour before the event(the time of each event is stored in the database). At first I was thinking about using CronJobs to schedule these emails, but now I am not sure if that will work. Is there any other node module that will allow me to implement the reminder email functionality.
If you have Redis available to backend it, you might look at something like bull.
From the readme:
Minimal CPU usage due to a polling-free design.
Robust design based on Redis.
Delayed jobs.
Schedule and repeat jobs according to a cron specification.
Rate limiter for jobs.
Retries.
Priority.
Concurrency.
Pause/resume—globally or locally.
Multiple job types per queue.
Threaded (sandboxed) processing functions.
Automatic recovery from process crashes.
You can give a try node-schedule. It is using cron-job underneath.
In a quality interval, you can check if there is an upcoming interval, and send the reminder to the appropriate persons.

Schedule task for Node.js web application

I have made a web real time application that connected to Node.js server through a websocket. In my website I can turn on/off an LED connected to Arduino Uno.
What I want to do is, I want my website have capability to turn on/off led at certain date and time dynamically. What I mean 'dynamically' is I can add new or remove current schedule task.
I have been trying using node-schedule, cron, but it's just a static schedule task. I can't change or add new task.
Use a db / file. You can store the dates in a json and then edit it as per your convenience. Use node-cron to create events of what you wanna do from the data. Create function that removes entry from json when you want to and it also remove it from the upcoming tasks by task.destroy() method of node-cron.
https://github.com/kdichev/Green-Systems/blob/development/PumpController.js
check what I have done with my pump. on line 19 I have an array of times that will run the pump according to the entries given.

SQS: Know remaining jobs

I'm creating an app that uses a JobQueue using Amazon SQS.
Every time a user logs in, I create a bunch of jobs for that specific user, and I want him to wait until all his jobs have been processed before taking the user to a specific screen.
My problem is that I don't know how to query the queue to see if there are still pending jobs for a specific user, or how is the correct way to implement such solution.
Everything regarding the queue (Job creation and processing is working as expected). But I am missing that final step.
Just for the record:
In my previous implementation I was using Redis + Kue and I had created a key with the user Id and the job count, every time a job was added that job count was incremented, and every time a job finished or failed I decremented that count. But now I want to move away from Redi + Kue and I am not sure how to implement this step.
Amazon SQS is not the ideal tool for the scenario you describe. A queueing system is normally used in a "Send and Forget" situation, where the sending system doesn't remain interested in later processing.
You could investigate Amazon Simple Workflow (SWF), which allows work to be monitored as it goes through several processes. Your existing code could mostly be re-used, just with the SWF framework added. Or even power it from Lambda, since you are already using node.js.

Which all libraries for NodeJS provide persistent scheduling and cron jobs

From what I have read, only Agenda,Node-crontab and schedule-drone provide this feature. It would be grateful if you provide a small description of the mechanism which these library use for persistent storage of jobs.
I need to send emails by reading the mail options from MongoDB and want my nodeJS application to somehow schedule and be in sych with these even if nodeJS is stopped temporarily.
For MySQL you can try with nodejs-persistable-scheduler
In other cases you need to build your own solution. For example, I created a collection/table to store the schedule state and rules. Then, if the service's crashes or restarted, I can get all the schedules form the database and restart them again from the app.listen event.

Resources