Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kue-scheduler every couldn't run on redis #46

Open
WUJJU opened this issue Sep 2, 2016 · 19 comments
Open

kue-scheduler every couldn't run on redis #46

WUJJU opened this issue Sep 2, 2016 · 19 comments

Comments

@WUJJU
Copy link

WUJJU commented Sep 2, 2016

Hi, is anyone having the same problem? I am trying to migrate my parse host backend to heroku.
I am changing Parse.Cloud.Job to kue-scheduler. It seems the every() function doesn't work in heroku redis, while it work on local redis-server.

var kue=require('kue-scheduler');
var queue=kue.createQueue({redis:'redis://h:p130l529a4jg21xxxxxxxxxxxxx@ec2-54-163-236-235.compute-1.am',
    skipConfig: true
});


var job=queue.create('new_job').attempts(3).priority('normal');

queue.every('2 seconds',job);

queue.process('new_job',function(job,done){
   console.log('Job',job.id,'is done');
   done();
})
@gmcnaught
Copy link
Collaborator

Kue depends on keyspace notifications, does heroku redis support enabling
keyspace expiration notifications?

On Friday, September 2, 2016, Hao Wu notifications@github.com wrote:

Hi, is anyone having the same problem? I am trying to migrate my parse
host backend to heroku. I am changing Parse.Cloud.Job to kue-scheduler. It
seems the every() function doesn't work in heroku redis, while it work on
local redis-server.

var kue=require('kue-scheduler');
var queue=kue.createQueue({redis:'redis://h:p130l529a4jg21xxxxxxxxxxxxx@ec2-54-163-236-235.compute-1.am javascript:_e(%7B%7D,'cvml','h:p130l529a4jg21xxxxxxxxxxxxx@ec2-54-163-236-235.compute-1.am');',
skipConfig: true
});

var job=queue.create('new_job').attempts(3).priority('normal');

queue.every('2 seconds',job);

queue.process('new_job',function(job,done){
console.log('Job',job.id,'is done');
done();
})


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#46, or mute the thread
https://github.com/notifications/unsubscribe-auth/ABTsfzJQTCPQ24KU9n-yJRnlVVC1Gxnqks5qmHL0gaJpZM4Jz_7u
.

@WUJJU
Copy link
Author

WUJJU commented Sep 2, 2016

Based on my search, Heroku redis close the keyspace notification by default.
I have log in my Heroku redis instance and "set notify-keyspace-events Ex", console shows "OK".
But every() is still not work. Any idea?

@lykmapipo
Copy link
Owner

Hello. Have you manage to fix it on Heroku.

On 2 Sep 2016 23:21, "Hao Wu" notifications@github.com wrote:

Based on my search, Heroku redis close the keyspace notification by
default. Do you have any idea to change the config. I try "redis-cli config
set notify-keyspace-events Ex". But fail.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#46 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABiUaUDHCB_ZHXzoyAp5_qN75GxVzhOCks5qmIU_gaJpZM4Jz_7u
.

@lykmapipo
Copy link
Owner

@WUJJU

Please ensure you have Redis 2.8.0+.

@lykmapipo
Copy link
Owner

@WUJJU

See requirements for kue-scheduler to work.
By default kue-scheduler will try to enable kespace notification. If it fails please use

redis-cli config set notify-keyspace-events Ex

@WUJJU
Copy link
Author

WUJJU commented Sep 5, 2016

@lykmapipo all requirements meet and notify-keyspace-events set to Ex. But still fail.

@lykmapipo
Copy link
Owner

Hello.

I think we should add logger to kue-scheduler to log its inner working.

Try to listen for "schedule error" event in your kue instance and see it it
fired.

On 5 Sep 2016 22:29, "Hao Wu" notifications@github.com wrote:

@lykmapipo https://github.com/lykmapipo all requirements meet and
notify-keyspace-events set to Ex. But still fail.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#46 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABiUacSbHOQWAi5m3Y0_HiJWxQS_ESbQks5qnG2hgaJpZM4Jz_7u
.

@josephktcheung
Copy link

Hi,

Just want to chime in with a reply from Heroku support. I too faced the same issue that Heroku Redis cannot run every. I tried connecting to Heroku Redis using redis-cli and do config set notify-keyspace-events Ex but it said unknown command CONFIG instead of OK. Then I contacted Heroku support by creating a ticket on this issue and here's Heroku support's reply:

I'm afraid that Heroku Redis doesn't currently support keyspace notifications. However, I can loop in Redis To Go to see if their instances do support this. They will reach out as soon as they can.

@WUJJU I'm afraid that you did config setting wrongly. set and config set are two different commands. set sets key to hold the string value. config set is used in order to reconfigure the server at run time without the need to restart Redis. You can change both trivial parameters or switch from one to another persistence option using this command (Quoted from Redis documentation). You can find the documenation of set and config set here and here.

@WUJJU you can try redis-cli config set notify-keyspace-events Ex in your local Redis -> enter redis-cli console -> type config get notify-keyspace-events, it should show xE. Then try to change the config to something else e.g. e by set notify-keyspace-events e, it will show OK but it's because it just set a new key notify-keyspace-event to hold e but it's not changing the config. If you do config get notify-keyspace-events now it will still show xE. If you log in your Heroku Redis instance and input config set notify-keyspace-events Ex it will show (error) ERR unknown command 'config'

@WUJJU
Copy link
Author

WUJJU commented Sep 11, 2016

@josephktcheung Thanks lot for the correction! Didn't expect the difference between set and config set.

@josephktcheung
Copy link

josephktcheung commented Sep 12, 2016

@WUJJU Welcome! Besides, I found out that Redis Cloud, another Heorku Redis add-on provider, allows us to set notify-keyspace-events. Happily using it.

@ghost
Copy link

ghost commented Sep 20, 2016

@josephktcheung How are you setting notify-keyspace-events for Redis Cloud?

@josephktcheung
Copy link

josephktcheung commented Sep 21, 2016

pretty straight forward,

  1. Add Redis Cloud heroku add-on
  2. Go to heroku settings page
  3. Reveal Config Vars in Config Variables
  4. Copy REDISCLOUD_URL, it should be something like redis://rediscloud:PASSWORD@xxx.redislabs.com:PORT_NUMBER
  5. In terminal enter redis-cli -h xxx.redislabs.com -p PORT_NUMBER -a PASSWORD with variables from REDISCLOUD_URL
  6. Once connected, enter config set notify-keyspace-events Ex
  7. You can verify it is set correctly by entering config get notify-keyspace-events
  8. Done!

@ghost
Copy link

ghost commented Sep 22, 2016

thanks @josephktcheung
I got the config notify-keyspace-events set. But I only see my job running one time, not repeatedly every 3 seconds.
Do I need to run the following (as outlined here https://gist.github.com/mojodna/1251812/21455e1dae2e6723a06988ee1978e69f44d89f75):
heroku scale web=1 worker=1

Is something wrong with my code? See below.

// Create a kue instance and a queue.
var redisUrl =  process.env.REDIS_URL;
var kue = require('kue-scheduler');
var Queue = kue.createQueue({
  redis: redisUrl,
  skipConfig: true
});

function emailDailyRecapHandler(request, response) 
{
    console.log("KUE RUNNING: emailDailyRecapHandler");
    done();
}
var jobName = "emailDailyRecap";
// create job instance 
var job = Queue
  .createJob(jobName)
  // Priority can be 'low', 'normal', 'medium', 'high' and 'critical'
  .priority('normal')
  // We don't want to keep the job in memory after it's completed.
  .removeOnComplete(true);

// Schedule it to run every 60 minutes. Function every(interval, job) accepts interval in either a human-interval String format or a cron String format.
Queue.every('3 seconds', job);
// Queue.now(job);

// Processing a scheduled job.
Queue.process(jobName, emailDailyRecapHandler);

@josephktcheung
Copy link

josephktcheung commented Sep 23, 2016

Have you tried the code locally @developermhayden? From what I saw, emailDailyRecapHandler's response parameter should be renamed to done. Otherwise you will get done is not defined

@josephktcheung
Copy link

josephktcheung commented Sep 23, 2016

Besides, @developermhayden are you sure REDIS_URL is the correct environment variable for redis cloud? From what I have in heroku settings, redis url environment variable name for redis cloud by default is REDISCLOUD_URL not REDIS_URL, REDIS_URL is for heroku redis, unless you renamed it. I suggest you to remove heroku redis if it's not used anymore to avoid confusion.

@ghost
Copy link

ghost commented Sep 23, 2016

@josephktcheung I mapped the REDICCLOUD_URL to REDIS_URL.
Your catch on done() was the solution. Thank you.

@ghost
Copy link

ghost commented Sep 26, 2016

Based on the examples in kue-scheduler, I should be able to set my reoccurring job using cron syntax.
I've tried and my jobs run every 2 seconds regardless of what cron syntax I use. After running the job 100 times or so, heroku goes idle and the jobs stop running. When I do a curl request to a parse server endpoint, the server responses correctly but jobs will not start running again until I restart the server by redploying.

I have the following now:

// Run daily at 5pm
Queue.every('*/0 */0 */17 * * *', job);

I have also tried the following, still getting the same results (job is running every 2 seconds)
Queue.every('0 17 * * *', job);

I need my job to run every day at 5pm

My Current code is as follows:

// Create a kue instance and a queue.
var jobTimeSchedule = '0 17 * * *';
var redisUrl =  process.env.REDIS_URL;
var kue = require('kue-scheduler');
var Queue = kue.createQueue({
  redis: redisUrl,
  skipConfig: true
});
var _ = require('underscore');

// jobs
function emailDailyRecapHandler(request, done) 
{
    console.log("KUE RUNNING: emailDailyRecapHandler");
    Parse.Cloud.useMasterKey();

    Parse.Cloud.httpRequest(
    {
        method: 'POST',
        url: urlBase + 'emailDailyRecap',
        headers: 
        {
            'Content-Type': 'application/json;charset=utf-8',
            'X-Parse-Application-Id': Parse.applicationId,
            'X-Parse-REST-API-Key': parseRestApiKey,
            'X-Parse-Webhook-Key': parseWebHookKey
        },
        body: 
        {
            params: {}
        }
    }).then(function(httpResponse) 
    {
        console.log("emailDailyRecap success: " + httpResponse.text);

        return done();
    }, 
    function(error) 
    {
        console.error('emailDailyRecap: request failed with response code ' + error);

        // Pass Error object to done() to mark this job as failed.
        return done(new Error(errorMessage));
    });
}
console.log("Parse.Cloud.job is undefined using Kue-scheduler");
var jobName = "emailDailyRecap";
// create job instance 
var job = Queue
  .createJob(jobName)
  // Priority can be 'low', 'normal', 'medium', 'high' and 'critical'
  .priority('normal')
  // We don't want to keep the job in memory after it's completed.
  .removeOnComplete(true);
  //.unique(jobName);

// Schedule it to run every 60 minutes. Function every(interval, job) accepts interval in either a human-interval String format or a cron String format.
console.log("Schedule job to run: " + jobTimeSchedule);
Queue.every(jobTimeSchedule, job);
// Queue.every('0 0 17 * * *', job);
// Queue.now(job);

// Processing a scheduled job.
Queue.process(jobName, emailDailyRecapHandler);

@lykmapipo
Copy link
Owner

@developermhayden May you please open a new issue regarding cron syntax. Also i will appreciate if you can update kue-scheduler spec to fail on your scenario. That will help me and other contributors to fix its a bug.

@siddheshsatoskar
Copy link

You can also try config set notify-keyspace-events Xe If still does not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants