-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Infer interruption handler from job #96
Closed
plasticine
wants to merge
1
commit into
Shopify:main
from
plasticine:plasticine/infer-interruption-handler
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "./job_iteration/version" | ||
require_relative "./job_iteration/enumerator_builder" | ||
require_relative "./job_iteration/iteration" | ||
require_relative "./job_iteration/integrations" | ||
|
||
module JobIteration | ||
extend self | ||
|
||
# Use this to _always_ interrupt the job after it's been running for more than N seconds. | ||
# @example | ||
# | ||
# JobIteration.max_job_runtime = 5.minutes | ||
# | ||
# This setting will make it to always interrupt a job after it's been iterating for 5 minutes. | ||
# Defaults to nil which means that jobs will not be interrupted except on termination signal. | ||
attr_accessor :max_job_runtime | ||
|
||
# Set if you want to use your own enumerator builder instead of default EnumeratorBuilder. | ||
# @example | ||
# | ||
# class MyOwnBuilder < JobIteration::EnumeratorBuilder | ||
# # ... | ||
# end | ||
# | ||
# JobIteration.enumerator_builder = MyOwnBuilder | ||
attr_accessor :enumerator_builder | ||
self.enumerator_builder = JobIteration::EnumeratorBuilder | ||
|
||
# Used internally for hooking into job processing frameworks like Sidekiq and Resque. | ||
def self.load_interruption_integration(integration) | ||
JobIteration::Integrations.load(integration) | ||
end | ||
end |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module JobIteration | ||
module Integrations | ||
IntegrationLoadError = Class.new(StandardError) | ||
|
||
extend ActiveSupport::Autoload | ||
|
||
autoload :SidekiqIntegration | ||
autoload :ResqueIntegration | ||
|
||
class << self | ||
def load(integration) | ||
integration = const_get(integration.to_s.camelize << "Integration") | ||
integration.new | ||
rescue NameError | ||
raise IntegrationLoadError, "#{integration} integration is not supported." | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require "resque" | ||
|
||
module JobIteration | ||
module Integrations | ||
class ResqueIntegration | ||
module ResqueIterationExtension # @private | ||
def initialize(*) # @private | ||
$resque_worker = self | ||
super | ||
end | ||
end | ||
|
||
# The patch is required in order to call shutdown? on a Resque::Worker instance | ||
Resque::Worker.prepend(ResqueIterationExtension) | ||
|
||
def stopping? | ||
$resque_worker.try!(:shutdown?) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require "sidekiq" | ||
|
||
module JobIteration | ||
module Integrations | ||
class SidekiqIntegration | ||
def stopping? | ||
if defined?(Sidekiq::CLI) && Sidekiq::CLI.instance | ||
Sidekiq::CLI.instance.launcher.stopping? | ||
else | ||
false | ||
end | ||
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
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
module JobIteration | ||
module Integrations | ||
# https://api.rubyonrails.org/classes/ActiveJob/QueueAdapters/AsyncAdapter.html | ||
module AsyncIntegration | ||
class << self | ||
def interruption_adapter | ||
false | ||
end | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what it's worth, this could just use Ruby's built-in autoloading, which wouldn't require the rename.
From there, a separate PR could propose the rename and use of
ActiveSupport::Autoload
, though I'm not sure there is much benefit, since the autoloading is so limited.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Argh — I didn’t even consider this, thanks