22

I need to run code in Node.js every 24 hours. I came across a function called setTimeout. Below is my code snippet

var et = require('elementtree');
var XML = et.XML;
var ElementTree = et.ElementTree;
var element = et.Element;
var subElement = et.SubElement;
var data='<?xml version="1.0"?><entries><entry><TenantId>12345</TenantId><ServiceName>MaaS</ServiceName><ResourceID>enAAAA</ResourceID><UsageID>550e8400-e29b-41d4-a716-446655440000</UsageID><EventType>create</EventType><category term="monitoring.entity.create"/><DataCenter>global</DataCenter><Region>global</Region><StartTime>Sun Apr 29 2012 16:37:32 GMT-0700 (PDT)</StartTime><ResourceName>entity</ResourceName></entry><entry><TenantId>44445</TenantId><ServiceName>MaaS</ServiceName><ResourceID>enAAAA</ResourceID><UsageID>550e8400-e29b-41d4-a716-fffffffff000</UsageID><EventType>update</EventType><category term="monitoring.entity.update"/><DataCenter>global</DataCenter><Region>global</Region><StartTime>Sun Apr 29 2012 16:40:32 GMT-0700 (PDT)</StartTime><ResourceName>entity</ResourceName></entry></entries>'
etree = et.parse(data);
var t = process.hrtime();
// [ 1800216, 927643717 ]

setTimeout(function () {
  t = process.hrtime(t);
  // [ 1, 6962306 ]
  console.log(etree.findall('./entry/TenantId').length); // 2
  console.log('benchmark took %d seconds and %d nanoseconds', t[0], t[1]);
  //benchmark took 1 seconds and 6962306 nanoseconds
},1000);

I want to run the above code once per hour and parse the data. For my reference I had used one second as the timer value. Any idea how to proceed will be much helpful.

Flip
  • 4,701
  • 5
  • 32
  • 63
Amanda G
  • 1,663
  • 7
  • 31
  • 41
  • 4
    not like this... You want to run the entire script once an hour, not count on the node process to always be alive. If you're in linux look up cron jobs. – Mark Kahn Feb 14 '13 at 05:54
  • @cwolves I am in windows 7 any idea how to work on that?? – Amanda G Feb 14 '13 at 06:10
  • If you're on something other than a linux compatible OS then use another program that does the same thing. [Windows Task Scheduler seems to be a popular answer.](http://stackoverflow.com/questions/132971/what-is-the-windows-version-of-cron) – ekillaby Aug 07 '13 at 05:57
  • I also agree with @cwolves in that it may not be necessary to have your node.js program running all the time. It's just another process running in your system, which seems unnecessary. The idea of cron is that it's a manager for running potentially many scripts or jobs on a schedule while only using the system resources needed to run what's scheduled to run at that time. – ekillaby Aug 07 '13 at 06:04
  • TaskTimer - https://github.com/onury/tasktimer – Onur Yıldırım Aug 23 '16 at 15:58

1 Answers1

46

There are basically three ways to go

  1. setInterval()

The setTimeout(f, n) function waits n milliseconds and calls function f. The setInterval(f, n) function calls f every n milliseconds.

setInterval(function(){
  console.log('test');
}, 60 * 60 * 1000);      

This prints test every hour. You could just throw your code (except the require statements) into a setInterval(). However, that seems kind of ugly to me. I'd rather go with:

  1. Scheduled Tasks Most operating systems have a way of sheduling tasks. On Windows this is called "Scheduled Tasks" on Linux look for cron.

  2. Use a libary As I realized while answering, one could even see this as a duplicate of that question.

Community
  • 1
  • 1
Marc Fischer
  • 1,296
  • 12
  • 16
  • 2
    There's a typo in setInterval – Jonathan Nov 30 '13 at 23:27
  • 2
    Node.js is scheduled at the millisecond level and it just seems like terrible advice to suggest that you should use these tactics to schedule things so far into the future within JavaScript. There are just so many things that change the time in unexpected ways, put the computer to sleep/hibernate and such. Like others have suggested you should delegate long-term scheduling to Microsoft-based system programs in this case. – Michael Blankenship Oct 08 '15 at 18:09