Liking cljdoc? Tell your friends :D

Temporal Client

Overview

To initialize a Workflow Client, create an instance of a Workflow client with temporal.client.core/create-client, create a Workflow stub with temporal.client.core/create-workflow, and invoke the Workflow with temporal.client.core/start). Finally, gather the results of the Workflow with temporal.client.core/get-result.

To initialize a Schedule Client, create an instance of a Schedule client with temporal.client.schedule/create-client, create a Schedule with temporal.client.schedule/schedule, and run a scheduled workflow execution immediately with temporal.client.schedule/execute).

Details

Using the namespace temporal.client.core or temporal.client.schedule

To start a Workflow Execution or manage a Scheduled Workflow Execution, your Temporal Server must be running, and your front-end service must be accepting gRPC calls.

You can provide options to (create-client) to establish a connection to the specifics of the Temporal front-end service in your environment.

(create-client {:target "temporal-frontend:7233" :namespace "my-namespace"})

Workflow Executions

After establishing a successful connection to the Temporal Frontend Service, you may perform Workflow Execution specific operations such as:

As a simple example, for the following Workflow implementation:

(require '[temporal.workflow :refer [defworkflow]])
(require '[temporal.activity :refer [defactivity] :as a])

(defactivity greet-activity
  [ctx {:keys [name] :as args}]
  (log/info "greet-activity:" args)
  (str "Hi, " name))

(defworkflow greeter-workflow
  [args]
  (log/info "greeter-workflow:" args)
  @(a/invoke greet-activity args))

We can create a client to invoke our Workflow as follows:

(require '[temporal.client.core :as c])

(def task-queue "MyTaskQueue")

(let [client (c/create-client)
      workflow (c/create-workflow client greeter-workflow {:task-queue task-queue})]
  (c/start workflow {:name "Bob"})
  (println @(c/get-result workflow)))

Evaluating this code should result in Hi, Bob appearing on the console. Note that (get-result) returns a promise, thus requiring a dereference.

Scheduled Workflow Executions

After establishing a successful connection to the Temporal Frontend Service, you may perform Schedule specific operations such as:

Schedules can be built with cron expressions or built in Temporal time types.

Schedules can executed immediately via "triggering" them.

Schedules will handle overlapping runs determined by the SchedulePolicy you give it when creating it.

Example Usage

As a simple example, for the following Workflow implementation:

(require '[temporal.workflow :refer [defworkflow]])
(require '[temporal.activity :refer [defactivity] :as a])

(defactivity greet-activity
  [ctx {:keys [name] :as args}]
  (log/info "greet-activity:" args)
  (str "Hi, " name))

(defworkflow greeter-workflow
  [args]
  (log/info "greeter-workflow:" args)
  @(a/invoke greet-activity args))

Create and manage a schedule as follows

(require '[temporal.client.schedule :as s])
(let [client-options {:target "localhost:7233"
                      :namespace "default"
                      :enable-https false}
      task-queue "MyTaskQueue"
      workflow-id "my-workflow"
      schedule-id "my-schedule"
      client (s/create-client client-options)]
  (s/schedule client schedule-id {:schedule {:trigger-immediately? true
                                             :memo {"Created by" "John Doe"}}
                                  :spec {:cron-expressions ["0 0 * * *"]
                                         :timezone "US/Central"}
                                  :policy {:pause-on-failure? false
                                           :catchup-window (Duration/ofSeconds 10)
                                         :overlap :skip}
                                  :action {:arguments {:name "John"}
                                           :options {:workflow-id workflow-id
                                                     :task-queue task-queue}
                                           :workflow-type greeter-workflow}})
  (s/describe client schedule-id)
  (s/pause client schedule-id)
  (s/unpause client schedule-id)
  (s/execute client schedule-id :skip)
  (s/reschedule client schedule-id {:spec {:cron-expressions ["0 1 * * *"]}})
  (s/unschedule client schedule-id))

Can you improve this documentation? These fine people already did:
Greg Haskins, Felipe Cortez & Bailey Kocin
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close