Class: LockedProcess

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

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.0.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid_file) ⇒ LockedProcess

Returns a new instance of LockedProcess.



10
11
12
13
14
15
16
17
# File 'lib/locked_process.rb', line 10

def initialize(pid_file)
  unless block_given?
    raise ArgumentError.new('Block must be given')
  end

  @process = lambda { yield }
  @pid_file = pid_file
end

Instance Attribute Details

#pid_fileObject (readonly)

Returns the value of attribute pid_file.



8
9
10
# File 'lib/locked_process.rb', line 8

def pid_file
  @pid_file
end

Instance Method Details

#executeObject



23
24
25
26
27
28
29
# File 'lib/locked_process.rb', line 23

def execute
  create_pid_file
  Thread.new do
    @process.call
    remove_pid_file
  end
end

#pid_file_exists?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/locked_process.rb', line 19

def pid_file_exists?
  File.exists?(pid_file)
end