Class: Izanami::Workers::Command
- Inherits:
-
Struct
- Object
- Struct
- Izanami::Workers::Command
- Defined in:
- lib/izanami/workers/command.rb
Overview
Worker that executes the cap commands and stores the results.
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
-
#redis_options ⇒ Object
Returns the value of attribute redis_options.
-
#shell ⇒ Object
writeonly
Sets the attribute shell.
Instance Method Summary collapse
-
#call ⇒ Object
Executes the cap command with the correct environment and handles the IO streams.
-
#cmd ⇒ String
The cap command.
-
#handle_streams(stdin, stdout, thread) ⇒ Object
Handle the process IO streams.
-
#shell_env ⇒ Hash
ENV vars for the cap command.
-
#shell_options ⇒ Hash
Options for the new process.
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command
12 13 14 |
# File 'lib/izanami/workers/command.rb', line 12 def command @command end |
#redis_options ⇒ Object
Returns the value of attribute redis_options
12 13 14 |
# File 'lib/izanami/workers/command.rb', line 12 def @redis_options end |
#shell=(value) ⇒ Object (writeonly)
Sets the attribute shell
15 16 17 |
# File 'lib/izanami/workers/command.rb', line 15 def shell=(value) @shell = value end |
Instance Method Details
#call ⇒ Object
Executes the cap command with the correct environment and handles the IO streams.
19 20 21 22 23 |
# File 'lib/izanami/workers/command.rb', line 19 def call Open3.popen2e(shell_env, cmd, ) do |stdin, stdout, thread| handle_streams(stdin, stdout, thread) end end |
#cmd ⇒ String
The cap command
54 55 56 |
# File 'lib/izanami/workers/command.rb', line 54 def cmd "bundle exec cap #{command['tasks']}" end |
#handle_streams(stdin, stdout, thread) ⇒ Object
Handle the process IO streams.
It writes the process output to a Channel and saves the status of the command.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/izanami/workers/command.rb', line 64 def handle_streams(stdin, stdout, thread) mapper = Izanami::Mappers::Command.new() channel = Izanami::Channel.new(mapper) channel.write(command['id']) do |input| while (line = stdout.gets) input << line.chomp end end status = thread.value.success? ? 'success' : 'fail' mapper.update(command['id'], 'status', status) stdin.close stdout.close end |
#shell_env ⇒ Hash
ENV vars for the cap command.
This worker is executed inside a ruby process initiated by bundler. Because the cap process is executed in a sandbox with its own bundler ecosystem, we need to remove all the bundler environment variables to have a clean execution.
33 34 35 36 37 38 39 |
# File 'lib/izanami/workers/command.rb', line 33 def shell_env bundler_keys = ENV.select { |var, _| var.to_s.match(/\ABUNDLE/) }.keys bundler_keys.reduce({}) do |hash, (k,_)| hash[k] = nil hash end end |
#shell_options ⇒ Hash
Options for the new process.
Here we define the directory for the cap process, which is the ENV configuration variable.
47 48 49 |
# File 'lib/izanami/workers/command.rb', line 47 def { chdir: ENV['IZANAMI_SANDBOX'] } end |