Sha256: 15f6cb27e700898abfbfe5d7fdb796d3d6c440b67c06b1a5413f349d441d65c8

Contents?: true

Size: 1.25 KB

Versions: 28

Compression:

Stored size: 1.25 KB

Contents

module ForemanInventoryUpload
  module Async
    module DelayedStart
      extend ActiveSupport::Concern

      START_WINDOW = 3.hours.seconds

      def after_delay(delay = nil, logger: nil, &block)
        logger ||= self.logger if respond_to? :logger
        delay ||= ForemanRhCloud.requests_delay || Random.new.rand(START_WINDOW)
        delay = delay.to_i

        logger&.debug("planning a delay for #{delay} seconds before the rest of the execution")

        sequence do
          plan_action(ForemanInventoryUpload::Async::DelayAction, delay)
          concurrence(&block)
        end
      end

      def humanized_name
        _('Wait and %s' % super)
      end
    end

    class DelayAction < ::Actions::EntryAction
      Wake = Algebrick.atom

      def plan(delay)
        plan_self(delay: delay)
      end

      def run(event = nil)
        case event
        when nil
          action_logger.debug("Going to sleep for #{sleep_seconds} seconds")
          plan_event(Wake, sleep_seconds)
          suspend
        when Wake
          action_logger.debug('Waking up')
        else
          action_logger.debug("DelayAction received unknown event #{event}")
        end
      end

      def sleep_seconds
        input[:delay].to_i
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
foreman_rh_cloud-8.0.49 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-8.0.48 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-8.0.47 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-8.0.46 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-6.0.45 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-7.0.46 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-7.0.45 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-6.0.44 lib/foreman_inventory_upload/async/delayed_start.rb