Class: Sqeduler::LockMaintainer

Inherits:
Object
  • Object
show all
Defined in:
lib/sqeduler/lock_maintainer.rb

Overview

This is to ensure that if you set your jobs to run one a time and something goes wrong causing a job to run for a long time, your lock won't expire. This doesn't stop long running jobs, it just ensures you only end up with one long running job rather than 20 of them.

Constant Summary collapse

RUN_INTERVAL =
30
RUN_JITTER =
1..5

Instance Method Summary collapse

Constructor Details

#initializeLockMaintainer

Returns a new instance of LockMaintainer.



11
12
13
# File 'lib/sqeduler/lock_maintainer.rb', line 11

def initialize
  @class_with_locks = {}
end

Instance Method Details

#runObject

This is only done when we initialize Sqeduler, don't need to worry about threading



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sqeduler/lock_maintainer.rb', line 16

def run
  @maintainer_thread ||= Thread.new do
    loop do
      begin
        if redis_lock.lock
          begin
            synchronize
          ensure
            redis_lock.unlock
          end
        end

      rescue => ex
        Service.logger.error "[#{self.class}] #{ex.class}, #{ex.message}"
      end

      sleep RUN_INTERVAL + rand(RUN_JITTER)
    end
  end
end