Class: Workers::System

Inherits:
Object
  • Object
show all
Extended by:
Resque::Plugins::Result
Defined in:
lib/workers/system.rb

Class Method Summary collapse

Class Method Details

.create_blast_fetch_command(db_names, hit_id, hit_display_id, algo) ⇒ Object

Create fetch command based on config/quorum_settings.yml



80
81
82
83
84
85
86
87
88
89
# File 'lib/workers/system.rb', line 80

def self.create_blast_fetch_command(db_names, hit_id, hit_display_id, algo)
  # System command
  cmd = ""

  fetch = File.join(Quorum.blast_bin, "fetch")
  cmd << "#{fetch} -f blastdbcmd -l #{Quorum.blast_log_dir} " <<
    "-m #{Quorum.blast_tmp_dir} -d #{Quorum.blast_db} " <<
    "-n '#{db_names}' -b '#{hit_id}' -s '#{hit_display_id}' " <<
    "-a #{algo}"
end

.create_search_command(algo, id) ⇒ Object

Create search command based on config/quorum_settings.yml



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/workers/system.rb', line 94

def self.create_search_command(algo, id)
  # System command
  cmd = ""
  return cmd unless Quorum::SUPPORTED_ALGORITHMS.include?(algo)

  case
  when Quorum::BLAST_ALGORITHMS.include?(algo)
    search = File.join(Quorum.blast_bin, "search")
    cmd << "#{search} -l #{Quorum.blast_log_dir} " <<
      "-m #{Quorum.blast_tmp_dir} -b #{Quorum.blast_db} " <<
      "-t #{Quorum.blast_threads} "
  else
    return cmd
  end

  cmd << "-s #{algo} -i #{id} " <<
    "-d #{ActiveRecord::Base.configurations[::Rails.env.to_s]['database']} " <<
    "-a #{ActiveRecord::Base.configurations[::Rails.env.to_s]['adapter']} " <<
    "-k #{ActiveRecord::Base.configurations[::Rails.env.to_s]['host']} " <<
    "-u #{ActiveRecord::Base.configurations[::Rails.env.to_s]['username']} " <<
    "-p '#{ActiveRecord::Base.configurations[::Rails.env.to_s]['password']}' "
end

.execute_sshObject

Execute command on remote machine.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/workers/system.rb', line 53

def self.execute_ssh
  Net::SSH.start(@ssh_host, @ssh_user, @ssh_options) do |ssh|
    ssh.open_channel do |ch|
      ch.exec(@cmd) do |ch, success|
        if success
          # Capture STDOUT from ch.exec()
          if @stdout
            ch.on_data do |ch, data|
              @out = data
            end
          end
          # Read the exit status of the remote process.
          ch.on_request("exit-status") do |ch, data|
            @exit_status = data.read_long
          end
        else
          Rails.logger.warn "Channel Net::SSH exec() failed. :'("
        end
      end
    end
    ssh.loop
  end
end

.perform(meta_id, cmd, remote, ssh_host, ssh_user, ssh_options = {}, stdout = false) ⇒ Object

Resque worker method.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/workers/system.rb', line 11

def self.perform(meta_id, cmd, remote, ssh_host, ssh_user, ssh_options = {}, stdout = false)
  @meta_id     = meta_id
  @cmd         = cmd
  @remote      = remote
  @ssh_host    = ssh_host
  @ssh_user    = ssh_user
  @ssh_options = ssh_options
  @stdout      = stdout
  @exit_status = 1
  @out         = ""

  self.set_ssh_options

  if remote
    self.execute_ssh
  else
    @out         = `#{@cmd}`
    @exit_status = $?.exitstatus
  end

  if @exit_status > 0
    raise "Worker failed :'(. See quorum/log/quorum.log for more information."
  end

  @out if stdout
end

.set_ssh_optionsObject

Convert each key in ssh_options to a symbol.



41
42
43
44
45
46
47
48
# File 'lib/workers/system.rb', line 41

def self.set_ssh_options
  unless @ssh_options.empty?
    @ssh_options = @ssh_options.inject({}) do |memo, (k, v)|
      memo[k.to_sym] = v
    memo
    end
  end
end