• Stars
    star
    218
  • Rank 181,805 (Top 4 %)
  • Language
    Ruby
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ruby SDK for Temporal

Ruby SDK for Temporal

Coverage Status

Temporal

A pure Ruby library for defining and running Temporal workflows and activities.

To find more about Temporal itself please visit https://temporal.io/.

Getting Started

Clone this repository:

git clone [email protected]:coinbase/temporal-ruby.git

Include this gem to your Gemfile:

gem 'temporal-ruby', github: 'coinbase/temporal-ruby'

Define an activity:

require 'temporal-ruby'
class HelloActivity < Temporal::Activity
  def execute(name)
    puts "Hello #{name}!"

    return nil
  end
end

Define a workflow:

require 'path/to/hello_activity'

class HelloWorldWorkflow < Temporal::Workflow
  def execute
    HelloActivity.execute!('World')

    return nil
  end
end

Configure your Temporal connection and register the namespace with the Temporal service:

require 'temporal-ruby'
Temporal.configure do |config|
  config.host = 'localhost'
  config.port = 7233
  config.namespace = 'ruby-samples'
  config.task_queue = 'hello-world'
  config.credentials = :this_channel_is_insecure
end

begin
  Temporal.register_namespace('ruby-samples', 'A safe space for playing with Temporal Ruby')
rescue Temporal::NamespaceAlreadyExistsFailure
  nil # service was already registered
end

Configure and start your worker process in a terminal shell:

require 'path/to/configuration'
require 'temporal/worker'

worker = Temporal::Worker.new
worker.register_workflow(HelloWorldWorkflow)
worker.register_activity(HelloActivity)
worker.start # runs forever

You can add several options when initializing worker (here defaults are provided as values):

Temporal::Worker.new(
  activity_thread_pool_size: 20, # how many threads poll for activities
  workflow_thread_pool_size: 10, # how many threads poll for workflows
  binary_checksum: nil, # identifies the version of workflow worker code
  activity_poll_retry_seconds: 0, # how many seconds to wait after unsuccessful poll for activities
  workflow_poll_retry_seconds: 0  # how many seconds to wait after unsuccessful poll for workflows
)

And finally start your workflow in another terminal shell:

require 'path/to/configuration'
require 'path/to/hello_world_workflow'

Temporal.start_workflow(HelloWorldWorkflow)

Congratulation you've just created and executed a distributed workflow!

To view more details about your execution, point your browser to http://localhost:8088/namespace/ruby-samples/workflows?range=last-3-hours&status=CLOSED.

There are plenty of runnable examples demonstrating various features of this library available, make sure to check them out.

Installing dependencies

Temporal service handles all the persistence, fault tolerance and coordination of your workflows and activities. To set it up locally, download and boot the Docker Compose file from the official repo. The Docker Compose file forwards all ports to your localhost so you can interact with the containers easily from your shells.

Run:

curl -O https://raw.githubusercontent.com/temporalio/docker-compose/main/docker-compose.yml

docker-compose up

Using Credentials

SSL

In many production deployments you will end up connecting to your Temporal Services via SSL. In this case you must read the public certificate of the CA that issued your Temporal server's SSL certificate and create an instance of gRPC Channel Credentials.

Configure your Temporal connection:

Temporal.configure do |config|
    config.host = 'localhost'
    config.port = 7233
    config.namespace = 'ruby-samples'
    config.task_queue = 'hello-world'
    config.credentials = GRPC::Core::ChannelCredentials.new(root_cert, client_key, client_chain)
end

OAuth2 Token

Use gRPC Call Credentials to add OAuth2 token to gRPC calls:

Temporal.configure do |config|
    config.host = 'localhost'
    config.port = 7233
    config.namespace = 'ruby-samples'
    config.task_queue = 'hello-world'
    config.credentials = GRPC::Core::CallCredentials.new(updater_proc)
end

updater_proc should be a method that returns proc. See an example of updater_proc in googleauth library.

Combining Credentials

To configure both SSL and OAuth2 token cedentials use compose method:

Temporal.configure do |config|
    config.host = 'localhost'
    config.port = 7233
    config.namespace = 'ruby-samples'
    config.task_queue = 'hello-world'
    config.credentials = GRPC::Core::ChannelCredentials.new(root_cert, client_key, client_chain).compose(
        GRPC::Core::CallCredentials.new(token.updater_proc)
    )
end

Workflows

A workflow is defined using pure Ruby code, however it should contain only a high-level deterministic outline of the steps (their composition) that need to be executed to complete a workflow. The actual work should be defined in your activities.

NOTE: Keep in mind that your workflow code can get run multiple times (replayed) during the same execution, which is why it must NOT contain any non-deterministic code (network requests, DB queries, etc) as it can break your workflows.

Here's an example workflow:

class RenewSubscriptionWorkflow < Temporal::Workflow
  def execute(user_id)
    subscription = FetchUserSubscriptionActivity.execute!(user_id)
    subscription ||= CreateUserSubscriptionActivity.execute!(user_id)

    return if subscription[:active]

    ChargeCreditCardActivity.execute!(subscription[:price], subscription[:card_token])

    RenewedSubscriptionActivity.execute!(subscription[:id])
    SendSubscriptionRenewalEmailActivity.execute!(user_id, subscription[:id])
  rescue CreditCardNotChargedError => e
    CancelSubscriptionActivity.execute!(subscription[:id])
    SendSubscriptionCancellationEmailActivity.execute!(user_id, subscription[:id])
  end
end

In this simple workflow we are checking if a user has an active subscription and then attempt to charge their credit card to renew an expired subscription, notifying the user of the outcome. All the work is encapsulated in activities, while the workflow itself is responsible for calling the activities in the right order, passing values between them and handling failures.

There is a couple of ways to execute an activity from your workflow:

# Calls the activity by its class and blocks the execution until activity is
# finished. The return value of your activity will get assigned to the result
result = MyActivity.execute!(arg1, arg2)

# Here's a non-blocking version of the execute, returning back the future that
# will get fulfilled when activity completes. This approach allows modelling
# asynchronous workflows with activities executed in parallel
future = MyActivity.execute(arg1, arg2)
result = future.get

# Full versions of the calls from above, but has more flexibility (shown below)
result = workflow.execute_activity!(MyActivity, arg1, arg2)
future = workflow.execute_activity(MyActivity, arg1, arg2)

# In case your workflow code does not have access to activity classes (separate
# process, activities implemented in a different language, etc), you can
# simply reference them by their names
workflow.execute_activity('MyActivity', arg1, arg2, options: { namespace: 'my-namespace', task_queue: 'my-task-queue' })

Besides calling activities workflows can:

  • Use timers
  • Receive signals
  • Execute other (child) workflows
  • Respond to queries

Activities

An activity is a basic unit of work that performs the desired action (potentially causing side-effects). It can return a result or raise an error. It is defined like so:

class CloseUserAccountActivity < Temporal::Activity
  class UserNotFound < Temporal::ActivityException; end

  def execute(user_id)
    user = User.find_by(id: user_id)

    raise UserNotFound, 'User with specified ID does not exist' unless user

    user.close_account
    user.save

    AccountClosureEmail.deliver(user)

    return nil
  end
end

It is important to make your activities idempotent, because they can get retried by Temporal (in case a timeout is reached or your activity has thrown an error). You normally want to avoid generating additional side effects during subsequent activity execution.

To achieve this there are two methods (returning a UUID token) available from your activity class:

  • activity.run_idem — unique within for the current workflow execution (scoped to run_id)
  • activity.workflow_idem — unique across all execution of the workflow (scoped to workflow_id)

Both tokens will remain the same across multiple retry attempts of the activity.

Asynchronous completion

When dealing with asynchronous business logic in your activities, you might need to wait for an external event to complete your activity (e.g. a callback or a webhook). This can be achieved by manually completing your activity using a provided async_token from activity's context:

class AsyncActivity < Temporal::Activity
  def execute(user_id)
    user = User.find_by(id: user_id)

    # Pass the async_token to complete your activity later
    ExternalSystem.verify_user(user, activity.async_token)

    activity.async # prevents activity from completing immediately
  end
end

Later when a confirmation is received you'll need to complete your activity manually using the token provided:

Temporal.complete_activity(async_token, result)

Similarly you can fail the activity by calling:

Temporal.fail_activity(async_token, MyError.new('Something went wrong'))

This doesn't change the behaviour from the workflow's perspective — as any other activity the result will be returned or an error raised.

NOTE: Make sure to configure your timeouts accordingly and not to set heartbeat timeout (off by default) since you won't be able to emit heartbeats and your async activities will keep timing out.

Similar behaviour can also be achieved in other ways (one which might be more preferable in your specific use-case), e.g.:

  • by polling for a result within your activity (long-running activities with heartbeat)
  • using retry policy to keep retrying activity until a result is available
  • completing your activity after the initial call is made, but then waiting on a completion signal from your workflow

Worker

Worker is a process that communicates with the Temporal server and manages Workflow and Activity execution. To start a worker:

require 'temporal/worker'

worker = Temporal::Worker.new
worker.register_workflow(HelloWorldWorkflow)
worker.register_activity(SomeActivity)
worker.register_activity(SomeOtherActivity)
worker.start

A call to worker.start will take over the current process and will keep it unning until a TERM or INT signal is received. By only registering a subset of your workflows/activities with a given worker you can split processing across as many workers as you need.

Starting a workflow

All communication is handled via Temporal service, so in order to start a workflow you need to send a message to Temporal:

Temporal.start_workflow(HelloWorldWorkflow)

Optionally you can pass input and other options to the workflow:

Temporal.start_workflow(RenewSubscriptionWorkflow, user_id, options: { workflow_id: user_id })

Passing in a workflow_id allows you to prevent concurrent execution of a workflow — a subsequent call with the same workflow_id will always get rejected while it is still running, raising Temporal::WorkflowExecutionAlreadyStartedFailure. You can adjust the behaviour for finished workflows by supplying the workflow_id_reuse_policy: argument with one of these options:

  • :allow_failed will allow re-running workflows that have failed (terminated, cancelled, timed out or failed)
  • :allow will allow re-running any finished workflows both failed and completed
  • :reject will reject any subsequent attempt to run a workflow

Execution Options

There are lots of ways in which you can configure your Workflows and Activities. The common ones (namespace, task_queue, timeouts and retry policy) can be defined in one of these places (in the order of precedence):

  1. Inline when starting or registering a workflow/activity (use options: argument)
  2. In your workflow/activity class definitions by calling a class method (e.g. namespace 'my-namespace')
  3. Globally, when configuring your Temporal library via Temporal.configure

Periodic workflow execution

In certain cases you might need a workflow that runs periodically using a cron schedule. This can be achieved using the Temporal.schedule_workflow API that take a periodic cron schedule as a second argument:

Temporal.schedule_workflow(HealthCheckWorkflow, '*/5 * * * *')

This will instruct Temporal to run a HealthCheckWorkflow every 5 minutes. All the rest of the arguments are identical to the Temporal.start_workflow API.

NOTE: Your execution timeout will be measured across all the workflow invocations, so make sure to set it to allow as many invocations as you need. You can also set it to nil, which will use a default value of 10 years.

Breaking Changes

Since the workflow execution has to be deterministic, breaking changes can not be simply added and deployed — this will undermine the consistency of running workflows and might lead to unexpected behaviour. However, breaking changes are often needed and these include:

  • Adding new activities, timers, child workflows, etc.
  • Remove existing activities, timers, child workflows, etc.
  • Rearranging existing activities, timers, child workflows, etc.
  • Adding/removing signal handlers

In order to add a breaking change you can use workflow.has_release?(release_name) method in your workflows, which is guaranteed to return a consistent result whether or not it was called prior to shipping the new release. It is also consistent for all the subsequent calls with the same release_name — all of them will return the original result. Consider the following example:

class MyWorkflow < Temporal::Workflow
  def execute
    ActivityOld1.execute!

    workflow.sleep(10)

    ActivityOld2.execute!

    return nil
  end
end

which got updated to:

class MyWorkflow < Temporal::Workflow
  def execute
    Activity1.execute!

    if workflow.has_release?(:fix_1)
      ActivityNew1.execute!
    end

    workflow.sleep(10)

    if workflow.has_release?(:fix_1)
      ActivityNew2.execute!
    else
      ActivityOld.execute!
    end

    if workflow.has_release?(:fix_2)
      ActivityNew3.execute!
    end

    return nil
  end
end

If the release got deployed while the original workflow was waiting on a timer, ActivityNew1 and ActivityNew2 won't get executed, because they are part of the same change (same release_name), however ActivityNew3 will get executed, since the release wasn't yet checked at the time. And for every new execution of the workflow — all new activities will get executed, while ActivityOld will not.

Later on you can clean it up and drop all the checks if you don't have any older workflows running or expect them to ever be executed (e.g. reset).

NOTE: Releases with different names do not depend on each other in any way.

Testing

It is crucial to properly test your workflows and activities before running them in production. The provided testing framework is still limited in functionality, but will allow you to test basic use-cases.

The testing framework is not required automatically when you require temporal-ruby, so you have to do this yourself (it is strongly recommended to only include this in your test environment, spec_helper.rb or similar):

require 'temporal/testing'

This will allow you to execute workflows locally by running HelloWorldWorkflow.execute_locally. Any arguments provided will forwarded to your #execute method.

In case of a higher level end-to-end integration specs, where you need to execute a Temporal workflow as part of your code, you can enable local testing:

Temporal::Testing.local!

This will treat every Temporal.start_workflow call as local and perform your workflows inline. It also works with a block, restoring the original mode back after the execution:

Temporal::Testing.local! do
  Temporal.start_workflow(HelloWorldWorkflow)
end

Make sure to check out example integration specs for more details. Instructions for running these integration specs can be found in examples/README.md.

TODO

There's plenty of work to be done, but most importanly we need:

  • Write specs for everything
  • Implement support for missing features

LICENSE

Copyright 2020 Coinbase, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

More Repositories

1

terraform-landscape

Improve Terraform's plan output to be easier to read and understand
Ruby
1,570
star
2

coinbase-wallet-sdk

An open protocol that lets users connect their mobile wallets to your DApp
TypeScript
1,423
star
3

coinbase-pro-trading-toolkit

DEPRECATED — The Coinbase Pro trading toolkit
TypeScript
864
star
4

kryptology

Go
846
star
5

coinbase-pro-node

DEPRECATED — The official Node.js library for Coinbase Pro
JavaScript
839
star
6

build-onchain-apps

Accelerate your onchain creativity with the Build Onchain Apps Template. ⛵️
Solidity
701
star
7

odin

Archived: Odin deployer to AWS for 12 Factor applications.
Go
543
star
8

coinbase-python

DEPRECATED — Coinbase Python API
Python
521
star
9

onchainkit

React components and TypeScript utilities to help you build top-tier onchain apps.
TypeScript
481
star
10

solidity-style-guide

471
star
11

assume-role

DEPRECATED — assume-role: a CLI tool making it easy to assume IAM roles through an AWS Bastion account
Shell
423
star
12

geoengineer

DEPRECATED — Infrastructure As Code
Ruby
401
star
13

coinbase-node

DEPRECATED — The official Node.js library for the Coinbase API.
JavaScript
363
star
14

mesh-specifications

Specification files for the Mesh Blockchain Standard
Shell
320
star
15

cbpay-js

Coinbase Pay SDK
TypeScript
317
star
16

coinbase-php

DEPRECATED — PHP wrapper for the Coinbase API
PHP
297
star
17

smart-wallet

Solidity
287
star
18

coinbase-ruby

DEPRECATED — Ruby wrapper for the Coinbase API
Ruby
241
star
19

waas-sdk-react-native

Coinbase Wallet as a Service (WaaS) SDK for React Native. Enables MPC Operations for iOS and Android Devices.
TypeScript
234
star
20

step

step is a framework for building, testing and deploying AWS Step Functions and Lambda
Go
208
star
21

wallet-mobile-sdk

An open protocol for mobile web3 apps to interact with wallets
Kotlin
203
star
22

mesh-sdk-go

Mesh Client Go SDK
Go
192
star
23

coinbase-ios-sdk

Integrate bitcoin into your iOS application with Coinbase
Swift
171
star
24

nft-dapp-starter-kit

Starter kit for developers who want to build an NFT minting site
TypeScript
159
star
25

mesh-cli

CLI for the Mesh API
Go
154
star
26

coinbase-java

Coinbase API v1 library for Java
Java
147
star
27

coinbase-commerce-node

Coinbase Commerce Node
JavaScript
142
star
28

waas-client-library-go

Coinbase Wallet as a Service (WaaS) Client Library in Go.
Go
140
star
29

coinbase-commerce-php

Coinbase Commerce PHP
PHP
136
star
30

traffic_jam

DEPRECATED — Ruby library for time-based rate limiting
Ruby
129
star
31

dexter

Forensics acquisition framework designed to be extensible and secure
Go
122
star
32

coinbase-exchange-ruby

DEPRECATED — Official Ruby library for the GDAX API
Ruby
122
star
33

mongobetween

Go
110
star
34

multisig-tool

DEPRECATED — Multisig Vault recovery tool
JavaScript
110
star
35

mesh-bitcoin

Bitcoin Mesh API Implementation
Go
108
star
36

mesh-ethereum

Ethereum Mesh API Implementation
Go
100
star
37

coinbase-advanced-py

The Advanced API Python SDK is a Python package that makes it easy to interact with the Coinbase Advanced API. The SDK handles authentication, HTTP connections, and provides helpful methods for interacting with the API.
Python
98
star
38

coinbase-android-sdk

DEPRECATED — Android SDK for Coinbase
Java
96
star
39

react-coinbase-commerce

Coinbase Commerce React
JavaScript
92
star
40

fenrir

Archived: AWS SAM deployer to manage serverless projects.
Go
91
star
41

pwnbot

You call PwnBot in Slack on someone else's unlocked computer
JavaScript
90
star
42

commerce-onchain-payment-protocol

Solidity
90
star
43

digital-asset-policy-proposal

Digital Asset Policy Proposal: Safeguarding America’s Financial Leadership
84
star
44

coinbase-commerce-python

Coinbase Commerce Python
Python
79
star
45

magic-spend

Solidity
78
star
46

verifications

📜 "Coinbase Verifications" is a set of Coinbase-verified onchain attestations that enable access to apps and other onchain benefits.
Solidity
75
star
47

CBTabViewExample

TypeScript
72
star
48

chainstorage

The File System For a Multi-Blockchain World
Go
71
star
49

coinbase-bitmonet-sdk

DEPRECATED — Library to accept bitcoin payments in your Android App
Java
63
star
50

self-service-iam

DEPRECATED — Self Service AWS IAM Policies for dev at scale
JavaScript
58
star
51

coinbase-wordpress

DEPRECATED — Coinbase plugin/widget for Wordpress
57
star
52

coinbase-commerce-woocommerce

Accept Bitcoin on your WooCommerce-powered website.
PHP
57
star
53

paymaster-bundler-examples

JavaScript
55
star
54

barbar

DEPRECATED — OSX crypto-currency price ticker
Swift
53
star
55

demeter

DEPRECATED — Security Group Management For AWS
Ruby
52
star
56

coinbase-exchange-node

DEPRECATED — Use gdax-node
JavaScript
47
star
57

cadence-ruby

Ruby SDK for Cadence
Ruby
43
star
58

coinbase-woocommerce

DEPRECATED — Accept Bitcoin on your WooCommerce-powered website.
38
star
59

protoc-gen-rbi

Protobuf compiler plugin that generates Sorbet .rbi "Ruby Interface" files.
Go
38
star
60

mesh-ecosystem

Repository of all open source Mesh implementations and SDKs
33
star
61

master_lock

Inter-process locking library using Redis.
Ruby
32
star
62

coinbase-commerce-ruby

Coinbase Commerce Ruby Gem
Ruby
30
star
63

watchdog

DEPRECATED -- Github Bot for Datadog codification
Go
28
star
64

bittip

DEPRECATED — Reddit tip bot
JavaScript
27
star
65

cash-addr

Utility to convert between base58 and CashAddr BCH addresses.
Ruby
26
star
66

maxfuzz

DEPRECATED — Containerized Cloud Fuzzing
C
26
star
67

rules_ruby

Bazel Ruby Rules
Starlark
24
star
68

mesh-geth-sdk

go-ethereum based sdk for Mesh API
Go
24
star
69

onchain-app-template

TypeScript
24
star
70

redisbetween

Go
23
star
71

gtt-ui

DEPRECATED
JavaScript
22
star
72

btcexport

Export process for Bitcoin blockchain data to CSV
Go
22
star
73

bchd-explorer

Vue
21
star
74

baseca

Go
21
star
75

coinbase-sdk-nodejs

TypeScript
21
star
76

coinbase-commerce-whmcs

Coinbase Commerce module for WHMCS
PHP
18
star
77

coinbase-nft-floor-price

Coinbase NFT floor price estimate model
Python
17
star
78

coinbase-magento

DEPRECATED — Accept Bitcoin on your Magento-powered website.
17
star
79

salus

We would like to request that all contributors please clone a *fresh copy* of this repository since the September 21st maintenance.
HTML
17
star
80

coinbase-android-sdk-example

DEPRECATED — Example android app leveraging the coinbase android sdk
Java
15
star
81

staking-client-library-go

Programmatic access to Coinbase's best-in-class staking infrastructure and services. 🔵
Go
15
star
82

coinbase-spree

DEPRECATED — Accept bitcoin payments on your Spree store with Coinbase.
15
star
83

coinbase-cloud-sdk-js

TypeScript
14
star
84

staking-client-library-ts

Programmatic access to Coinbase's best-in-class staking infrastructure and services. 🔵
TypeScript
14
star
85

service_variables

Service level variables backed by Redis - useful for service wide configuration.
Ruby
12
star
86

solidity-workshop

JavaScript
12
star
87

chainsformer

Go
12
star
88

omniauth-coinbase

DEPRECATED — Coinbase OAuth 2 Strategy for Omniauth
Ruby
12
star
89

coinbase-javascript-sdk

DEPRECATED
JavaScript
11
star
90

coinbase-commerce-prestashop

DEPRECATED — Official Coinbase Commerce Prestashop Payment Module
PHP
11
star
91

mkr-vote-proxy

Cold storage-friendly voting for MKR tokens
Solidity
11
star
92

chainnode

Go
10
star
93

step-asg-deployer

Deprecated, renamed and maintained at https://github.com/coinbase/odin
Go
10
star
94

wrapped-tokens-os

TypeScript
10
star
95

eip-token-upgrade

Solidity
10
star
96

client-analytics

TypeScript
9
star
97

code-of-conduct

Code of conduct for open source projects managed by Coinbase
9
star
98

coinbase-magento2

DEPRECATED: Accept Bitcoin on your Magento2-powered website.
8
star
99

coinbase-commerce-opencart

DEPRECATED — Coinbase Commerce Integration For Opencart
PHP
8
star
100

waas-proxy-server

Go
7
star