Tide 

Similar to elixir/export but design for asynchronous.
Installation
Adding tide to your list of dependencies in mix.exs:
def deps do
[
{:tide, "~> 0.2.1"}
]
endUsage
Start Tide.Supervisor to create Ruby worker pool
children = [
{Tide.Supervisor, root: :code.priv_dir(:app_name) |> Path.join("ruby"), file: "app"},
# ...
]
options = [strategy: :one_for_one, name: __MODULE__]
Supervisor.start_link(children, options)Register Event
Create a Ruby script (e.g. priv/ruby/app.rb) and put your code
# Immediately will use the return value
Elixir::Tide.on("say") do |name|
reply :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!"
endAgent
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.
Attach a state to agent
{:ok, state} = Tide.State.start_link()
{:ok, agent} = Tide.Agent.start_link(state)
When we execute an event the state will convert to keyword list and save as Tide::State object.
That means the Map or Keyword List is accept as a state.
state = %{user_id: 1, name: "John"}
{:ok, agent} = Tide.Agent.start_link(state)
The state is part of agent and unable directly change it, but we can use Tide.Agent.update/2 to update it.
agent
|> Tide.Agent.update(fn(state) -> state |> Map.put(:name, "Bob") end)Or replace state to another new state
agent
|> Tide.Agent.update(%{user_id: 1, name: "Alice"})Roadmap
-
Worker Pool
-
[x] Customize root directory by
rootoptions -
[x] Customize main file by
fileoptions -
[x] Customize initialize function by
functionoptions -
[ ] Customize environment variable by
envoptions
-
[x] Customize root directory by
-
User Defined Module
-
[ ] Customize
Tide.Reaction -
[ ] Customize
Tide.State -
[x] Customize
Tide.Supervisor -
[ ] Customize
Tide.Agent
-
[ ] Customize