简体   繁体   中英

Start job with meteor job collection

I am using meteor-job-collection ( https://github.com/vsivsi/meteor-job-collection ); however, I cannot create a job.

I have a method defined as

Meteor.methods( {
  insertItems: function ( dataArray ) {
    check( dataArray, [ Object ] );

    dataArray.forEach( function ( element ) {
      [...]
    } );
  }
} );

but the method is very memory demanding, so I want to wrap it inside a job. How do I start this job?

I have tried

var job = new Job( Jobs, 'insertItems', data ).priority( 'normal' ).retry(
  {
    retries: 5,
    wait: 15 * 60 * 1000
  }
).delay( 60 * 60 * 1000 ).save();

but I get the error

Error invoking Method 'jobQueue_jobSave': Internal server error [500]

My Job Collection is defined as

Jobs = JobCollection( 'jobQueue' );

if ( Meteor.isServer ) {
  Jobs.allow( {
    admin: function ( userId, method, params ) {
      return true;
    },
  } );
}

Multiple things are missing from your code.

1 - Make sure you start the job server before submitting any jobs. Call startJobServer() on the jobCollection on the server.

Jobs = JobCollection( 'jobQueue' );

if ( Meteor.isServer ) {
  Jobs.allow( {
    admin: function ( userId, method, params ) {
      return true;
    },
  } );
  Jobs.startJobServer();
}

2 - You need to implement the processing of the job. A job is nothing more than a label with data attached scheduled to run at a certain point in time. The handler implements the job logic. In your case you would need something like this:

var workers = Job.processJobs('jobQueue', 'insertItems',
  function (job, cb) {
    insertData = job.data;
    // do anything with the job data here.
    // when done, call job.done() or job.fail()

    job.done(); // when done successfully
    job.fail("with reason or error"); //when failing

    // Be sure to invoke the callback
    // when work on this job has finished
    cb();
  }
);    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM