Skip to content

Commit 45502eb

Browse files
authored
Merge pull request #2 from cretz/activity-worker
Activity worker sample
2 parents 9c5d96e + 2fab1e0 commit 45502eb

16 files changed

Lines changed: 457 additions & 1 deletion

File tree

.github/workflows/ci.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Continuous Integration
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
- main
7+
- "releases/*"
8+
9+
jobs:
10+
build-lint-test:
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
os: [ubuntu-latest, macos-12, macos-latest]
15+
# Earliest and latest supported
16+
rubyVersion: ["3.1", "3.3"]
17+
runs-on: ${{ matrix.os }}
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v2
21+
22+
- name: Setup Ruby
23+
uses: ruby/setup-ruby@v1
24+
with:
25+
ruby-version: ${{ matrix.rubyVersion }}
26+
27+
- name: Install bundle
28+
run: bundle install
29+
30+
- name: Lint and test
31+
run: bundle exec rake TESTOPTS="--verbose"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Gemfile.lock

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
AllCops:
2+
NewCops: enable
3+
TargetRubyVersion: 3.1
4+
SuggestExtensions: false

CONTRIBUTING.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Contributing
2+
3+
## Creating a sample
4+
5+
When creating a sample, do the following:
6+
7+
* Create a dir for the entire sample to live in, using underscores as word separators
8+
* Add code to sample dir that:
9+
* Separates workers from starters
10+
* Separates main/primary code from testable, modular code (latter should be in module named after the sample dir)
11+
* If needed, add tests in `test/<sample-dir>/<subject>_test.rb`
12+
* Add a `README.md` to sample dir that:
13+
* Has high-level overview of what the sample shows
14+
* Links to original `../README.md` for prerequisites
15+
* Explains any additional setup steps
16+
* Explains what to run and when/how to see desired result
17+
* Adds anything else necessary to understand the sample
18+
* Add the sample dir to the root `README.md` bulleted list (keep list in alphabetical order)
19+
* Add any extra dependencies the sample requires to the root `Gemfile` as a group named after the sample (keep groups in
20+
alphabetical order)
21+
* Add any extra tasks the sample requires to the root `Rakefile` under a namespace named after the sample (keep
22+
namespaces in alphabetical order)

Gemfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
gem 'temporalio'
6+
7+
group :development do
8+
gem 'minitest'
9+
gem 'rake'
10+
gem 'rubocop'
11+
end

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
1-
⚠️ UNDER ACTIVE DEVELOPMENT
1+
# Temporal Ruby SDK Samples
2+
3+
This is the set of Ruby samples for the [Ruby SDK](https://github.com/temporalio/sdk-ruby).
4+
5+
⚠️ UNDER ACTIVE DEVELOPMENT
6+
7+
The Ruby SDK is under active development and has not released a stable version yet. APIs may change in incompatible ways
8+
until the SDK is marked stable.
9+
10+
## Usage
11+
12+
Prerequisites:
13+
14+
* Ruby 3.1+
15+
* Local Temporal server running (can [install CLI](https://docs.temporal.io/cli#install) then
16+
[run a dev server](https://docs.temporal.io/cli#start-dev-server))
17+
* `bundle install` run in the root
18+
19+
## Samples
20+
21+
<!-- Keep this list in alphabetical order -->
22+
* [activity_worker](activity_worker) - Use Ruby activities from a workflow in another language.
23+
24+
## Development
25+
26+
To check format and test this repository, run:
27+
28+
bundle exec rake

Rakefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require 'rake/testtask'
4+
5+
Rake::TestTask.new(:test) do |t|
6+
t.warning = false
7+
t.libs << 'test'
8+
t.libs << '.'
9+
t.test_files = FileList['test/**/*_test.rb']
10+
end
11+
12+
require 'rubocop/rake_task'
13+
14+
RuboCop::RakeTask.new
15+
16+
task default: %w[rubocop test]

activity_worker/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Activity Worker
2+
3+
This sample shows a Go workflow calling Ruby activity.
4+
5+
To run, first see [README.md](../README.md) for prerequisites. Then, with [Go](https://go.dev/) installed, run the
6+
following from the [go-workflow] directory in a separate terminal to start the Go workflow worker:
7+
8+
go run .
9+
10+
Then in another terminal, start the Ruby activity worker from this directory:
11+
12+
bundle exec ruby activity_worker.rb
13+
14+
Finally in another terminal, use the Ruby client to the workflow from this directory:
15+
16+
bundle exec ruby starter.rb
17+
18+
The Ruby code will invoke the Go workflow which will execute the Ruby activity and return. The output of the final
19+
command should be:
20+
21+
```
22+
Workflow result: Hello, SomeUser!
23+
```

activity_worker/activity.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require 'temporalio/activity'
4+
5+
module ActivityWorker
6+
# Activity is a class with execute implemented
7+
class SayHelloActivity < Temporalio::Activity
8+
def execute(name)
9+
"Hello, #{name}!"
10+
end
11+
end
12+
end

activity_worker/activity_worker.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'activity'
4+
require 'temporalio/client'
5+
require 'temporalio/worker'
6+
7+
# Create a client
8+
client = Temporalio::Client.connect('localhost:7233', 'default')
9+
10+
# Create worker with the client and activity
11+
worker = Temporalio::Worker.new(
12+
client:,
13+
task_queue: 'activity-worker-sample',
14+
# By providing the class to the activity, it will be instantiated for every
15+
# attempt. If we provide an instance (e.g. SayHelloActivity.new), the same
16+
# instance is reused.
17+
activities: [ActivityWorker::SayHelloActivity]
18+
)
19+
20+
# Run the worker until SIGINT
21+
puts 'Starting worker (ctrl+c to exit)'
22+
worker.run(shutdown_signals: ['SIGINT'])

0 commit comments

Comments
 (0)