Skip to content

Add lifecycle hooks for Executor #7

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

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ Naming/FileName:

Style/BlockComments:
Exclude:
- 'spec/spec_helper.rb'
- 'spec/spec_helper.rb'
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Feature - Add lifecycle hooks for Executor.

0.1.0 (2024-11-16)
------------------

Expand Down
4 changes: 4 additions & 0 deletions lib/aws-activejob-sqs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def self.configure
def self.fifo?(queue_url)
queue_url.end_with?('.fifo')
end

def self.on_worker_stop(...)
Executor.on_stop(...)
end
end
end
end
21 changes: 21 additions & 0 deletions lib/aws/active_job/sqs/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ class Executor
fallback_policy: :abort # Concurrent::RejectedExecutionError must be handled
}.freeze

class << self
def on_stop(&block)
lifecycle_hooks[:stop] << block
end

def lifecycle_hooks
@lifecycle_hooks ||= Hash.new { |h, k| h[k] = [] }
end

def clear_hooks
@lifecycle_hooks = nil
end
end

def initialize(options = {})
@executor = Concurrent::ThreadPoolExecutor.new(DEFAULTS.merge(options))
@retry_standard_errors = options[:retry_standard_errors]
Expand All @@ -32,6 +46,7 @@ def execute(message)
end

def shutdown(timeout = nil)
run_hooks_for(:stop)
@executor.shutdown
clean_shutdown = @executor.wait_for_termination(timeout)
if clean_shutdown
Expand Down Expand Up @@ -71,6 +86,12 @@ def post_task(message)
@task_complete.set
end
end

def run_hooks_for(event_name)
return unless (hooks = self.class.lifecycle_hooks[event_name])

hooks.each(&:call)
end
end
end
end
Expand Down
23 changes: 23 additions & 0 deletions spec/aws/active_job/sqs/executor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,29 @@ module SQS
expect(tp).to receive(:wait_for_termination).with(5).and_return true
executor.shutdown(5)
end

describe 'when lifecycle hooks are registered' do
let(:hook) { double }

before do
allow(hook).to receive(:call)
end

after do
Executor.clear_hooks
end

it 'executs hook when shutdown' do
Aws::ActiveJob::SQS.on_worker_stop do
hook.call
end
executor = Executor.new

executor.shutdown

expect(hook).to have_received(:call)
end
end
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ class TestJobWithDedupKeys < TestJob
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
Expand Down Expand Up @@ -115,12 +121,6 @@ class TestJobWithDedupKeys < TestJob
# particularly slow.
config.profile_examples = 10

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random

# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
Expand Down