-
Notifications
You must be signed in to change notification settings - Fork 204
class ShopifyCli::ProcessSupervision
ProcessSupervision wraps a running process spawned by exec
and keeps track
if its pid
and keeps a log file for it as well
-
identifier
a string or a symbol to identify this process by -
pid
process ID for the running process -
time
starttime of the process -
pid_path
filepath to the pidfile for this process -
log_path
filepath to the logfile for this process
run_dir()
see source
# File lib/shopify-cli/process_supervision.rb, line 20
def run_dir
# is the directory where the pid and logfile are kept
File.join(ShopifyCli.cache_dir, "sv")
end
for_ident(identifier)
Will find and create a new instance of ProcessSupervision for a running
process if it is currently running. It will return nil if the process is not
running.
-
identifier
- a string or a symbol that a process was started with
-
process
- ProcessSupervision instance if the process is running this will be nil if the process is not running.
see source
# File lib/shopify-cli/process_supervision.rb, line 38
def for_ident(identifier)
pid, time = File.read(File.join(ShopifyCli::ProcessSupervision.run_dir, "#{identifier}.pid")).split(":")
new(identifier, pid: Integer(pid), time: time)
rescue Errno::ENOENT
nil
end
start(identifier, args)
will fork and spawn a new process that is separate from the current process.
This process will keep running beyond the command running so be careful!
-
identifier
- a string or symbol to identify the new process by. -
args
- a command to run, either a string or array of strings
-
process
- ProcessSupervision instance if the process is running, this will be nil if the process did not start.
see source
# File lib/shopify-cli/process_supervision.rb, line 59
def start(identifier, args)
return for_ident(identifier) if running?(identifier)
# Windows doesn't support forking process without extra gems, so we resort to spawning a new child process -
# that means that it dies along with the original process if it is interrupted. On UNIX, we fork the process so
# that it doesn't have to be restarted on every run.
if Context.new.windows?
pid_file = new(identifier)
# Make sure the file exists and is empty, otherwise Windows fails
File.open(pid_file.log_path, "w") {}
pid = Process.spawn(
*args,
out: pid_file.log_path,
err: pid_file.log_path,
in: Context.new.windows? ? "nul" : "/dev/null",
)
pid_file.pid = pid
pid_file.write
Process.detach(pid)
else
fork do
pid_file = new(identifier, pid: Process.pid)
pid_file.write
STDOUT.reopen(pid_file.log_path, "w")
STDERR.reopen(pid_file.log_path, "w")
STDIN.reopen("/dev/null", "r")
Process.setsid
exec(*args)
end
end
sleep(0.1)
for_ident(identifier)
end
stop(identifier)
will attempt to shutdown a running process
-
identifier
- a string or symbol to identify the new process by.
-
stopped
- [true, false]
see source
# File lib/shopify-cli/process_supervision.rb, line 107
def stop(identifier)
process = for_ident(identifier)
return false unless process
process.stop
end
running?(identifier)
will help identify if your process is still running in the background.
-
identifier
- a string or symbol to identify the new process by.
-
running
- [true, false]
see source
# File lib/shopify-cli/process_supervision.rb, line 124
def running?(identifier)
process = for_ident(identifier)
return false unless process
process.alive?
end
stop()
will attempt to shutdown a running process
-
ctx
- the context of this command
-
stopped
- [true, false]
see source
# File lib/shopify-cli/process_supervision.rb, line 151
def stop
kill_proc
unlink
true
rescue
false
end
alive?()
will help identify if your process is still running in the background.
-
alive
- [true, false]
see source
# File lib/shopify-cli/process_supervision.rb, line 166
def alive?
stat(pid)
end
write()
persists the pidfile
see source
# File lib/shopify-cli/process_supervision.rb, line 173
def write
FileUtils.mkdir_p(File.dirname(pid_path))
File.write(pid_path, "#{pid}:#{time}")
end