I have to develop/use a scheduler in my NodeJS project to check if any subscriptions need to be renewed. Job needs to run once every day and check my database if 'enddate' of a any user subscription record falls on that day. If it is, run a piece of logic to renew it (i have my own renew logic which doesnt depend on any external services like payment providers/gateways).
Application will be in cloud which means all my app instances have to be stateless. Database (MySQL) is the only stateful part of the system and Renew logic has to run only once.
How can i do this ?
My database table row for a subscription looks like this:
Id: 1, User: John Doe, Plan: Monthly Premium, Start Date: 1-10-2015, End Date: 30-10-2014, status : "ACTIVE"
Problems:
1.) Trigger job running logic everyday - Easy to solve in nodejs with setInterval or a scheduler library
2.) Running a worker or a piece of code that checks the db and renews it - This needs to happen only once per user subscription. Many instances can potentially run the renew logic at the same time when daily scheduler triggers the process. Not sure how to solve this part of the problem.
One possible solution consists of a shell script, put in your crontab, that would run each day.
This script would do :
Using a sql statement or stored procedure, find records where subscription
end date is today
For those records, apply your logic. (send email or anything else)
Flag the record to indicate that a something has been done
for this user (and put in another field the process date).
you can create a worker and use node-cron to trigger subscription expire everyday.
i'm not sure what renew process does. you can use lock something until the renew process complete and release it. I was thinking i would use redlock
You can use Agenda, but it requires a MongoDb to maintain its state. If you are okay with adding one more DB, then you can give this a shot.
Related
I would like to delay deletion of data from the database. I am using MySQL, nest.js. I heard that CRON is what I need. I want to delete the entry in a week. Can you help me with this? CRON is what I need, or i need to use something another?
A cron job (or at in Windows) or a MySQL EVENT can be created to periodically check for something and take action. The resolution is only 1 minute.
If you need a very precise resolution, another technique would be required. For example, if you don't want to show a user something that is more than 1 week old to the second, then simply exclude that from the SELECT. That is add something like this to the WHERE: AND created_date >= NOW() - INTERVAL 7 DAY.
Doing the above gives you the freedom to schedule the actual DELETE for only, say, once a day -- rather than pounding on the database only to usually find nothing to do.
If you do choose to "pound on the database", be aware of the following problem. If one instance of the deleter script is running for a long time (for any of a number of reasons), it might not be finished before the next copy comes along. In some situations these scripts can stumple over each other to the extent of effectively "crashing" the server.
That leads to another solution -- a single script that runs forever. It has a simple loop:
Do the actions needed (deleting old rows)
Sleep 1 -- or 10 or 60 or whatever -- this is to be a "nice guy" and not "pound on the system".
The only tricky part is making sure that starts up after any server restart or crash of the script.
You can configure a cronjob to periodically delete it.
There are several ways to configure a cron job.
You can write a shell script that periodically deletes entities in the db using linux crontab, or you can configure an application that provides cronjobs such as jenkins or airflow.
AWS lambda also provides cronjob.
Using crontab provided by nestjs seems to be the simplest to solve the problem.
See this link
https://docs.nestjs.com/techniques/task-scheduling
I have been asked to send text message(sms) automatically when there is data changed in a table on mysql.. I have been searched many times and it comes with trigger..
I was wondering how mysql will send the text message(sms).. is it using gammu or somethingelse?
Thank you anyway for the response..
MySQL doesn't have any builtin functionality to send SMS messages.
As your search revealed, we can use a TRIGGER to perform actions whenever a row is inserted, updated or deleted from a table. One of the possible actions the trigger might perform is to INSERT a row to another table.
A separate component (not the MySQL database) could connect to the database and periodically poll that other table, and send SMS messages.
You have a few choices here, none of them wonderful.
One is to write a program in some application language (php or java, perhaps) to poll the dbms looking for changes in a table. Then it can send the requisite messages. You can do this by putting a time-updated timestamp in each row of the table. Then your polling will query for rows changed since the last time the polling ran. If you run this query once every six seconds, for example, you'll be no more than a tenth of a minute out of date with these SMS messages. That probably works for most wireless-network SMS.
Another is to add a user-defined function to MySQL and then invoke that function from a trigger. This one, for example, sends a message to a message queuing system like RabbitMQ or ActiveMQ. https://github.com/mysqludf/lib_mysqludf_stomp
You could then write a queue consumer app to send your SMS messages. This approach has low latency and will scale up nicely. But, it requires extending MySQL, and rigging up a nontrivial queuing system.
A third approach is to get the software that updates your table also to send the SMS messages.
I'm working on a Rails 3.2 application where users create, read and update multiple kinds of reports. We use MySQL and Redis.
I would like to notify users when one of their reports hasn't been updated in the previous X months by showing them notifications on their profile/dashboard page in the browser.
I would prefer to do this asynchronously.
At some point, I would also like to have live, in-app notifications so users can be notified when a report they're watching has been updated or someone likes their report. Conceptually, I was contemplating the best way of going about doing this...
A cron job that runs a SQL query and retrieves all reports that have update_at fields older than 3 months, and creates a notification record linked to that user and report. A new notification record will be created only if user didn't have an existing notification.
A background job, using something like Resque that checks the database periodically throughout the day. Notifications are stored in a queue. This seems like it would scale better into a more robust, in-app notifications feature.
Are these my only two options? Is there a better asynchronous way to listen to the database, and notify a user when one of their records hasn't been updated in 3 months? Is some implementation of websockets necessary here?
I don't believe you necessarily need to use CPU cycles with some always-running background job, but that depends on how powerful you want these notifications to be. If you want them to be real-time, and you plan on using a publisher-subscriber model in the future then I'd consider a redis + node server setup.
The complexity of websockets means more implementation time. Will that extra time spent really benefit you? If it's very important that your notifications be asynchronous and real-time, like exchanging messages in a chat app or getting pop-up notifications on Facebook, then this is the right choice.
However, if all you really want to do is let users know when their reports are past a certain age, when they request their profile page, a cron that runs every couple of hours will be much easier to implement. You could even use Google Analytics to get a feel for when users are putting the heaviest burden on your app, and schedule the job around that.
First, create a rake task that completes the behavior your described above--check for old reports and create notifications. Put that in your app's lib/tasks directory. And then write a cron job like so:
$ 0 6,12,18 * * * cd /path/to/app && bundle exec rake task_name
This task will run 3x a day, at 6am, 12pm and 6pm.
From there, if you want to create an activity feed for your users, you could also implement a synchronous pub-sub solution pretty simply. E.g., User A subscribes to User B's reports, User B publishes a new report, after_save callback to create a notification for User A. This will be slower, it won't be live, but if it's a small app and this feature isn't a core part, then this makes better use of your time than the other options.
I have a problem related to automatic task scheduling.
Currently i am able to find out when my customer has last credited his account, how am i able to find out whether he will pay anything in the next 3 days?
So if no payment has been made in the next three days for any customer, to automatically alert me preferably by a notification directly to my ipad.
I dont want myself to open the app for checks to be done only when i log in, because then if i jump on my application 6 days later, i could have had a customer that hasnt paid in 6 days when the app should have alerted me on the 3rd day so i could ring my customer up to deal with the matter.
I need to work in this matter due to the structure of my application and business.
I am able to monitor everything else but need some insight on how I can go about doing this. the current notification system inside the phone only fires based on time, and I cannot do interval checks where maybe i could run a background task, if that would work then i would have done it like that but thats not the case.
Pavan
If I understand your question correctly, you should compute the interval of the event that you want and post a wake-up timer that is that period of time from "now." If you need it through the notification center, then just handle it silently and clear it from the notifications.
Based on the discussion below:
You will need a little bit of server work. APNS looks complicated, but it really has very few moving parts -- especially if it is a private App. What system component is keeping an eye on Amazon? Do you have an App or web server? For example, if I were to poke a record into your system (purchased services) what workflow is triggered to notify Accounting to process an invoice and collections at a later date? Am I making any sense of your system architecture?
Perfect - you are done. You have all the system components you need and the rest is coding. The server app processes the accounts DB and finds new entries. If found, it publishes a record ID to the APNS server (Apple owns this server). You write code to register to receive the push-notification (subscriber). When you get a push, that will wake up your registered app with the record ID (and some other subscription stuff for bookkeeping -- but you are the only subscriber and only subscribing to one DB table -- so you can largely ignore. Now turn around and query based on that record. Done!
I am running a simple mysql db that contatins values for temperature sensors in a clients home. I have been looking into different ways to create email alerting if the temp is below or above a preset value.
There are ways that I can accomplish this via a simple php script to be ran off of a cron job from what i have gathered but the problem with them is it needs to be put 'on hold' for a certain amount of time that way once a client gets an alert they wont keep getting that alert every five minutes when the cron runs.
Also as a future question of this project, is there a way to have cron jobs created from values that users store in the sql db? That way the users can have customized alerting?
Sorry if it didn't come out clear, I will be right next to the computer all day to answer questions on this regard if need be.
To resolve your problem, create an alerts table that keeps track of when the last alert was. Depending on your platform, it may be possible for PHP to schedule cron jobs. You could save the user settings in a table and have PHP create/update/delete the cron jobs.