如何使用解析服务器上的kue安排作业? [英] How to schedule a job with kue on parse-server?

查看:65
本文介绍了如何使用解析服务器上的kue安排作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与Heroku一起部署的解析服务器(来自我的GitHub存储库),并由mongoLab托管.我正在尝试在我的应用程序中发送预定的推送通知,而kue似乎是最可行的选择.但是,由于我不熟悉它,因此我不确定该采用哪种方法.我相信我已经通过GitHub在服务器上正确安装了kue.现在,我希望将此代码安排在将来的某个日期执行:

I have a parse-server deployed with Heroku (from my GitHub repo) and hosted by mongoLab. I am attempting to send scheduled push notifications within my app and kue seems to be the most viable option. However, as I am very unfamiliar with it, I am not sure the way to approach it. I believe I have correctly installed kue on my server (through GitHub). Now, I would like to schedule this code to be executed at a date in the future:

    Parse.Cloud.define("sendPush", function(request, response) {


      var pushQuery = new Parse.Query(Parse.Installation);
      pushQuery.equalTo('username', request.params.targetUsername);

      Parse.Push.send({
        where: pushQuery, // Set our Installation query                                                                                                                                                              
        data: {
          alert: 'Hello!',
          badge: 'Increment',
          sound: 'PopDing.caf'
        },   
      }, { success: function() {
  console.log("#### PUSH OK");
      }, error: function(error) {
  console.log("#### PUSH ERROR" + error.message);
      }, useMasterKey: true});

    });

如果我正以正确的方式处理问题,那么我需要代码来简单地安排要在将来的指定时间执行的作业(上面的代码).我不打算将代码安排为定期或间隔运行,仅在将来的指定时间运行一次.答案或任何建议将不胜感激,谢谢!

If I am approaching this the correct way, then I need the code to simply schedule a job (the code above) to be executed at a specified time in the future. I do not the code to be scheduled to run regularly or at intervals, just once at the specified time in the future. An answer or any advice would be greatly appreciated, thanks!

推荐答案

下面是一个示例,您可以使用 kue 在将来的特定时间仅一次:(12小时后)

Here is an example how you can accomplish scheduling this task with kue only once at a specific time in the future: (after 12 hours)

var kue = require( 'kue' );

// create our job queue
var jobs = kue.createQueue();

// one minute
var minute = 60000;

var job= jobs.create( 'parseCloud', {
    alert: 'Hello!',
          badge: 'Increment',
          sound: 'PopDing.caf'
} ).delay( minute * 60 * 12)
  .priority( 'high' )
  .save();



job.on( 'complete', function () {
  console.log( 'renewal job completed' );
} );



jobs.process( 'parseCloud', function ( job, done ) {

      var pushQuery = new Parse.Query(Parse.Installation);
      pushQuery.equalTo('username', request.params.targetUsername);

      Parse.Push.send({
        where: pushQuery, // Set our Installation query                                                                                                                                                              
        data: {
          alert: job.data.alert,
          badge: job.data.badge,
          sound: job.data.sound
        },   
      }, { success: function() {

        console.log("#### PUSH OK");
        done();
      }, error: function(error) {
        console.log("#### PUSH ERROR" + error.message);
         done();
      }, useMasterKey: true});

} );

// start the UI
kue.app.listen( 3000 );
console.log( 'UI started on port 3000' );

这篇关于如何使用解析服务器上的kue安排作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆