Skip to content
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

Fix default retry behavior for local activities #67

Merged
merged 1 commit into from
Oct 7, 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
11 changes: 8 additions & 3 deletions src/temporal/internal/activity.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,21 @@
^ActivityOptions [params]
(u/build (ActivityOptions/newBuilder) invoke-option-spec (import-invoke-options params)))

(defn- local-retry-options-> [{:keys [maximum-attempts] :or {maximum-attempts 0} :as options}]
(-> options
(cond-> (zero? maximum-attempts) (assoc :maximum-attempts Integer/MAX_VALUE)) ;; workaround for https://github.com/temporalio/sdk-java/issues/1727
(common/retry-options->)))

(def local-invoke-option-spec
{:start-to-close-timeout #(.setStartToCloseTimeout ^LocalActivityOptions$Builder %1 %2)
:schedule-to-close-timeout #(.setScheduleToCloseTimeout ^LocalActivityOptions$Builder %1 %2)
:retry-options #(.setRetryOptions ^LocalActivityOptions$Builder %1 (common/retry-options-> %2))
:retry-options #(.setRetryOptions ^LocalActivityOptions$Builder %1 (local-retry-options-> %2))
:do-not-include-args #(.setDoNotIncludeArgumentsIntoMarker ^LocalActivityOptions$Builder %1 %2)
:local-retry-threshold #(.setLocalRetryThreshold ^LocalActivityOptions$Builder %1 %2)})

(defn local-invoke-options->
^LocalActivityOptions [params]
(u/build (LocalActivityOptions/newBuilder (LocalActivityOptions/getDefaultInstance)) local-invoke-option-spec (import-invoke-options params)))
^LocalActivityOptions [{:keys [retry-options] :or {retry-options {}} :as params}]
(u/build (LocalActivityOptions/newBuilder (LocalActivityOptions/getDefaultInstance)) local-invoke-option-spec (import-invoke-options (assoc params :retry-options retry-options))))

(extend-protocol p/Datafiable
ActivityInfo
Expand Down
47 changes: 47 additions & 0 deletions test/temporal/test/local_retry.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
;; Copyright © Manetu, Inc. All rights reserved

(ns temporal.test.local-retry
(:require [clojure.test :refer :all]
[promesa.core :as p]
[taoensso.timbre :as log]
[temporal.client.core :as c]
[temporal.workflow :refer [defworkflow]]
[temporal.activity :refer [defactivity] :as a]
[temporal.test.utils :as t])
(:import (io.temporal.client WorkflowFailedException)
[io.temporal.failure TimeoutFailure ActivityFailure]
[java.time Duration]))

(use-fixtures :once t/wrap-service)

(defactivity local-retry-activity
[ctx args]
(log/info "local-retry-activity")
(Thread/sleep 100000000))

(defworkflow local-retry-workflow
[args]
(log/info "local-retry-workflow:" args)
@(-> (a/local-invoke local-retry-activity {} (merge args {:do-not-include-args true
:start-to-close-timeout (Duration/ofMillis 500)}))
(p/catch ActivityFailure
:fail)))

(defn exec [args]
(let [workflow (t/create-workflow local-retry-workflow)]
(c/start workflow args)
@(-> (c/get-result workflow)
(p/then (constantly :fail))
(p/catch WorkflowFailedException
(fn [ex]
(if (instance? TimeoutFailure (ex-cause ex))
:pass
:fail))))))

(deftest the-test
(testing "RetryPolicy defaults"
(is (= :pass (exec {}))))
(testing "Explicit unlimited"
(is (= :pass (exec {:retry-options {:maximum-attempts 0}}))))
(testing "Verify that setting maximum-attempts to a finite value is respected"
(is (= :fail (exec {:retry-options {:maximum-attempts 1}})))))