Sha256: 0b2ea6412e9377282de83a8ec7bbf495da6629ca13ec28b136f972c2db14df5a

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 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}.
    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 {git} command.
      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

      # {git} command for the updating.
      #
      # @return [String]
      def cmd
        'git pull'
      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

2 entries across 2 versions & 1 rubygems

Version Path
izanami-0.15.0 lib/izanami/workers/watchdog.rb
izanami-0.14.0 lib/izanami/workers/watchdog.rb