Module: Revenant

Defined in:
lib/locks/mysql.rb,
lib/revenant.rb,
lib/revenant/pid.rb,
lib/revenant/task.rb,
lib/revenant/manager.rb

Overview

Each lock module must implement a singleton method called lock_function This method should return a proc that will be called with a lock_name argument. The proc should return true if the lock has been acquired, false otherwise

Lock modules may expose other methods to users as needed, but only lock_function is required. Modules register new lock types by calling Revenant.register(name, klass)

Defined Under Namespace

Modules: Daemon, Manager, MySQL Classes: PID, Task

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.find_module(lock_type) ⇒ Object



16
17
18
19
20
21
# File 'lib/revenant.rb', line 16

def self.find_module(lock_type)
  @lock_types ||= {}
  @lock_types.fetch(lock_type.to_sym) do
    raise ArgumentError, "unknown lock type: #{lock_type.inspect}"
  end
end

.initObject



27
28
29
30
31
# File 'lib/revenant.rb', line 27

def self.init
  require 'revenant/task'
  require_dir "locks"
  require_dir "plugins"
end

.pluginsObject



23
24
25
# File 'lib/revenant.rb', line 23

def self.plugins
  @plugins ||= {}
end

.register(lock_type, klass) ⇒ Object

Register a new type of lock. User code specifies which by setting lock_type = :something while configuring a Revenant::Task



7
8
9
10
11
12
13
14
# File 'lib/revenant.rb', line 7

def self.register(lock_type, klass)
  @lock_types ||= {}
  if klass.respond_to?(:lock_function)
    @lock_types[lock_type.to_sym] = klass
  else
    raise ArgumentError, "#{klass} must have a `lock_function` that returns a callable object"
  end
end

.require_dir(relative_path) ⇒ Object

Given ‘locks/foo’, will require ./locks/foo/*.rb’ with normalized (relative) paths. (e.g. require ‘locks/foo/example’ for example.rb)



35
36
37
38
39
40
41
42
43
44
# File 'lib/revenant.rb', line 35

def self.require_dir(relative_path)
  current = File.expand_path('..', __FILE__)
  make_relative = /#{current}\//
  $LOAD_PATH << current unless $LOAD_PATH.include?(current)
  pattern = File.join(current, relative_path, '*.rb')
  Dir[pattern].each do |full_path|
    relative_name = full_path.gsub(make_relative,'').gsub(/\.rb$/,'')
    require relative_name
  end
end

.working_directoryObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/revenant.rb', line 46

def self.working_directory
  # If the 'PWD' environment variable points to our
  # current working directory, use it instead of Dir.pwd.
  # It may have a better name for the same destination,
  # in the presence of symlinks.
  e = File.stat(env_pwd = ENV['PWD'])
  p = File.stat(Dir.pwd)
  e.ino == p.ino && e.dev == p.dev ? env_pwd : Dir.pwd
rescue
  Dir.pwd
end