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
Changes from 1 commit
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
22 changes: 14 additions & 8 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 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
# @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))
unless state_file.nil?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change this to just if state_file

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

File.open(state_file, 'w') do |f|
f.write(JSON.generate(state))
end
end
end
end
Expand Down