nathanbreitsch / storytime

Make stories with your friends!
0 stars 0 forks source link

chron-job on server #9

Open nathanbreitsch opened 9 years ago

nathanbreitsch commented 9 years ago

Every two minutes the server flushes the sentence suggestions and adds the best one to the story. Right now, the server checks every second whether it is time to make these changes. That's n00bcode and must be fixed.

The code is located in server/server.js and looks like this:

if(minutes == 1 && seconds == 0){//yes, 1 for some reason var topSentence = Sentences.findOne({},{sort:{score:-1}}); console.log(topSentence.sentence); Sentences.remove({}); Story.insert({"sentence":topSentence.sentence}); return "0"; }

This code should be removed from the getServerTime method and replaced with a cron job. After a thorough 6 seconds of googling, I found the following node package:
https://www.npmjs.org/package/node-schedule

node-schedule will do the trick, but feel free to use another tool if it floats your boat.

The node-schedule page explains everything, but I will recap some:

To install node-schedule, just go to your command line and sudo npm install node-schedule. To use node-schedule in your code, just put var schedule = require('node-schedule'); at the top. You should write your cronjob at the bottom of server.js. It will look something like:

var rule = new schedule.RecurrenceRule();

(do something with rule to make the job execute every 2 minutes)

var j = schedule.scheduleJob(rule, function(){ console.log('The answer to life, the universe, and everything!'); });

I think you could also just replace rule with the string '/2 * * * '. Research and play around.

You will know you're done when:

  1. The noobcode is gone
  2. The sentence page still clears and top sentence is still added to top every two minutes.

Let me know if you have questions.

brianbreitsch commented 9 years ago

http://stackoverflow.com/questions/24114666/meteor-timesync-package-not-syncing-when-changing-client-time

https://github.com/mizzao/meteor-timesync

brianbreitsch commented 9 years ago

https://www.youtube.com/watch?v=pNKNYLv2BpQ