Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Schedule a random delay before execution
- Loading branch information
1 parent
332c68e
commit 8c38ff7
Showing
5 changed files
with
76 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters