From 1e29f810ccf99eacad7ca8326ddf7f5be28eb188 Mon Sep 17 00:00:00 2001 From: Kevin Tyll Date: Wed, 9 Mar 2011 11:37:45 -0500 Subject: [PATCH] Forgot to implement the json API Documented the throttle method. --- README.markdown | 23 +++++++++++++++++++---- app/controllers/resque_controller.rb | 6 ++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.markdown b/README.markdown index 23f2a2e..88c8a4a 100644 --- a/README.markdown +++ b/README.markdown @@ -29,7 +29,7 @@ This plugin requires the resque 1.5 or higher gem and of course the redis gem. This gem now required the resque-status 0.2.2 or higher gem as well. sudo gem install resque-status - + Times Fixed ----------- @@ -121,15 +121,30 @@ You can call tick or set_status to add messages along the way too. You will want to override the completed method so that it isn't called until the very end of the entire process. -I've also added two more methods, #incr_counter(:counter) and #count(:counter). We have dozens of single_record_loader +I've also added two more methods, #incr_counter(:counter) and #count(:counter). We have dozens of single_record_loader workers processing records at a time. You encounter a race condition when they are all calling #at at the same time to -update the :num attribute. So I created these two methods to atomically increment a dedicated counter. Just call -#incr_counter and pass in a symbol for what you want to call the counter. You can create any number of different counters +update the :num attribute. So I created these two methods to atomically increment a dedicated counter. Just call #incr_counter +and pass in a symbol for what you want to call the counter. You can create any number of different counters for different purposes. We keep track of different validation issues for each record. Use #counter and pass is the same symbol to read the integer back. The redis entries created by these methods all get cleaned up with a call to Resque::Status.clear(uuid) When you kill a job on the UI, it will kill all the workers in the chain. +Throttle a Queue +---------------- + +A throttle method has been added. This is useful if you have a queue that tends to have very high volume, for example, +the queue that process all the individual records of a file. You don't want to load that queue up with 1 million entries, +possibly blowing out the memory of your Redis server. + + FasterCSV.foreach(self.file_path, :headers => true, :quote_char => '"') do |row| + Resque.throttle(:single_record_loader, 10000, 30) + SingleRecordLoader.create({'uuid' => uuid, 'row_data' => row.to_hash, 'rows_in_file' => total_rows}) + end + +Putting the throttle before enqueing the SingleRecordLoader will check the single_record_loader queue to make sure it has +less than 10000 entries in it before proceeding. If is has 10000 or more entries, it will sleep for 30 seconds before checking again. + Manage Workers -------------- diff --git a/app/controllers/resque_controller.rb b/app/controllers/resque_controller.rb index 5c571bc..72f058e 100644 --- a/app/controllers/resque_controller.rb +++ b/app/controllers/resque_controller.rb @@ -181,6 +181,9 @@ def statuses @end = @start + (params[:per_page] || 20) @statuses = Resque::Status.statuses(@start, @end) @size = Resque::Status.status_ids.size + if params[:format] == 'js' + render :text => @statuses.to_json + end end def clear_statuses @@ -190,6 +193,9 @@ def clear_statuses def status @status = Resque::Status.get(params[:id]) + if params[:format] == 'js' + render :text => @status.to_json + end end def kill