{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/kue/issues/856","repository_url":"https://api.github.com/repos/Automattic/kue","labels_url":"https://api.github.com/repos/Automattic/kue/issues/856/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/kue/issues/856/comments","events_url":"https://api.github.com/repos/Automattic/kue/issues/856/events","html_url":"https://github.com/Automattic/kue/issues/856","id":146443645,"number":856,"title":"How can I check the state of the queue to determine if a specific job exists","user":{"login":"kirps","id":698605,"avatar_url":"https://avatars.githubusercontent.com/u/698605?v=3","gravatar_id":"","url":"https://api.github.com/users/kirps","html_url":"https://github.com/kirps","followers_url":"https://api.github.com/users/kirps/followers","following_url":"https://api.github.com/users/kirps/following{/other_user}","gists_url":"https://api.github.com/users/kirps/gists{/gist_id}","starred_url":"https://api.github.com/users/kirps/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kirps/subscriptions","organizations_url":"https://api.github.com/users/kirps/orgs","repos_url":"https://api.github.com/users/kirps/repos","events_url":"https://api.github.com/users/kirps/events{/privacy}","received_events_url":"https://api.github.com/users/kirps/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Automattic/kue/labels/Question","name":"Question","color":"bfe5bf"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2016-04-06T21:39:33Z","updated_at":"2016-04-07T19:54:42Z","closed_at":null,"body":"I need to create a job if and only if it does not exist in one of the states besides failed. The best I've come up with is below, but it loses access to queue events for some reason. Would LOVE some help/guidance.\r\n\r\n```js\r\nvar createJob = function(job_data) {\r\n\tqueue.create('event-test2',{title: 'Event Test2'}).save(); //this job triggers the handler\r\n\t//job type callback hell -- could be promisified or something?\r\n\tkue.Job.rangeByType(job_data.type, 'active', 0, 10000, 'asc', function(err, jobs) {\r\n\t\tif(err){\r\n\t\t\tconsole.log('error checking active jobs.');\r\n\t\t\treturn;\r\n\t\t} else if (jobs.length === 0) {\r\n\t\t\tkue.Job.rangeByType(job_data.type, 'inactive', 0, 10000, 'asc', function(err, jobs) {\r\n\t\t\t\tif(err){\r\n\t\t\t\t\tconsole.log('error checking inactive jobs.');\r\n\t\t\t\t\treturn;\r\n\t\t\t\t} else if (jobs.length === 0) {\r\n\t\t\t\t\tkue.Job.rangeByType(job_data.type, 'complete', 0, 10000, 'asc', function(err, jobs) {\r\n\t\t\t\t\t\tif(err){\r\n\t\t\t\t\t\t\tconsole.log('error checking complete jobs.');\r\n\t\t\t\t\t\t\treturn;\r\n\t\t\t\t\t\t} else if (jobs.length === 0) {\r\n\t\t\t\t\t\t\tkue.Job.rangeByType(job_data.type, 'delayed', 0, 10000, 'asc', function(err, jobs) {\r\n\t\t\t\t\t\t\t\tif(err){\r\n\t\t\t\t\t\t\t\t\tconsole.log('error checking delayed jobs.');\r\n\t\t\t\t\t\t\t\t\treturn;\r\n\t\t\t\t\t\t\t\t} else if (jobs.length === 0) {\r\n\t\t\t\t\t\t\t\t\t//now we know that the job of this type does not exist\r\n\t\t\t\t\t\t\t\t\t//save this on to the queue\r\n\t\t\t\t\t\t\t\t\tconsole.log('calling enqueue job'); //this is written to the console\r\n\t\t\t\t\t\t\t\t\tvar job = queue.create(job_data.type, job_data.options)\r\n\t\t\t\t\t\t\t\t\t\t.delay(job_data.day_of_week.toDate())\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t.save(); //this job is placed on the queue, but does NOT trigger the event handler\r\n\t\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\t});\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t});\r\n\t\t\t\t}\r\n\t\t\t});\r\n\t\t}\t\t\r\n\t});\r\n};\r\n```"},"comment":{"url":"https://api.github.com/repos/Automattic/kue/issues/comments/207065633","html_url":"https://github.com/Automattic/kue/issues/856#issuecomment-207065633","issue_url":"https://api.github.com/repos/Automattic/kue/issues/856","id":207065633,"user":{"login":"kirps","id":698605,"avatar_url":"https://avatars.githubusercontent.com/u/698605?v=3","gravatar_id":"","url":"https://api.github.com/users/kirps","html_url":"https://github.com/kirps","followers_url":"https://api.github.com/users/kirps/followers","following_url":"https://api.github.com/users/kirps/following{/other_user}","gists_url":"https://api.github.com/users/kirps/gists{/gist_id}","starred_url":"https://api.github.com/users/kirps/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kirps/subscriptions","organizations_url":"https://api.github.com/users/kirps/orgs","repos_url":"https://api.github.com/users/kirps/repos","events_url":"https://api.github.com/users/kirps/events{/privacy}","received_events_url":"https://api.github.com/users/kirps/received_events","type":"User","site_admin":false},"created_at":"2016-04-07T19:54:42Z","updated_at":"2016-04-07T19:54:42Z","body":"The issue is unique jobs, thus some uuid is necessary, mind is calculated. Job.id does not seem to be overridable, thus I can't use that uuid in Job.get. \r\n\r\nThus I must query the list of all job.ids for each state, iterate over them, get all the job objects by id, and check for a uuid property in their data. This quickly turns into an async mess.\r\n\r\nAlternatively I can store a map of uuid: job.id in some other data store and check for each job.id. \r\n\r\nThat requires either implementing another data store or at least additional raw redis data, which means sharing the redis client.\r\n\r\nIt feels like this should be trivial, so I feel like I'm missing something."}}