{"action":"opened","issue":{"url":"https://api.github.com/repos/lykmapipo/kue-scheduler/issues/61","repository_url":"https://api.github.com/repos/lykmapipo/kue-scheduler","labels_url":"https://api.github.com/repos/lykmapipo/kue-scheduler/issues/61/labels{/name}","comments_url":"https://api.github.com/repos/lykmapipo/kue-scheduler/issues/61/comments","events_url":"https://api.github.com/repos/lykmapipo/kue-scheduler/issues/61/events","html_url":"https://github.com/lykmapipo/kue-scheduler/issues/61","id":184116147,"number":61,"title":"\"every\" breaks if two node servers try to schedule a unique job","user":{"login":"ywang-clarify","id":17168067,"avatar_url":"https://avatars.githubusercontent.com/u/17168067?v=3","gravatar_id":"","url":"https://api.github.com/users/ywang-clarify","html_url":"https://github.com/ywang-clarify","followers_url":"https://api.github.com/users/ywang-clarify/followers","following_url":"https://api.github.com/users/ywang-clarify/following{/other_user}","gists_url":"https://api.github.com/users/ywang-clarify/gists{/gist_id}","starred_url":"https://api.github.com/users/ywang-clarify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ywang-clarify/subscriptions","organizations_url":"https://api.github.com/users/ywang-clarify/orgs","repos_url":"https://api.github.com/users/ywang-clarify/repos","events_url":"https://api.github.com/users/ywang-clarify/events{/privacy}","received_events_url":"https://api.github.com/users/ywang-clarify/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-10-20T01:48:22Z","updated_at":"2016-10-20T01:48:22Z","closed_at":null,"body":"Hi,\r\n\r\nI'm running into a problem where if I have two node servers connecting to the same Redis try to use 'every' to set up recurring execution of the same job. What I was hoping for is that regardless of the number of node servers, the total number of executions of this job remains the same. So if two servers both schedule Queue.every('20 seconds', myUniqueJob), myUniqueJob is only executed once (not twice) every 20 seconds.\r\n\r\nWhat I actually see is a 'lock error' event on the first node server:\r\n\r\n> { [LockError: Exceeded 0 attempts to lock the resource \"q:scheduler:locks:every_mail\".]\r\n>   name: 'LockError',\r\n>   message: 'Exceeded 0 attempts to lock the resource \"q:scheduler:locks:every_mail\".' }\r\n\r\nThe second node server shows 0 runs of the job.\r\n\r\nTo reproduce this, I've modified the 'should be able to schedule a unique job to run every 2 seconds from now' to run every 20 seconds instead so I have time to run two tests in parallel.  The modified test is as follows:\r\n```javascript\r\n  it(\r\n    'should be able to schedule a unique job to run every 20 seconds from now',\r\n    function (done) {\r\n\r\n      var data = {\r\n        to: faker.internet.email()\r\n      };\r\n\r\n      var backoff = {\r\n        delay: 60000,\r\n        type: 'fixed'\r\n      };\r\n      var runCount = 0;\r\n      var jobs = [];\r\n\r\n      Queue.process('unique_every', function (job, finalize) {\r\n        //increament run counts\r\n        runCount++;\r\n\r\n        jobs.push(job);\r\n\r\n        /*jshint camelcase:false */\r\n        expect(job.id).to.exist;\r\n        expect(job.type).to.equal('unique_every');\r\n        expect(parseInt(job._max_attempts)).to.equal(3);\r\n        expect(job.data.to).to.equal(data.to);\r\n        expect(job.data.schedule).to.equal('RECCUR');\r\n        expect(job.data.expiryKey).to.equal('q:scheduler:every_mail');\r\n        expect(job.data.dataKey).to.equal(\r\n          'q:scheduler:data:every_mail');\r\n\r\n        expect(job._backoff).to.eql(backoff);\r\n        expect(parseInt(job._priority)).to.equal(0);\r\n        /*jshint camelcase:true */\r\n\r\n        finalize();\r\n      });\r\n\r\n      //listen on success scheduling\r\n      Queue.on('schedule success', function (job) {\r\n        console.log('schedule success');\r\n        if (job.type === 'unique_every') {\r\n          /*jshint camelcase:false */\r\n          expect(job.type).to.equal('unique_every');\r\n          expect(parseInt(job._max_attempts)).to.equal(3);\r\n          expect(job.data.to).to.equal(data.to);\r\n          expect(job._backoff).to.eql(backoff);\r\n          expect(parseInt(job._priority)).to.equal(0);\r\n          /*jshint camelcase:true */\r\n        }\r\n      });\r\n\r\n      Queue.on('already scheduled', function() { console.log('already scheduled');});\r\n      Queue.on('lock error', console.error);\r\n      Queue.on('unlock error', console.error);\r\n      Queue.on('schedule error', console.error);\r\n\r\n      var job = Queue\r\n        .createJob('unique_every', data)\r\n        .attempts(3)\r\n        .backoff(backoff)\r\n        .priority('normal')\r\n        .unique('every_mail');\r\n\r\n      Queue.every('20 seconds', job);\r\n\r\n      //wait for two runtimes of the same job instance to be runned\r\n      setTimeout(function () {\r\n        expect(runCount).to.equal(2);\r\n        var ids = _.map(jobs, 'id');\r\n        expect(ids[0]).to.equal(ids[1]);\r\n        done();\r\n      }, 60015);\r\n    });\r\n```\r\n\r\nWhen running only 1 instance of this test, it passes. \r\nThen I ran only this test in two windows simultaneously.\r\nThe first window shows:\r\n\r\n> Running \"mochaTest:test\" (mochaTest) task\r\n> \r\n> \r\n>   Queue#every\r\n>     √ should be a function\r\n> schedule success\r\n> { [LockError: Exceeded 0 attempts to lock the resource \"q:scheduler:locks:every_mail\".]\r\n>   name: 'LockError',\r\n>   message: 'Exceeded 0 attempts to lock the resource \"q:scheduler:locks:every_mail\".' }\r\n>     1) should be able to schedule a unique job to run every 20 seconds from now\r\n>     2) \"after each\" hook\r\n> \r\n> \r\n>   1 passing (1m)\r\n>   2 failing\r\n> \r\n>   1) Queue#every should be able to schedule a unique job to run every 20 seconds from now:\r\n> \r\n>       Uncaught AssertionError: expected 'Leif.Ziemann@gmail.com' to equal 'Desmond28@hotmail.com'\r\n>       + expected - actual\r\n> \r\n>       -Leif.Ziemann@gmail.com\r\n>       +Desmond28@hotmail.com\r\n> \r\n>     at test\\schedule\\every.spec.js:54:32\r\n> \r\n\r\nThe second window shows:\r\n\r\n> Running \"mochaTest:test\" (mochaTest) task\r\n> \r\n> \r\n>   Queue#every\r\n>     √ should be a function\r\n> schedule success\r\n> schedule success\r\n>     1) should be able to schedule a unique job to run every 20 seconds from now\r\n> \r\n> \r\n>   1 passing (1m)\r\n>   1 failing\r\n> \r\n>   1) Queue#every should be able to schedule a unique job to run every 20 seconds from now:\r\n> \r\n>       Uncaught AssertionError: expected 0 to equal 2\r\n>       + expected - actual\r\n> \r\n>       -0\r\n>       +2\r\n> \r\n>     at [object Object]._onTimeout (test\\schedule\\every.spec.js:97:29)\r\n"}}
{"actor":{"display_login":"ywang-clarify"}}