MongoDB updating variable based on date - javascript

I am trying to update a database field on a MongoDB collection using Meteor at an exact time -- say 12:00 AM every Monday.
How should I go about doing this?
I am trying matching the day of the week and the exact time number but I can't seem to make it work for the server to do this more than once without using a loop that will make the website freeze.

If you can install a cron-job npm module, like: npm install cron, than you could set up a cron job with a Monday-only pattert, to call your db update code.
var CronJob = require('cron').CronJob;
var job = new CronJob('00 00 12 * * 1', function() {
/*
* Runs every weekday Monday at 12:00:00
*/
}, function () {
/* DB update code */
}
);
Docs available at https://github.com/ncb000gt/node-cron, if you need to fine tune your cron pattern or other options.

Related

How to automatically insert data in mongoose js at certain time

As I am building website using node js and mongoose i need one hint of help that how i can automatically insert data in mongoose node js at certain time. Example at end of every month my data current data should automatically be stored in mongoose data base.
You can use node-cron to perform any CRON operation in node js.
https://www.npmjs.com/package/node-cron
Example:
var cron = require('node-cron');
/* CRON JOB Scheduled to daily at 12AM */
cron.schedule('0 0 12 1/1 * ? *', () => {
// Add your API/Logic here to insert data in mongoose using node.js
});
CRON generator

Discord.js do something in specific time

I would like to make bot that in specific time every day will for example message but i want that the bot can still response to commands. Is there any node.js library or some function.
You can use the cron package to do this.
You first have to import CronJob:
const { CronJob } = require('cron');
If you want to scedule a message at 17:18 everyday, you define a CronJob like this:
const job = new CronJob('0 18 17 * * *', () => {
//send a message
});
and start it:
job.start();
the format for the string is 'sec min hour day of month month day of week'. You use * if all values of that parameter is accepted. Your callback function will be called everytime all 6 of the paremeters match the current time. The sec parameter must not be *, or else the message would get sent every second in that minute.

How to run cron jobs according to user's timezone in nodeJs?

I am using cron jobs to reset a particular field back to zero let's call it a streak I am trying to reset current streak of a user. so for that I am running a cron job at 11:59 to reset the data if user doesn't perform a particular task,But it's running according to UTC time. I want it to run according to users timezone who is using the application. Is it possible to run it according to different timezone or anyone could suggest any other way to do so.
You should set the time with a lib like http://momentjs.com/timezone/docs/.
var a = moment.tz("2013-11-18 11:55", "America/Toronto");
or
You should try CronJob npm package
new CronJob(
targetTime,
async function () {
await donsomething
},
null,
true,
time_zone
);

Node-Schedule Triggering Issue

I am creating a function on my server which is supposed to trigger daily, but not only does it not trigger as expected it triggers every time the server is reset (which it did not do yesterday). The data only needs updated once a day and rapidapi is pay per api call after 100 calls a day, so I'm really trying to get this scheduled job working.
I've only tried node-schedule on my server, which has a dependency of CRON (which is why scheduleJob is using the CRON format). Originally the function triggered as fast as my computer could refresh the server, so I know the scheduleJob is stopping the API call from being constant.
schedule.scheduleJob('* * */23 * *', function () {
console.log('Daily API call initiated.');
unirest.get("https://unogs-unogs-v1.p.rapidapi.com/aaapi.cgi?q=get:exp:US&t=ns&st=adv&p=1")
.header("X-RapidAPI-Host", "unogs-unogs-v1.p.rapidapi.com")
.header("X-RapidAPI-Key", `${keys.RICHARD_UNOGS_KEY}`)
.end(function (result) {
console.log(result.status, result.headers);
//console.log(result.body) to see all data
let data = JSON.stringify(result.body)
fs.writeFile('./movieData.json', data)
});
})
Is there a better way to write this function or achieve the desired results? I only need it triggering once per day, but my computer isn't on 24/7. Since I'm still developing the app, I don't think hosting the server on Heroku or another platform is time-efficient, but I'm more than willing to try it if it'll solve my issue.
The main goal is to tell my server to run it once a day, but to check if it was run in the last 24 hours. If it hasn't been run, run it.
ANSWERED BY jakemingolla: My CRON expression was incorrectly formatted for my intended use. * * */23 * * has the function triggering on every 23rd day (today is the 24th, +1 for index value) with no limitation or other value. That's why my function was calling constantly today. I changed my CRON expression to 15 9 * * * to trigger my API call every morning at 0915 hours.
I'm not sure your CRON format is correct based on https://crontab.guru. Based on * * */23 * *, that would correspond to "“At every minute on every 23rd day-of-month", which does not sound like what you want.
For example, the CRON string 0 0 * * * * is every day at midnight. I would update that CRON string to see if it helps alleviate the problem.

How to delete mysql data from table at midnight?

I have a mysql table and I want it to be "emptied" every night at midnight. I have searched for an answer on the web and came across nothing that seemed to help me. I had this idea of using javascript to get the current time and then run an if statement and see if it is equal to midnight and if it was to execute a php script that deleted the information.
Javascript:
var myVar=setInterval(function(){myTimer()},1000);
function myTimer()
{
var d=new Date();
var t=d.toLocaleTimeString();
if(t == 12:00:00 AM){
$.ajax({
URL: 'delete.php';
});
};
};
delete.php:
<?php
require 'connect.php';
mysql_query("DELETE * FROM messages;");
?>
I have tested this by setting the time in the if statement to a time a few minutes ahead of my actual time and it does not work.
Implementing your own event scheduler, especially as a web page using JavaScript is a bad idea.
Use for that either
a cron job to run DELETE statement through the mysql command line interface
/path/to/mysql -u<user> -p"<password>" <db_name> -e "delete from messages"
or a MySQL event, e.g.
CREATE EVENT delete_messages_at_midnight 
ON SCHEDULE EVERY 1 DAY STARTS CURDATE() + INTERVAL 1 DAY
DO DELETE FROM messages;
If you go with MySQL event approach:
use SHOW PROCESSLIST to check if the event scheduler is enabled. If it's ON you should see a process "Daemon" by user "event_scheduler".
use SET GLOBAL event_scheduler = ON;to enable the scheduler if it's currently not enabled.
More on configuring event scheduler read here

Categories

Resources