-
Notifications
You must be signed in to change notification settings - Fork 15
Priority annotations #274
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
Priority annotations #274
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'temporalio/api' | ||
|
||
module Temporalio | ||
Priority = Data.define( | ||
:priority_key | ||
) | ||
|
||
# Priority contains metadata that controls relative ordering of task processing when tasks are | ||
# backlogged in a queue. Initially, Priority will be used in activity and workflow task | ||
# queues, which are typically where backlogs exist. Priority is (for now) attached to | ||
# workflows and activities. Activities and child workflows inherit Priority from the workflow | ||
# that created them, but may override fields when they are started or modified. For each field | ||
# of a Priority on an activity/workflow, not present or equal to zero/empty string means to | ||
# inherit the value from the calling workflow, or if there is no calling workflow, then use | ||
# the default (documented on the field). | ||
# | ||
# The overall semantics of Priority are: | ||
# 1. First, consider "priority_key": lower number goes first. | ||
# (more will be added here later). | ||
# | ||
# @!attribute priority_key | ||
# @return [Integer, nil] The priority key, which is a positive integer from 1 to n, where | ||
# smaller integers correspond to higher priorities (tasks run sooner). In general, tasks in a | ||
# queue should be processed in close to priority order, although small deviations are possible. | ||
# The maximum priority value (minimum priority) is determined by server configuration, and | ||
# defaults to 5. | ||
# | ||
# The default priority is (min+max)/2. With the default max of 5 and min of 1, that comes | ||
# out to 3. | ||
class Priority | ||
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. I'm wondering if this should be a |
||
# @!visibility private | ||
def self._from_proto(priority) | ||
return default if priority.nil? | ||
|
||
new(priority_key: priority.priority_key.zero? ? nil : priority.priority_key) | ||
end | ||
|
||
# The default priority instance. | ||
# | ||
# @return [Priority] The default priority | ||
def self.default | ||
@default ||= new(priority_key: nil) | ||
end | ||
|
||
# @!visibility private | ||
def _to_proto | ||
return nil if priority_key.nil? | ||
|
||
Temporalio::Api::Common::V1::Priority.new(priority_key: priority_key || 0) | ||
end | ||
|
||
# @return [Boolean] True if this priority is empty/default | ||
def empty? | ||
priority_key.nil? | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
require 'random/formatter' | ||
require 'temporalio/error' | ||
require 'temporalio/priority' | ||
require 'temporalio/workflow/activity_cancellation_type' | ||
require 'temporalio/workflow/child_workflow_cancellation_type' | ||
require 'temporalio/workflow/child_workflow_handle' | ||
|
@@ -130,6 +131,7 @@ def self.deprecate_patch(patch_id) | |
# optimization on some servers that sends activities back to the same worker as the calling workflow if they can | ||
# run there. If `false` (the default), eager execution may still be disabled at the worker level or may not be | ||
# requested due to lack of available slots. | ||
# @param priority [Priority] Priority of the activity. This is currently experimental. | ||
# | ||
# @return [Object] Result of the activity. | ||
# @raise [Error::ActivityError] Activity failed (and retry was disabled or exhausted). | ||
|
@@ -148,12 +150,14 @@ def self.execute_activity( | |
cancellation: Workflow.cancellation, | ||
cancellation_type: ActivityCancellationType::TRY_CANCEL, | ||
activity_id: nil, | ||
disable_eager_execution: false | ||
disable_eager_execution: false, | ||
priority: Priority.default | ||
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. Can we add the import to this file and to |
||
) | ||
_current.execute_activity( | ||
activity, *args, | ||
task_queue:, summary:, schedule_to_close_timeout:, schedule_to_start_timeout:, start_to_close_timeout:, | ||
heartbeat_timeout:, retry_policy:, cancellation:, cancellation_type:, activity_id:, disable_eager_execution: | ||
heartbeat_timeout:, retry_policy:, cancellation:, cancellation_type:, activity_id:, disable_eager_execution:, | ||
priority: | ||
) | ||
end | ||
|
||
|
@@ -175,13 +179,14 @@ def self.execute_child_workflow( | |
retry_policy: nil, | ||
cron_schedule: nil, | ||
memo: nil, | ||
search_attributes: nil | ||
search_attributes: nil, | ||
priority: Priority.default | ||
) | ||
start_child_workflow( | ||
workflow, *args, | ||
id:, task_queue:, static_summary:, static_details:, cancellation:, cancellation_type:, | ||
parent_close_policy:, execution_timeout:, run_timeout:, task_timeout:, id_reuse_policy:, | ||
retry_policy:, cron_schedule:, memo:, search_attributes: | ||
retry_policy:, cron_schedule:, memo:, search_attributes:, priority: | ||
).result | ||
end | ||
|
||
|
@@ -372,6 +377,7 @@ def self.sleep(duration, summary: nil, cancellation: Workflow.cancellation) | |
# @param cron_schedule [String, nil] Cron schedule. Users should use schedules instead of this. | ||
# @param memo [Hash{String, Symbol => Object}, nil] Memo for the workflow. | ||
# @param search_attributes [SearchAttributes, nil] Search attributes for the workflow. | ||
# @param priority [Priority] Priority of the workflow. This is currently experimental. | ||
# | ||
# @return [ChildWorkflowHandle] Workflow handle to the started workflow. | ||
# @raise [Error::WorkflowAlreadyStartedError] Workflow already exists for the ID. | ||
|
@@ -393,13 +399,14 @@ def self.start_child_workflow( | |
retry_policy: nil, | ||
cron_schedule: nil, | ||
memo: nil, | ||
search_attributes: nil | ||
search_attributes: nil, | ||
priority: Priority.default | ||
) | ||
_current.start_child_workflow( | ||
workflow, *args, | ||
id:, task_queue:, static_summary:, static_details:, cancellation:, cancellation_type:, | ||
parent_close_policy:, execution_timeout:, run_timeout:, task_timeout:, id_reuse_policy:, | ||
retry_policy:, cron_schedule:, memo:, search_attributes: | ||
retry_policy:, cron_schedule:, memo:, search_attributes:, priority: | ||
) | ||
end | ||
|
||
|
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.
If you want to default a field for this you can still leave the
initialize
the way you had it in the class and just call super, e.g.But requiring it is fine too.
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.
Yeah, honestly requiring it makes more sense