Tide Hex.pmBuild Status

Similar to elixir/export but design for asynchronous.

Installation

Adding tide to your list of dependencies in mix.exs:

def deps do
  [
    {:tide, "~> 0.1.0"}
  ]
end

Usage

Start Tide.Worker to handle event

children = [
  {Tide.Worker, "priv/ruby"},
  # ...
]
options = [strategy: :one_for_one, name: __MODULE__]

Supervisor.start_link(children, options)

Load your Ruby script

Tide.Worker.load("app")

In most cases, you only need to load the one script

Register Event

Create a Ruby script (e.g. priv/ruby/app.rb) and put your code

require 'elixir/tide'

# Immediately will use the return value
Elixir::Tide.on("say") do |name|
  [:ok, "Hello #{name}"]
end

# Async event use "Tide.Agent.emit"
Elixir::Tide.on("sleep") do |wait_time|
  sleep wait_time.to_i
  reply :ok, "I am awake!"
end

Agent

Start a Tide.Agent to manage events

{:ok, pid} = Tide.Agent.start_link()

Execute event (return immediately)

{:ok, message } = Tide.Agent.exec(pid, "say", ["World"])
# => "Hello World"

Emit event (asynchronous, and managed by Tide.Reaction)

{:ok, message } = Tide.Agent.emit(pid, "sleep", [1])
# After 1 seconds
{:ok, message } =
  Tide.Agent.reaction(pid)
  |> Tide.Reaction.next

# => "I am awake!"

Reaction

The Tide.Reaction is a queue collect the reply from Ruby. You can use it to prevent the Tide.Agent.exec blocking your process.

State

The Erlport didn't support call Erlang function from Thread, and to ensure your Ruby script can recover from crash. Use the Tide.State to persist the state and it will pass to Ruby by Tide.

Tide.Agent.state(pid)
|> Tide.State.put(:name, "Hello")
Elixir::Tide.on("from_state") do
  puts "Hi, #{state[:name]}"
end

Roadmap