Class: Resque::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/resque-multi-job-forks.rb

Overview

Add support to allow a single fork to process multiple jobs. You may limit the fork to n minutes of execution time, or a total job limit.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jobs_per_forkObject

total jobs to process per fork.



10
11
12
# File 'lib/resque-multi-job-forks.rb', line 10

def jobs_per_fork
  @jobs_per_fork
end

#jobs_processedObject (readonly)

jobs processed by this fork.



11
12
13
# File 'lib/resque-multi-job-forks.rb', line 11

def jobs_processed
  @jobs_processed
end

#seconds_per_forkObject

seconds fork may process jobs for.



9
10
11
# File 'lib/resque-multi-job-forks.rb', line 9

def seconds_per_fork
  @seconds_per_fork
end

Instance Method Details

#fork_hijacked?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/resque-multi-job-forks.rb', line 30

def fork_hijacked?
  @release_fork_limit
end

#fork_job_limitObject



51
52
53
# File 'lib/resque-multi-job-forks.rb', line 51

def fork_job_limit
  jobs_per_fork.nil? ? Time.now.to_i + seconds_per_fork : jobs_per_fork
end

#fork_job_limit_reached?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/resque-multi-job-forks.rb', line 55

def fork_job_limit_reached?
  fork_job_limit_remaining <= 0 ? true : false
end

#fork_job_limit_remainingObject



59
60
61
# File 'lib/resque-multi-job-forks.rb', line 59

def fork_job_limit_remaining
  jobs_per_fork.nil? ? @release_fork_limit - Time.now.to_i : jobs_per_fork - @jobs_processed
end

#hijack_forkObject



34
35
36
37
38
39
40
41
# File 'lib/resque-multi-job-forks.rb', line 34

def hijack_fork
  log 'hijack fork.'
  @suppressed_fork_hooks = [Resque.after_fork, Resque.before_fork]
  Resque.after_fork = Resque.before_fork = nil
  @release_fork_limit = fork_job_limit
  @jobs_processed = 0
  @cant_fork = true
end

#minutes_per_forkObject



67
68
69
# File 'lib/resque-multi-job-forks.rb', line 67

def minutes_per_fork
  ENV['MINUTES_PER_FORK'].nil? ? 1 : ENV['MINUTES_PER_FORK'].to_i
end

#perform_with_multi_job_forks(job = nil) ⇒ Object Also known as: perform



14
15
16
17
18
# File 'lib/resque-multi-job-forks.rb', line 14

def perform_with_multi_job_forks(job = nil)
  perform_without_multi_job_forks(job)
  hijack_fork unless fork_hijacked?
  @jobs_processed += 1
end

#release_forkObject



43
44
45
46
47
48
49
# File 'lib/resque-multi-job-forks.rb', line 43

def release_fork
  log "jobs processed by child: #{jobs_processed}"
  run_hook :before_child_exit, self
  Resque.after_fork, Resque.before_fork = *@suppressed_fork_hooks
  @release_fork_limit = @jobs_processed = @cant_fork = nil
  log 'hijack over, counter terrorists win.'
end

#shutdown_with_multi_job_forksObject Also known as: shutdown?



22
23
24
25
# File 'lib/resque-multi-job-forks.rb', line 22

def shutdown_with_multi_job_forks
  release_fork if fork_hijacked? && fork_job_limit_reached?
  shutdown_without_multi_job_forks
end