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-11.0.3 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-9.0.59 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-11.0.2 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-11.0.1 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-11.0.0 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-9.0.58 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-10.0.2 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-9.0.57 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-10.0.1 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-9.0.56 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-9.0.55 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-9.0.54 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-9.0.53 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-9.0.52 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-8.0.52 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-9.0.51 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-8.0.51 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-7.0.48 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-7.0.47 lib/foreman_inventory_upload/async/delayed_start.rb
foreman_rh_cloud-8.0.50 lib/foreman_inventory_upload/async/delayed_start.rb