Calling MongoDB on /server as recurring job


#1
Meteor.setInterval(Meteor.bindEnvironment(ThisTry()), 5000);

function ThisTry() {
    let start = new moment();
    start.subtract(30, 'minute'); // pick up stragglers, if any
    let end = new moment();
    end.add(3, 'minute');
    let start1 = start.toDate();
    let end1 = end.toDate();

    let sendQue = QueueItems.find({
        sent: false,
        "library_item.shareType": 'tweet',
        dateTime: {$gte: start1, $lt: end1}});

    console.log("Count to send: " + sendQue.count());
}

Runs once, correct output then:

I20161002-17:02:49.238(-4)? Exception in callback of async function: TypeError: Cannot read property ‘apply’ of undefined
I20161002-17:02:49.238(-4)? at runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1)
I20161002-17:02:49.238(-4)? at packages/meteor/dynamics_nodejs.js:123:1
I20161002-17:02:49.238(-4)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20161002-17:02:49.238(-4)? at packages/meteor/timers.js:6:1

  1. Tried a cron job…
const CronJob = Npm.require('cron').CronJob;
new CronJob('0 */1 * * * *', function() { // change 1 to 5
    console.log('Cron Run...');
    let sendQue = QueueItems.find({});
    console.log(sendQue.count());
}, null, true, 'America/New_York');

Get no results and this error:

I20161002-17:10:00.981(-4)? Cron Run…
W20161002-17:10:01.085(-4)? (STDERR) C:\Users\Greg\WebstormProjects\Saturn_V2.meteor\local\build\programs\server\packages\meteor.js:1060
W20161002-17:10:01.085(-4)? (STDERR) throw new Error("Meteor code must always run within a Fiber. " +
W20161002-17:10:01.085(-4)? (STDERR) ^
W20161002-17:10:01.086(-4)? (STDERR)
W20161002-17:10:01.086(-4)? (STDERR) Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
W20161002-17:10:01.088(-4)? (STDERR) at Object.Meteor.nodeCodeMustBeInFiber (packages/meteor/dynamics_nodejs.js:9:1)
W20161002-17:10:01.088(-4)? (STDERR) at [object Object].
.extend.get (packages/meteor/dynamics_nodejs.js:21:1)
W20161002-17:10:01.088(-4)? (STDERR) at Object.collection.(anonymous function) [as find] (packages/matb33_collection-hooks/packages/matb33_collection-hooks.js:107:1)
W20161002-17:10:01.088(-4)? (STDERR) at [object Object].find (packages/mongo/collection.js:315:29)
W20161002-17:10:01.088(-4)? (STDERR) at CronJob. (server/autosharing/every5.ts:13:30)
W20161002-17:10:01.088(-4)? (STDERR) at CronJob.fireOnTick (C:\Users\Greg\WebstormProjects\Saturn_V2\node_modules\cron\lib\cron.js:392:22)
W20161002-17:10:01.088(-4)? (STDERR) at [object Object].callbackWrapper [as _onTimeout] (C:\Users\Greg\WebstormProjects\Saturn_V2\node_modules\cron\lib\cron.js:435:9)
W20161002-17:10:01.088(-4)? (STDERR) at Timer.listOnTimeout (timers.js:92:15)

I have not written my async function yet…
At a loss of how to even access the database it seems on the server (in the /server path )at intervals.


#2

I’ve used synced-cron in pretty much every app i’ve made. haven’t looked through your code or errors, but i recommend checking it out.


#3

Not sure how this helps.

Can’t call the database…


#4

Why do you think it can’t call the database?

This is a snippet of my apps where I am using it to clear old database entries:


#5

Just in case you haven’t seen it job-collection, works great and has good documentation.


#6

I hadn’t seen that one before, good to know. Synced-Cron isn’t being maintained anymore - but I still love how simple and easy it is to use.


#7

Seems way way over kill for wanting a simple 5 minute cron job.


#8

You could just wrap your cron call in a Fiber, like:

import { CronJob } from 'cron';
import Fiber from 'fibers'
...
new CronJob('0 */1 * * * *', () => {
  Fiber(() => {
    console.log('Cron Run...');
    const sendQue = QueueItems.find({});
    console.log(sendQue.count());
  }).run();
}, null, true, 'America/New_York');

#9

Or you do it the sloppy way:

Create a route in your router which does nothing but call the Meteor method which does the work. Then add an actual Cron (as in shell script) job which uses curl to call this route. And if the route isn’t called by 127.0.0.1, then simply do nothing.