Sha256: a8c865ab12ddd6e095c95801fb4f748b98121ea49f3b05685ca56a837b0c453d

Contents?: true

Size: 1.66 KB

Versions: 5

Compression:

Stored size: 1.66 KB

Contents

require 'open3'
require 'izanami/worker'

module Izanami
  module Workers

    # Utility worker that watch the IZANAMI_SANDBOX repository
    # and updates the content each IZANAMI_WATCHDOG_SLEEP_TIME time
    # via {git} and {bundler}.
    class Watchdog
      Izanami::Worker self

      # Main action. Updates the repo in a loop,
      # waiting the defined time.
      def call
        loop do
          update_repo
          sleep wait_time
        end
      end

      # Update the repo. Prints to $stdout the results of the shell commands.
      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

      # Options for the new process.
      #
      # Here we define the directory for the {cap} process, which is the
      # ENV['IZANAMI_SANDBOX'] configuration variable.
      #
      # @return [Hash]
      def shell_options
        { chdir: ENV['IZANAMI_SANDBOX'] }
      end

      # Shell commands to keep the repo updated.
      #
      # @return [String]
      def cmd
        [
          'git pull',
          'bundle install',
        ].join('&&')
      end

      # Time to wait between each update.
      #
      # Can be configured with the ENV['IZANAMI_WATCHDOG_SLEEP_TIME'] variable.
      #
      # @return [Fixnum] the time (default to 300 seconds).
      def wait_time
        time = ENV['IZANAMI_WATCHDOG_SLEEP_TIME'] || 300
        time.to_i
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
izanami-0.20.0 lib/izanami/workers/watchdog.rb
izanami-0.19.0 lib/izanami/workers/watchdog.rb
izanami-0.18.0 lib/izanami/workers/watchdog.rb
izanami-0.17.0 lib/izanami/workers/watchdog.rb
izanami-0.16.0 lib/izanami/workers/watchdog.rb