-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infer the interruption handler for a given integraion from the job it…
…self Instead of automatically attempting to load the interruption adapter implicitly this patch changes this to be discovered for a given worker based on the job itself based on its class adapter. I believe this will work OK, and should allow a bit more flexibility around using JobIteration. This patch also contains a bunch of renames, broadly from `job-iteration` -> `job_iteration`. This might be a bit contentious, but the current module naming breaks autoloading a bit, which is not ideal, and I’d like to leverage it for loading integrations.
- Loading branch information
1 parent
83e708e
commit 47adc55
Showing
23 changed files
with
149 additions
and
127 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
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