Class: Resque::Plugins::BatchedLogger

Inherits:
Job
  • Object
show all
Defined in:
lib/resque/plugins/batched_logger.rb

Constant Summary collapse

LOG_FILE =
"log/batched_jobs.log"
@@logger =
nil

Class Method Summary collapse

Class Method Details

.perform(batch_name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/resque/plugins/batched_logger.rb', line 19

def self.perform(batch_name)
  begin
    FileUtils.mkdir_p(File.dirname(LOG_FILE))
    FileUtils.touch(LOG_FILE)

    if !batch_to_log?(batch_name)
      logger.puts("No jobs to log for '#{batch_name}'")
    elsif jobs_pending?(batch_name)
      sleep 5                          # Wait 5 seconds
      Resque.enqueue(self, batch_name) # Requeue, to check again
    else
      # Start processing the logs
      # Pull in the info stored in redis
      job_stats = {
        :processing_time => 0,      :longest_processing_time => 0,
        :processed_job_count => 0,  :enqueued_job_count => Resque.redis.get("#{batch_name}:jobcount"),
        :start_time => nil,         :finish_time => nil
      }
      Resque.redis.del("#{batch_name}:jobcount") # Stop any other logging processes picking up the same job
      # lpop pops the first element off the list in redis
      while job = Resque.redis.lpop("batch_stats:#{batch_name}")
        job = decode_job(job) # Decode from string format
        # job => [run_time, start_time, end_time]
        job_stats[:processing_time] += job[0]                                                           # run_time
        job_stats[:longest_processing_time] = job[0] if job[0] > job_stats[:longest_processing_time]
        job_stats[:start_time] ||= job[1]                                                               # start_time
        job_stats[:finish_time] ||= job[2]
        job_stats[:finish_time] = job[2] if job[2] > job_stats[:finish_time]                            # end_time
        job_stats[:processed_job_count] += 1
      end
      Resque.redis.del("batch_stats:#{batch_name}") # Cleanup the array of stats we've just processed (it should be empty now)

      if job_stats[:processed_job_count].zero?
        log_empty_batch(batch_name)
      else
        log_stats(batch_name, job_stats)
      end
    end
  ensure
    logger.close
    @@logger = nil
  end
end

.requeue_allObject

Requeue’s all loggers



13
14
15
16
17
# File 'lib/resque/plugins/batched_logger.rb', line 13

def self.requeue_all
  Resque.redis.keys("*:jobcount").each do |key|
    Resque.enqueue(self, key.gsub(":jobcount", ""))
  end
end