vsivsi / meteor-job-collection

A persistent and reactive job queue for Meteor, supporting distributed workers that can run anywhere.
https://atmospherejs.com/vsivsi/job-collection
Other
385 stars 68 forks source link

How to use worker within Meteor? #168

Closed bluepuma77 closed 8 years ago

bluepuma77 commented 8 years ago

I am trying to get a worker to run within Meteor. I used most of the sample code provided, but somehow the worker is never triggered. Can you provide a working example of a worker within Meteor?

///////////////////
// Server
if (Meteor.isServer) {

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

  Meteor.startup(function () {
    // Normal Meteor publish call, the server always
    // controls what each client can see
    Meteor.publish('allJobs', function () {
      return JobsQueue.find({});
    });

    // Start the myJobs queue running
    return JobsQueue.startJobServer();
  });

  Meteor.setTimeout(function(){

    // Create a worker
    var queue = Job.processJobs(
      'myJobQueue', // also tried JobsQueue here
      'sendEmail',   // Type of job to request
      {
        concurrency: 4,
        payload: 1,
        prefetch: 1
      },
      function (job, callback) {
        // Only called when there is a valid job
        console.log('[JobsQueue] Working job:', job.data);
        job.done();
        callback();
      }
    );    

    // Create a job:
    var job = new Job(JobsQueue, '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.'
      }
    );
    console.log('[JobsQueue] Job created', job.data);    

  }, 2000);
}

What's wrong with my code? The worker is never triggered.

vsivsi commented 8 years ago

All linked to from the README for this project: https://github.com/vsivsi/meteor-job-collection-playground https://github.com/vsivsi/meteor-job-collection-playground-worker https://github.com/vsivsi/meteor-file-job-sample-app

vsivsi commented 8 years ago

It looks like, in the code you included above, you should be calling:

JobsQueue.processJobs('sendEmail', options, workerFunc);

See: https://github.com/vsivsi/meteor-job-collection#jq--jcprocessjobstype-options-worker---anywhere

The Job.processJobs() form is for node.js code (outside of the Meteor environment). There's probably a way to make that work as well, but it's not the normal pattern when running within Meteor.

bluepuma77 commented 8 years ago

Thanks for the feedback, but it does not work for me within Meteor.

    var queue = JobsQueue.processJobs(
      'sendEmail',   // Type of job to request
      {
        concurrency: 4,
        payload: 1,
        prefetch: 1
      },
      function (job, callback) {
        // Only called when there is a valid job
        console.log('[JobsQueue] Working job:', job.data);
        job.done();
        callback();
      }
    );  

The code creates no output, so I assume the worker is not running.

vsivsi commented 8 years ago

I suggest you look at the sample apps I linked above.

I can't debug your app by just looking at code snippets. If you commit your entire sample app to a repo, I'll be happy to take a look.

bluepuma77 commented 8 years ago

The snippet is actually the full code to run the sample.

meteor create dummyapp
cd dummyapp
meteor add vsivsi:job-collection
cd server
vi main.js // replace with code from 1. post
meteor run

It will create a job in the queue, but the worker is not working. Note: the timeout in the code was added for the queue to be able to have time to initialize startJobServer(), not sure if this is required.

Chugot commented 8 years ago

You did not save the job in the snippet provided, that is necessary.

vsivsi commented 8 years ago

Right! Thanks @Chugot!

vsivsi commented 8 years ago
    // Create a job:
    var job = new Job(JobsQueue, '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.'
      }
    );

    var result = job.save();     //  <---  Need to save the job to the server/db!!!!
bluepuma77 commented 8 years ago

Wow, so plain and simple and still I didn't spot it. Thanks for your support!

vsivsi commented 8 years ago

Happens to all of us... Happy to help.