Skip to content

doomspork/transfusion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Transfusion Build Status License

An experimental event-based work flow — you probably don't want to use it just yet.

Installation

The package is currently not on hex, instead point to master:

def deps do
  [{:transfusion, github: "doomspork/transfusion"}]
end

Routing Messages

The router is the core of Transfusion handling the message routing and results. The router also maintains a list of current messages, retrying expired messages with an exponential backoff.

defmodule Example.Router do
  use Transfusion.Router,
    max_retries: 5,
    retry_after: 10000 # milliseconds

  require Logger

  broadcast "*", to: [AnotherExample.Router]

  forward "users", to: Users.Router

  topic "events", Example.Consumer do
    map "new.message", to: :new
  end

  def handle_error(msg, reason) do
    # Log or submit your error

    :noretry
  end
end

The broadcast/2 macro enables us to distributes messages to other router throughout our application in addition to processing it ourselves.

We can redirect, or forward, all messages for a given topic to another router using forward/2.

Most importantly is the topic/2 and map/2 macros, with these we can subscribe a consumer to various messages on a given topic.

Message Producing

Messages are primarily syntactic sugar for creating structs with a splash of validation. You can use Transfusion without creating a message module, maps work just fine!

defmodule Example.Message do
  use Transfusion.Message,
    router: Example.Router

  topic "events"

  message_type "new.message"

  values do
    attribute :subject, String
    attribute :body, String, required: true
  end
end

Message Consuming

Consumers do the real work in Transfusion. A consumer received events and is expected to do some work, returning {:ok, result} or {:error, reason}.

defmodule Example.Consumer do
  require Logger

  def new(%{body: body, _meta: %{id: id}}),
    do: Logger.info("Message (id: #{id}) received with body: #{body}")
end

Contributing

Feedback, feature requests, and fixes are welcomed and encouraged. Please make appropriate use of Issues and Pull Requests. All code should have accompanying tests.

License

MIT license. Please see LICENSE for details.

Releases

No releases published

Packages

No packages published

Languages