Can't create a new Job in vsivsi:job-collection

I’ve tried for the whole day but I cannot get an entry in the jobs collection I created. Here’s the code I’ve added specifically for the package, maybe I’m overlooking something (must be as no one else has such a problem).

This is jobs.js in the collections folder:

if (Meteor.isServer) {
Jobs = new JobCollection('jobs');

Meteor.startup(function () {
    console.log("JobServer started: " + Jobs.startJobServer());
    return;
});

};

The server report true so is running. The following code is in a separate file and is copied directly from the documentation of the vsivsi:job-collection package:

var job = new Job(Jobs, 'sendEmail', // type of job
                            // Job data that you define, including anything the job
                            // needs to complete. May contain links to files, etc...
                            {
                                address: 'bozo@clowns.com',
                                subject: 'Critical rainbow hair shortage',
                                message: 'LOL; JK, KThxBye.'
                            }
                        );

                        // Set some properties of the job and then submit it
job.priority('normal')
  .retry({ retries: 5,
    wait: 15*60*1000 })  // 15 minutes between attempts
  .delay(60*60*1000)     // Wait an hour before first try
  .save();               // Commit it to the server

// Now that it's saved, this job will appear as a document
// in the myJobs Collection, and will reactively update as
// its status changes, etc.

                        console.log ("Job inserted: " + job._id);

Console:
Job inserted: undefined

If I change the last line (console.log) to this:

console.log ("Job inserted: " + JSON.stringify(job));

then console is giving me the following error message:
TypeError: Converting circular structure to JSON

Which is strange as it’s for sure not a circular structure (see above). Like I said I copied the code directly from the documentation 1:1, so I don’t understand why it’s not working (only difference is that I named my job collection differently and left out the .allow and Meteor.publish part.

I’ve also checked the MongoDB directly via console and it doesn’t return any document:

db.jobs.find()

Any help is highly appreciated, I’m ready to throw the towel and give up on this package but it must be something simple that I overlooked. Thanks!

1 Like

Hi Andreas,

I also went through some pain to get this working. I made a couple of minor modifications and seem to have got your code to work… I think you might possibly have not subscribed to the Job Collection on the client side. Also I think you might need an allow/deny rule as you might hit a problem sooner or later… Finally I just renamed a few objects to avoid confusion later.

Anyway, if you create a bare bones Meteor app, add the vsivsi:job-collection package and then replace the default javascript file with the below you should see new jobs added to the job collection in the mongodb if you examine it from the terminal…

Here’s the full code for the single js file ->

if (Meteor.isClient) {
  // counter starts at 0
  Session.setDefault('counter', 0);

  myJobs = new JobCollection('myJobQueue');

  Template.hello.helpers({
    counter: function () {
      return Session.get('counter');
    }
  });

  Template.hello.events({
    'click button': function () {
      // increment the counter when button is clicked
      Session.set('counter', Session.get('counter') + 1);

      var newJob = new Job(myJobs, 'sendEmail', // type of job
                                  // Job data that you define, including anything the job
                                  // needs to complete. May contain links to files, etc...
                                  {
                                      address: 'bozo@clowns.com',
                                      subject: 'Critical rainbow hair shortage',
                                      message: 'LOL; JK, KThxBye.'
                                  }
                              );

                              // Set some properties of the job and then submit it

      newJob.priority('normal')
        .retry({ retries: 5,
          wait: 15*60*1000 })  // 15 minutes between attempts
        .delay(60*60*1000)     // Wait an hour before first try
        .save();               // Commit it to the server


      // Now that it's saved, this job will appear as a document
      // in the myJobs Collection, and will reactively update as
      // its status changes, etc.

      console.log ("Job inserted: " + newJob._id);

    }
  });
}

if (Meteor.isServer) {

    myJobs = new JobCollection('myJobQueue');

    myJobs.allow({
        // Grant full permission to any authenticated user
        admin: function (userId, method, params) {
          // return (userId ? true : false);
          return true;
        }
      });

    Meteor.startup(function () {
        console.log("JobServer started: " + myJobs.startJobServer());
        return;
    });
};

You can examine the new job entry in a mongo terminal with:

db.myJobQueue.jobs.find()

Hope that helps!

Cheers
Rick

p.s. I found I couldn’t retrieve the Job ID using ‘job._id’ after creating a new job like you are attempting either… never figured out why :frowning:

1 Like