Class: Resque::Worker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jobs_per_forkObject

Returns the value of attribute jobs_per_fork.



7
8
9
# File 'lib/resque-multi-job-forks.rb', line 7

def jobs_per_fork
  @jobs_per_fork
end

#jobs_processedObject (readonly)

Returns the value of attribute jobs_processed.



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

def jobs_processed
  @jobs_processed
end

#seconds_per_forkObject

Returns the value of attribute seconds_per_fork.



6
7
8
# File 'lib/resque-multi-job-forks.rb', line 6

def seconds_per_fork
  @seconds_per_fork
end

Instance Method Details

#fork_hijacked?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/resque-multi-job-forks.rb', line 27

def fork_hijacked?
  @release_fork_limit
end

#fork_job_limitObject



49
50
51
# File 'lib/resque-multi-job-forks.rb', line 49

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)


53
54
55
# File 'lib/resque-multi-job-forks.rb', line 53

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

#fork_job_limit_remainingObject



57
58
59
# File 'lib/resque-multi-job-forks.rb', line 57

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

#hijack_forkObject



31
32
33
34
35
36
37
38
# File 'lib/resque-multi-job-forks.rb', line 31

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



65
66
67
# File 'lib/resque-multi-job-forks.rb', line 65

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



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

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



40
41
42
43
44
45
46
47
# File 'lib/resque-multi-job-forks.rb', line 40

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.'
  @shutdown = true unless $TESTING
end

#shutdown_with_multi_job_forksObject Also known as: shutdown?



19
20
21
22
# File 'lib/resque-multi-job-forks.rb', line 19

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