Class: Izanami::Workers::Watchdog

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

Overview

Utility worker that watch the IZANAMI_SANDBOX repository and updates the content each IZANAMI_WATCHDOG_SLEEP_TIME time via git and bundler.

Instance Method Summary collapse

Instance Method Details

#callObject

Main action. Updates the repo in a loop, waiting the defined time.



15
16
17
18
19
20
# File 'lib/izanami/workers/watchdog.rb', line 15

def call
  loop do
    update_repo
    sleep wait_time
  end
end

#cmdString

Shell commands to keep the repo updated.

Returns:

  • (String)


50
51
52
53
54
55
# File 'lib/izanami/workers/watchdog.rb', line 50

def cmd
  [
    'git pull',
    'bundle install',
  ].join('&&')
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)


43
44
45
# File 'lib/izanami/workers/watchdog.rb', line 43

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

#update_repoObject

Update the repo. Prints to $stdout the results of the shell commands.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/izanami/workers/watchdog.rb', line 23

def update_repo
  Open3.popen2e(cmd, shell_options) do |stdin, stdout, thread|
    while (line = stdout.gets)
      $stdout.puts line.chomp
    end

    status = thread.value.success? ? 'success' : 'fail'
    $stdout.puts "\"#{cmd}\" => #{status}"

    stdin.close
    stdout.close
  end
end

#wait_timeFixnum

Time to wait between each update.

Can be configured with the ENV variable.

Returns:

  • (Fixnum)

    the time (default to 300 seconds).



62
63
64
65
# File 'lib/izanami/workers/watchdog.rb', line 62

def wait_time
  time = ENV['IZANAMI_WATCHDOG_SLEEP_TIME'] || 300
  time.to_i
end