Class: RerunTask::ProcessFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rerun_task/process_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid_dir) ⇒ ProcessFile

Returns a new instance of ProcessFile.



7
8
9
10
# File 'lib/rerun_task/process_file.rb', line 7

def initialize(pid_dir)
  @pid_dir = pid_dir
  FileUtils.mkdir_p(@pid_dir) unless Dir.exists?(@pid_dir)
end

Instance Attribute Details

#pidObject

Returns the value of attribute pid.



5
6
7
# File 'lib/rerun_task/process_file.rb', line 5

def pid
  @pid
end

#process_nameObject

Returns the value of attribute process_name.



5
6
7
# File 'lib/rerun_task/process_file.rb', line 5

def process_name
  @process_name
end

Class Method Details

.create_file_path(pid_dir, process_name) ⇒ Object



29
30
31
32
# File 'lib/rerun_task/process_file.rb', line 29

def self.create_file_path(pid_dir, process_name)
  basename = Pathname.new("#{pid_dir}/#{process_name}.pid").basename
  "#{pid_dir}/#{basename}"
end

.load_file(pid_dir, process_name) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/rerun_task/process_file.rb', line 46

def self.load_file(pid_dir, process_name)
  f = File.open(create_file_path(pid_dir, process_name), 'r')
  p = ProcessFile.new(pid_dir)
  args = parse_file(f.read)
  p.process_name = process_name.nil? ? args[:process_name] : process_name
  p.pid = args[:pid]
  f.close
  p
end

.parse_file(string) ⇒ Object



56
57
58
59
# File 'lib/rerun_task/process_file.rb', line 56

def self.parse_file(string)
  a = string.split("\n")
  { pid:  a.first, process_name: a.last }
end

Instance Method Details

#create_pid_fileObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rerun_task/process_file.rb', line 12

def create_pid_file
  if File.exists?(path)
    puts "file exists #{path}"

    process_from_file = ProcessFile.load_file(@pid_dir, process_name)
    raise RuntimeError.new("Process #{process_name} is already running with pid #{process_from_file.pid}") if process_from_file.process_exists?
  end

  File.open(path, 'w+') do |f|
    f.write(self.to_s)
  end
end

#destroyObject



61
62
63
# File 'lib/rerun_task/process_file.rb', line 61

def destroy
  FileUtils.rm(path)
end

#pathObject



25
26
27
# File 'lib/rerun_task/process_file.rb', line 25

def path
  ProcessFile.create_file_path(@pid_dir, process_name)
end

#process_exists?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/rerun_task/process_file.rb', line 42

def process_exists?
  extract_process_name(pid) == @process_name
end

#to_sObject



38
39
40
# File 'lib/rerun_task/process_file.rb', line 38

def to_s
  "#{Process.pid}\n#{process_name}"
end