-
Notifications
You must be signed in to change notification settings - Fork 73
Autonomic Updates #3029
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
Autonomic Updates #3029
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,10 +32,12 @@ class ReactionModel < Model | |
| ACTION_TYPES = [SCRIPT_REACTION, COMMAND_REACTION, NOTIFY_REACTION] | ||
|
|
||
| def self.create_unique_name(scope:) | ||
| reaction_names = self.names(scope: scope) # comes back sorted | ||
| reaction_names = self.names(scope: scope) | ||
| num = 1 # Users count with 1 | ||
| if reaction_names[-1] | ||
| num = reaction_names[-1][5..-1].to_i + 1 | ||
| unless reaction_names.empty? | ||
| # Extract numeric suffixes and find the max to avoid lexicographic sort issues | ||
| max_num = reaction_names.map { |name| name[5..-1].to_i }.max | ||
| num = max_num + 1 | ||
| end | ||
| return "REACT#{num}" | ||
| end | ||
|
|
@@ -77,7 +79,7 @@ def self.delete(name:, scope:) | |
| end | ||
|
|
||
| attr_reader :name, :scope, :snooze, :triggers, :actions, :enabled, :trigger_level, :snoozed_until | ||
| attr_accessor :username, :shard | ||
| attr_accessor :username, :shard, :label | ||
|
|
||
| def initialize( | ||
| name:, | ||
|
|
@@ -90,6 +92,7 @@ def initialize( | |
| snoozed_until: nil, | ||
| username: nil, | ||
| shard: 0, | ||
| label: nil, | ||
| updated_at: nil | ||
| ) | ||
| super("#{scope}#{PRIMARY_KEY}", name: name, scope: scope) | ||
|
|
@@ -102,6 +105,7 @@ def initialize( | |
| @triggers = validate_triggers(triggers) | ||
| @username = username | ||
| @shard = shard.to_i # to_i to handle nil | ||
| @label = label | ||
| @updated_at = updated_at | ||
| end | ||
|
|
||
|
|
@@ -177,28 +181,40 @@ def validate_actions(actions) | |
| return actions | ||
| end | ||
|
|
||
| def verify_triggers | ||
| # Validate that all triggers exist, but do not persist dependent changes yet. | ||
| # Returns the list of trigger models that need updating. | ||
| def validate_triggers_exist | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The change below is to ensure that the dependencies are not updated until after the trigger / reaction is created. This prevents an edge case where dependents are defined but the dependency doesn't exist. |
||
| if @triggers.empty? | ||
| raise ReactionInputError.new "reaction must contain at least one valid trigger: #{@triggers}" | ||
| end | ||
|
|
||
| models_to_update = [] | ||
| @triggers.each do | trigger | | ||
| model = TriggerModel.get(name: trigger['name'], group: trigger['group'], scope: @scope) | ||
| if model.nil? | ||
| raise ReactionInputError.new "failed to find trigger: #{trigger}" | ||
| end | ||
| model.update_dependents(dependent: @name) | ||
| model.update() | ||
| unless model.dependents.include?(@name) | ||
| model.update_dependents(dependent: @name) | ||
| models_to_update << model | ||
| end | ||
| end | ||
| models_to_update | ||
| end | ||
|
|
||
| # Persist dependent changes to trigger models | ||
| def commit_trigger_dependents(models) | ||
| models.each { |model| model.update() } | ||
| end | ||
|
|
||
| def create | ||
| unless Store.hget(@primary_key, @name).nil? | ||
| raise ReactionInputError.new "existing reaction found: #{@name}" | ||
| end | ||
| verify_triggers() | ||
| models = validate_triggers_exist() | ||
| @updated_at = Time.now.to_nsec_from_epoch | ||
| Store.hset(@primary_key, @name, JSON.generate(as_json, allow_nan: true)) | ||
| commit_trigger_dependents(models) | ||
| notify(kind: 'created') | ||
| end | ||
|
|
||
|
|
@@ -222,9 +238,10 @@ def update | |
| end | ||
| end | ||
|
|
||
| verify_triggers() | ||
| models = validate_triggers_exist() | ||
| @updated_at = Time.now.to_nsec_from_epoch | ||
| Store.hset(@primary_key, @name, JSON.generate(as_json, allow_nan: true)) | ||
| commit_trigger_dependents(models) | ||
| # No notification as this is only called via reaction_controller which already notifies | ||
| end | ||
|
|
||
|
|
@@ -281,6 +298,7 @@ def as_json(*a) | |
| 'actions' => @actions, | ||
| 'username' => @username, | ||
| 'shard' => @shard, | ||
| 'label' => @label, | ||
| 'updated_at' => @updated_at | ||
| } | ||
| 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.
Do we need the
namestuff anymore now that we're using thelabelfieldThere 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.
namewill remain as the primary key!