Skip to content
Browse files

Added BuildListsQueuesMonitoringJob

  • Loading branch information...
1 parent 8dc8f2a commit 813ac91a9987cac3c8f59afcb7f44214b6760463 @avokhmin avokhmin committed Feb 27, 2014
View
40 app/jobs/build_lists_queues_monitoring_job.rb
@@ -0,0 +1,40 @@
+class BuildListsQueuesMonitoringJob
+ @queue = :hook
+
+ def self.perform
+ redis = Resque.redis
+ redis.smembers('queues').each do |key|
+ next if key !~ /(user|mass)_build_/
+
+ queue = "queue:#{key}"
+ id = key.gsub(/[^\d]/, '')
+
+ if redis.llen(queue) == 0
+ if key =~ /^user/
+ last_updated_at = BuildList.select(:updated_at).
+ where(user_id: id).order('updated_at DESC').first
+ else
+ last_updated_at = MassBuild.select(:updated_at).where(id: 250).first
+ end
+ last_updated_at = last_updated_at.try(:updated_at)
+ # cleans queue if no activity and tasks for this queue
+ clean(key, !last_updated_at || (last_updated_at + 5.minutes) < Time.zone.now)
+ else
+ # ensures that user/mass-build in the set from which we select next jobs
+ set_key = key =~ /^user/ ? BuildList::USER_BUILDS_SET : BuildList::MASS_BUILDS_SET
+ redis.sadd set_key, id
+ end
+
+ end
+ end
+
+ def self.clean(key, and_condition)
+ queue = "queue:#{key}"
+ redis.multi do
+ redis.watch queue
+ redis.del queue
+ redis.srem 'queues', key
+ end if redis.llen(queue) == 0 && and_condition
+ end
+
+end
View
22 app/jobs/clean_build_lists_queues_job.rb
@@ -1,22 +0,0 @@
-class CleanBuildListsQueuesJob
- @queue = :hook
-
- def self.perform
- redis = Resque.redis
- redis.smembers('queues').each do |key|
- next if key !~ /(user|mass)_build_/
- queue = "queue:#{key}"
- last_updated_at = BuildList.where(user_id: key.gsub(/[^\d]/, '')).
- order('updated_at DESC').limit(1).pluck(:updated_at).first
- last_updated_at += 5.minutes if last_updated_at
- if redis.llen(queue) == 0 && (!last_updated_at || last_updated_at < Time.zone.now)
- redis.multi do
- redis.watch queue
- redis.del queue
- redis.srem 'queues', key
- end
- end
- end
- end
-
-end
View
12 config/resque_schedule.yml
@@ -12,9 +12,9 @@ build_lists_publish_task_manager:
queue: hook
description: 'Creates tasks for publishing'
-# clean_build_lists_queues:
-# every:
-# - '1m'
-# class: 'CleanBuildListsQueuesJob'
-# queue: hook
-# description: 'Cleans build_lists queues'
+clean_build_lists_queues:
+ every:
+ - '1m'
+ class: 'BuildListsQueuesMonitoringJob'
+ queue: hook
+ description: 'Cleans build_lists queues'

0 comments on commit 813ac91

Please sign in to comment.
Something went wrong with that request. Please try again.