-
Notifications
You must be signed in to change notification settings - Fork 1
Adds redis queueing of next steps. #189
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
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,58 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Service for add workflow steps to Resqueue queues | ||
| class QueueService | ||
| # Enqueue the provided step | ||
| # @param [WorkflowStep] workflow step to enqueue | ||
| def self.enqueue(step) | ||
| QueueService.new(step).enqueue | ||
| end | ||
|
|
||
| attr_reader :step | ||
|
|
||
| # @param [WorkflowStep] workflow step to enqueue | ||
| def initialize(step) | ||
| @step = step | ||
| end | ||
|
|
||
| # Enqueue the provided step | ||
| def enqueue | ||
| # Perform the enqueue to Resque | ||
| Resque.enqueue_to(queue_name.to_sym, class_name, step.druid) | ||
| Rails.logger.info "Enqueued #{class_name} for #{step.druid} to #{queue_name}" | ||
|
|
||
| # Update status | ||
| step.status = 'queued' | ||
| step.save | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # @example | ||
| # => dor:assemblyWF:jp2-create | ||
| def step_name | ||
| @step_name ||= "#{step.repository}:#{step.workflow}:#{step.process}" | ||
| end | ||
|
|
||
| # Generate the queue name from step | ||
| # | ||
| # @example | ||
| # => 'dor_assemblyWF_jp2-create_default' | ||
| # => 'dor_assemblyWF_jp2-create_mylane' | ||
| def queue_name | ||
| @queue_name ||= "#{step.repository}_#{step.workflow}_#{step.process}_#{step.lane_id}" | ||
| end | ||
|
|
||
| # Converts a given step to the Robot class name | ||
| # Based on https://github.com/sul-dlss/lyber-core/blob/master/lib/lyber_core/robot.rb#L33 | ||
| # @example | ||
| # => 'Robots::DorRepo::Assembly::Jp2Create' | ||
| def class_name | ||
| @class_name ||= begin | ||
| repo = step.repository.camelize | ||
| workflow = step.workflow.sub('WF', '').camelize | ||
| process = step.process.tr('-', '_').camelize | ||
| "Robots::#{repo}Repo::#{workflow}::#{process}" | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Load Resque configuration and controller | ||
| redis = Redis.new(host: Settings.redis.hostname, | ||
| port: Settings.redis.port, | ||
| thread_safe: true, | ||
| db: Settings.redis.db) | ||
|
|
||
| Resque.redis = Redis::Namespace.new(Settings.redis.namespace, redis: redis) |
This file contains hidden or 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 hidden or 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 hidden or 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,26 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| namespace :workflow do | ||
| desc 'Update a workflow step' | ||
| task :step, %i[repo druid workflow process version status] => :environment do |_task, args| | ||
| step = WorkflowStep.find_by( | ||
| repository: args[:repo], | ||
| druid: args[:druid], | ||
| workflow: args[:workflow], | ||
| process: args[:process], | ||
| version: args[:version] | ||
| ) | ||
|
|
||
| raise 'Workflow step does not already exist' if step.nil? | ||
|
|
||
| step.status = args[:status] | ||
| step.save | ||
| puts("Setting #{args[:process]} to #{args[:status]}") | ||
|
|
||
| # Enqueue next steps | ||
| next_steps = NextStepService.for(step: step) | ||
| next_steps.each { |next_step| QueueService.enqueue(next_step) } | ||
|
|
||
| SendUpdateMessage.publish(druid: step.druid) | ||
| end | ||
| end |
This file contains hidden or 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,63 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe StepsController do | ||
| let(:repository) { 'dor' } | ||
| let(:client) { instance_double(Dor::Services::Client::Object, current_version: '1') } | ||
| let(:druid) { first_step.druid } | ||
| let(:workflow_id) { 'accessionWF' } | ||
| let(:first_step) { FactoryBot.create(:workflow_step, status: 'completed') } # start-accession, which is already completed | ||
|
|
||
| before do | ||
| FactoryBot.create(:workflow_step, druid: druid, process: 'descriptive-metadata') | ||
| FactoryBot.create(:workflow_step, druid: druid, process: 'rights-metadata') | ||
| allow(Dor::Services::Client).to receive(:object).with(druid).and_return(client) | ||
| allow(SendUpdateMessage).to receive(:publish) | ||
| allow(QueueService).to receive(:enqueue) | ||
| end | ||
|
|
||
| describe 'PUT update' do | ||
| context 'when updating a step' do | ||
| let(:body) { '<process name="descriptive-metadata" status="test" />' } | ||
|
|
||
| it 'updates the step' do | ||
| put :update, body: body, params: { repo: repository, druid: druid, workflow: workflow_id, | ||
| process: 'descriptive-metadata', format: :xml } | ||
| expect(response.body).to eq('{"next_steps":[]}') | ||
| expect(SendUpdateMessage).to have_received(:publish).with(druid: druid) | ||
| expect(WorkflowStep.find_by(druid: druid, process: 'descriptive-metadata').status).to eq('test') | ||
| expect(QueueService).to_not have_received(:enqueue) | ||
| end | ||
|
|
||
| it 'verifies the current status' do | ||
| put :update, body: body, params: { repo: repository, druid: druid, workflow: workflow_id, | ||
| process: 'descriptive-metadata', 'current-status': 'not-waiting', | ||
| format: :xml } | ||
| expect(response.body).to eq('Status in params (not-waiting) does not match current status (waiting)') | ||
| expect(response.code).to eq('409') | ||
| end | ||
|
|
||
| it 'verifies that process in url and body match' do | ||
| put :update, body: body, params: { repo: repository, druid: druid, workflow: workflow_id, | ||
| process: 'rights-metadata', format: :xml } | ||
| expect(response.body).to eq('Process name in body (descriptive-metadata) does not match process name in URI ' \ | ||
| '(rights-metadata)') | ||
| expect(response.code).to eq('400') | ||
| end | ||
| end | ||
|
|
||
| context 'when completing a step' do | ||
| let(:body) { '<process name="descriptive-metadata" status="completed" />' } | ||
| it 'updates the step and enqueues next step' do | ||
| put :update, body: body, params: { repo: repository, druid: druid, workflow: workflow_id, | ||
| process: 'descriptive-metadata', format: :xml } | ||
| expect(response.body).to match(/rights-metadata/) | ||
| expect(SendUpdateMessage).to have_received(:publish).with(druid: druid) | ||
| expect(WorkflowStep.find_by(druid: druid, process: 'descriptive-metadata').status).to eq('completed') | ||
| expect(QueueService).to have_received(:enqueue).with(WorkflowStep.find_by(druid: druid, | ||
| process: 'rights-metadata')) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -10,5 +10,6 @@ | |
| repository { 'dor' } | ||
| version { 1 } | ||
| lane_id { 'default' } | ||
| status { 'waiting' } | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
+1
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.
I'd like us to move to this pattern in production too.