-
Notifications
You must be signed in to change notification settings - Fork 0
Add change-detection and trigger registry #8
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| module R3x | ||
| class ChangeDetectionJob < ApplicationJob | ||
| queue_as :default | ||
|
|
||
| def perform(workflow_key, options = nil) | ||
| workflow_key, options = normalize_arguments(workflow_key, options) | ||
| trigger_key = options.fetch(:trigger_key) | ||
|
|
||
| R3x::WorkflowPackLoader.load! | ||
| workflow_class = R3x::WorkflowRegistry.fetch(workflow_key) | ||
| trigger = find_trigger(workflow_class: workflow_class, trigger_key: trigger_key) | ||
| trigger_state = load_trigger_state(workflow_key: workflow_key, trigger_key: trigger_key, trigger_type: trigger.type) | ||
| result = normalize_result( | ||
| trigger.detect_changes( | ||
| workflow_key: workflow_key, | ||
| state: trigger_state.state.deep_symbolize_keys | ||
| ) | ||
| ) | ||
|
|
||
| TriggerState.transaction do | ||
| if result[:changed] | ||
| R3x::RunWorkflowJob.perform_later( | ||
| workflow_key, | ||
| trigger_key: trigger_key, | ||
| trigger_payload: result[:payload] | ||
| ) | ||
| end | ||
|
|
||
| trigger_state.record_check!(result) | ||
| end | ||
| rescue StandardError => e | ||
| trigger_state.record_error!(e) if defined?(trigger_state) && trigger_state&.persisted? | ||
| raise | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def find_trigger(workflow_class:, trigger_key:) | ||
| trigger = workflow_class.triggers_by_key[trigger_key] | ||
|
|
||
| if trigger.nil? | ||
| raise ArgumentError, "Unknown trigger key '#{trigger_key}' for workflow '#{workflow_class.workflow_key}'" | ||
| end | ||
|
|
||
| unless trigger.change_detecting? | ||
| raise ArgumentError, "Trigger '#{trigger_key}' is not change-detecting" | ||
| end | ||
|
|
||
| trigger | ||
| end | ||
|
|
||
| def load_trigger_state(workflow_key:, trigger_key:, trigger_type:) | ||
| TriggerState.find_or_create_by!( | ||
| workflow_key: workflow_key, | ||
| trigger_key: trigger_key | ||
| ) do |state| | ||
| state.trigger_type = trigger_type.to_s | ||
| state.state = {} | ||
| end | ||
| end | ||
|
|
||
| def normalize_arguments(workflow_key, options) | ||
| if workflow_key.is_a?(Hash) && options.nil? | ||
| options = workflow_key | ||
| workflow_key = nil | ||
| end | ||
|
|
||
| options = normalize_options_hash(options) | ||
| workflow_key ||= options[:workflow_key] | ||
|
|
||
| [ workflow_key.presence || raise(ArgumentError, "Missing workflow_key"), options ] | ||
| end | ||
|
|
||
| def normalize_options_hash(options) | ||
| case options | ||
| when nil | ||
| {} | ||
| when Hash | ||
| options.deep_symbolize_keys | ||
| else | ||
| raise ArgumentError, "Expected options hash, got #{options.class.name}" | ||
| end | ||
| end | ||
|
|
||
| def normalize_result(result) | ||
| normalized = result.deep_symbolize_keys | ||
|
|
||
| unless normalized.key?(:changed) && normalized.key?(:state) | ||
| raise ArgumentError, "Change-detecting trigger must return a hash with :changed and :state" | ||
| end | ||
|
|
||
| normalized[:state] ||= {} | ||
| normalized[:payload] ||= nil | ||
| normalized | ||
| 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
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 @@ | ||
| module R3x | ||
| class TriggerState < ApplicationRecord | ||
| serialize :state, coder: MultiJson if ActiveRecord::Base.connection_db_config.adapter.to_s.downcase == "sqlite" | ||
|
|
||
| validates :workflow_key, presence: true | ||
| validates :trigger_type, presence: true | ||
| validates :trigger_key, presence: true, uniqueness: { scope: :workflow_key } | ||
|
|
||
| def record_check!(result) | ||
| update!( | ||
| state: result.fetch(:state), | ||
| last_checked_at: Time.current, | ||
| last_error_at: nil, | ||
| last_error_message: nil, | ||
| last_triggered_at: result[:changed] ? Time.current : last_triggered_at | ||
| ) | ||
| end | ||
|
|
||
| def record_error!(error) | ||
| update!( | ||
| last_error_at: Time.current, | ||
| last_error_message: error.message | ||
| ) | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| class CreateTriggerStates < ActiveRecord::Migration[8.1] | ||
| def change | ||
| create_table :trigger_states do |t| | ||
| t.string :workflow_key, null: false | ||
| t.string :trigger_key, null: false | ||
| t.string :trigger_type, null: false | ||
| t.json :state, null: false, default: {} | ||
| t.datetime :last_checked_at | ||
| t.datetime :last_triggered_at | ||
| t.datetime :last_error_at | ||
| t.text :last_error_message | ||
|
|
||
| t.timestamps | ||
| end | ||
|
|
||
| add_index :trigger_states, [ :workflow_key, :trigger_key ], unique: true | ||
| add_index :trigger_states, :workflow_key | ||
| add_index :trigger_states, :trigger_type | ||
| 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
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.
Uh oh!
There was an error while loading. Please reload this page.