Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the state file an optional feature; fix some comments #19

Merged
merged 2 commits into from
Jun 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ before_install:
- bash download_serf
- bundle install --path .bundle
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
branches:
Expand Down
30 changes: 18 additions & 12 deletions lib/serfx/utils/async_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ module Utils
# in progress. Due to this, long running tasks should not be
# invoked as serf handler directly.
#
# AsyncJob helps buildng serf handlers that involve long running commands.
# AsyncJob helps building serf handlers that involve long running commands.
# It starts the command in background, allowing handler code to
# return immediately. It does double fork where the first child process is
# detached (attached to init as parent process) and and the target long
# running task is spawned as a second child process. This allows the first
# child process to wait and reap the output of actual long running task.
#
# The first child process updates a state file before spawing
# the long ranning task(state='invoking'), during the lon running task
# The first child process updates a state file before spawning
# the long ranning task(state='invoking'), during the long running task
# execution (state='running') and after the spawned process' return
# (state='finished'). This state file provides a convenient way to
# query the current state of an AsyncJob.
#
# AsyncJob porvide four methods to manage jobs. AsyncJob#start will
# AsyncJob provides four methods to manage jobs. AsyncJob#start will
# start the task. Once started, AyncJob#state_info can be used to check
# whether the job is still running or finished. One started a job can be
# either in 'running' state or in 'finished' state. AsyncJob#reap
Expand All @@ -30,6 +30,10 @@ module Utils
# AsyncJob#kill method. A new AyncJob can not be started unless previous
# AsyncJob with same name/state file is reaped.
#
# If the state file is nil, no state will be persisted for the job.
# As such, AsyncJob#state_info, AsyncJob#kill, and AsyncJob#reap will
# be a NO-OP.
#
# Following is an example of writing a serf handler using AsyncJob.
#
# @example
Expand Down Expand Up @@ -86,7 +90,7 @@ class AsyncJob
# @option opts [Symbol] :environment a hash containing environment variables
# @option opts [Symbol] :cwd a string (directory path) containing current directory of the command
def initialize(opts = {})
@state_file = opts[:state] || fail(ArgumentError, 'Specify state file')
@state_file = opts[:state]
@command = opts[:command]
@stdout_file = opts[:stdout] || File::NULL
@stderr_file = opts[:stderr] || File::NULL
Expand All @@ -96,7 +100,7 @@ def initialize(opts = {})

# kill an already running task
#
# @param sig [String] kill signal that will sent to the backgroun process
# @param sig [String] kill signal that will sent to the background process
# @return [TrueClass,FalseClass] true on success, false on failure
def kill(sig = 'KILL')
if running?
Expand All @@ -112,7 +116,7 @@ def kill(sig = 'KILL')
end
end

# obtain current state information about the task as json
# obtain current state information about the task as JSON
#
# @return [String] JSON string representing current state of the task
def state_info
Expand All @@ -130,7 +134,7 @@ def stateinfo
JSON.parse(state_info)
end

# delete the statefile of a finished task
# delete the state file of a finished task
#
# @return [String] 'success' if the task is reaped, 'failed' otherwise
def reap
Expand Down Expand Up @@ -204,14 +208,16 @@ def running?
#
# @return [TrueClass, FalseClass] true if the task exists, else false
def exists?
File.exist?(state_file)
state_file.nil? ? false : File.exist?(state_file)
end

# writes a hash as json in the state_file
# writes a hash as JSON in the state_file
# @param [Hash] state represented as a hash, to be written
def write_state(state)
File.open(state_file, 'w') do |f|
f.write(JSON.generate(state))
if state_file
File.open(state_file, 'w') do |f|
f.write(JSON.generate(state))
end
end
end
end
Expand Down