lib/scripterator/runner.rb in scripterator-0.1.1 vs lib/scripterator/runner.rb in scripterator-1.0.0
- old
+ new
@@ -15,20 +15,22 @@
@model ||= Proc.new { eval("#{@inferred_model_name}") } # constantize
end
def run(options = {})
- unless options[:start_id] || options[:end_id]
- raise 'You must provide either a start ID or end ID'
+ unless (options[:start_id] || options[:end_id]) || options[:id_list]
+ raise 'You must provide either a start ID or end ID, or a comma-delimited id list'
end
+ @id_list = options[:id_list] || []
@start_id = options[:start_id] || 1
@end_id = options[:end_id]
@redis_expiration = options[:redis_expiration]
@output_stream = options[:output_stream] || $stdout
raise 'No per_record code defined' unless @per_record
+ output_init_details
init_vars
run_blocks
output_stats
end
@@ -77,10 +79,15 @@
if @errors.count > 0 && !failed_ids.empty?
output " Retrieve failed IDs with redis: SMEMBERS #{script_key(:failed)}"
end
end
+ def output_init_details
+ output "Checked IDs being stored in redis list: #{script_key(:checked)}"
+ output "Failed IDs being stored in redis list: #{script_key(:failed)}"
+ end
+
def output(*args)
@output_stream.puts(*args)
end
def run_blocks
@@ -93,16 +100,23 @@
self.instance_eval(&@after) if @after
end
def run_loop
- if @end_id
- (@start_id..@end_id).each { |id| transform_one_record(fetch_record(id)) }
+ if @id_list.count > 0
+ run_for_id_list
+ elsif @end_id
+ @id_list = (@start_id..@end_id)
+ run_for_id_list
else
model_finder.find_each(start: @start_id) { |record| transform_one_record(record) }
end
expire_redis_sets
+ end
+
+ def run_for_id_list
+ @id_list.each { |id| transform_one_record(fetch_record(id)) }
end
def transform_one_record(record)
return if record.nil?