Class: Izanami::Workers::Command

Inherits:
Struct
  • Object
show all
Defined in:
lib/izanami/workers/command.rb

Overview

Worker that executes the cap commands and stores the results.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandObject

Returns the value of attribute command

Returns:

  • (Object)

    the current value of command



12
13
14
# File 'lib/izanami/workers/command.rb', line 12

def command
  @command
end

#redis_optionsObject

Returns the value of attribute redis_options

Returns:

  • (Object)

    the current value of redis_options



12
13
14
# File 'lib/izanami/workers/command.rb', line 12

def redis_options
  @redis_options
end

#shell=(value) ⇒ Object (writeonly)

Sets the attribute shell

Parameters:

  • value

    the value to set the attribute shell to.



15
16
17
# File 'lib/izanami/workers/command.rb', line 15

def shell=(value)
  @shell = value
end

Instance Method Details

#callObject

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, shell_options) do |stdin, stdout, thread|
    handle_streams(stdin, stdout, thread)
  end
end

#cmdString

The cap command

Returns:

  • (String)


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.

See Also:

  • Izanami::Workers::Command.{Open3{Open3.popen2e}


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(redis_options)
  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_envHash

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.

Returns:

  • (Hash)


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_optionsHash

Options for the new process.

Here we define the directory for the cap process, which is the ENV configuration variable.

Returns:

  • (Hash)


47
48
49
# File 'lib/izanami/workers/command.rb', line 47

def shell_options
  { chdir: ENV['IZANAMI_SANDBOX'] }
end