Class: SilverSpurs::Asyncifier

Inherits:
Object
  • Object
show all
Extended by:
SingleForwardable
Includes:
Singleton
Defined in:
lib/silver_spurs/asyncifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_pathObject



16
17
18
# File 'lib/silver_spurs/asyncifier.rb', line 16

def base_path
  @base_path ||= './silver_spurs_async'
end

#loggerObject



20
21
22
23
24
25
26
# File 'lib/silver_spurs/asyncifier.rb', line 20

def logger
  unless @logger
    @logger = Logger.new(STDERR)
    @logger.level = Logger::DEBUG
  end
  @logger
end

#timeoutObject



12
13
14
# File 'lib/silver_spurs/asyncifier.rb', line 12

def timeout
  @timeout ||= 60 * 60
end

Instance Method Details

#exists?(process_name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/silver_spurs/asyncifier.rb', line 76

def exists?(process_name)
  return true if has_lock? process_name
  File.exists? log_file_path(process_name)
end

#get_log(process_name) ⇒ Object



50
51
52
53
# File 'lib/silver_spurs/asyncifier.rb', line 50

def get_log(process_name)
  return nil unless File.exists? log_file_path(process_name)
  File.read log_file_path(process_name)
end

#has_lock?(process_name) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/silver_spurs/asyncifier.rb', line 37

def has_lock?(process_name)
  return false unless File.exists? pid_file_path(process_name)
  
  pid = File.read(pid_file_path(process_name)).to_i
  IO.popen("ps -o command -p #{pid}").read.split("\n").count == 2
end

#reap_old_process(process_name) ⇒ Object



69
70
71
72
73
74
# File 'lib/silver_spurs/asyncifier.rb', line 69

def reap_old_process(process_name)
  if has_lock? process_name
    launch_time = File.mtime pid_file_path(process_name)
    reap_process process_name if Time.now - launch_time > timeout
  end
end

#reap_orphaned_lock(process_name) ⇒ Object



55
56
57
# File 'lib/silver_spurs/asyncifier.rb', line 55

def reap_orphaned_lock(process_name)
  File.delete pid_file_path(process_name) unless has_lock? process_name
end

#reap_process(process_name) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/silver_spurs/asyncifier.rb', line 59

def reap_process(process_name)
  if has_lock? process_name
    pid = File.read(pid_file_path(process_name)).to_i
    Process.kill 'KILL', pid
    sleep 1
    Process.kill('TERM', pid) if has_lock? process_name
  end
  reap_orphaned_lock process_name
end

#spawn_process(process_name, command) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/silver_spurs/asyncifier.rb', line 28

def spawn_process(process_name, command)
  create_directory_tree
  logged_command = "#{command} 1> #{log_file_path process_name} 2>&1 && touch #{success_file_path process_name}"
  logger.debug "Executing: #{logged_command}"
  pid = Process.spawn logged_command
  File.open(pid_file_path(process_name), 'wb') { |f| f.write pid }
  Process.detach pid
end

#success?(process_name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/silver_spurs/asyncifier.rb', line 44

def success?(process_name)
  return false unless File.exists? success_file_path(process_name)
  return true unless File.exists? pid_file_path(process_name)
  File.mtime(success_file_path(process_name)) >= File.mtime(pid_file_path(process_name))
end