Module: Flydata::Command::ExclusiveRunnable::ClassMethods

Defined in:
lib/flydata/command/exclusive_runnable.rb

Instance Method Summary collapse

Instance Method Details

#command(subcommand, options = {}) ⇒ Object


62
63
64
65
66
67
68
69
# File 'lib/flydata/command/exclusive_runnable.rb', line 62

def command(subcommand, options = {})
  return options[:command] if options[:command]

  modules = self.name.split('::')

  cmd = modules.last.downcase
  subcommand == :run ? cmd : "#{cmd}:#{subcommand}"
end

#delete_exclusive_run_infoObject


57
58
59
60
# File 'lib/flydata/command/exclusive_runnable.rb', line 57

def delete_exclusive_run_info
  path = exclusive_run_info_path
  File.delete(path) if File.exists?(path)
end

#exclusive_run_homeObject


35
36
37
# File 'lib/flydata/command/exclusive_runnable.rb', line 35

def exclusive_run_home
  @@exclusive_run_home
end

#exclusive_run_home=(value) ⇒ Object


38
39
40
# File 'lib/flydata/command/exclusive_runnable.rb', line 38

def exclusive_run_home=(value)
  @@exclusive_run_home = value
end

#exclusive_run_info_pathObject


41
42
43
# File 'lib/flydata/command/exclusive_runnable.rb', line 41

def exclusive_run_info_path
  File.join(exclusive_run_home, "exclusive_run.info")
end

#load_exclusive_run_infoObject


45
46
47
48
49
50
# File 'lib/flydata/command/exclusive_runnable.rb', line 45

def load_exclusive_run_info
  path = exclusive_run_info_path
  return nil unless File.exists?(path)

  JSON.parse(File.open(path){|f| f.read})
end

#run_exclusive(method, options = {}) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/flydata/command/exclusive_runnable.rb', line 14

def run_exclusive(method, options = {})
  saved_method = :"__#{method}"
  alias_method saved_method, method

  define_method(method) do |*args|
    result = nil
    exclusive_run_info = nil
    begin
      info = self.class.load_exclusive_run_info
      if info
        raise "Command `#{info['command']}` is already running.  Wait until the command completes or stop the command.  (pid:#{info['pid']})"
      end
      exclusive_run_info = { command: self.class.command(method, options), pid: Process.pid }
      self.class.save_exclusive_run_info(exclusive_run_info)
      result = send(saved_method, *args)
    ensure
      self.class.delete_exclusive_run_info if exclusive_run_info
    end
    result
  end
end

#save_exclusive_run_info(info) ⇒ Object


52
53
54
55
# File 'lib/flydata/command/exclusive_runnable.rb', line 52

def save_exclusive_run_info(info)
  path = exclusive_run_info_path
  File.open(path, "w"){|f| f.write(info.to_json)}
end