https://github.com/doomspork/transfusion
An experimental event-based work flow
https://github.com/doomspork/transfusion
elixir-lang experimental message-passing
Last synced: about 1 year ago
JSON representation
An experimental event-based work flow
- Host: GitHub
- URL: https://github.com/doomspork/transfusion
- Owner: doomspork
- License: mit
- Created: 2017-06-23T15:00:27.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-10T18:23:59.000Z (over 8 years ago)
- Last Synced: 2025-02-15T02:44:12.915Z (over 1 year ago)
- Topics: elixir-lang, experimental, message-passing
- Language: Elixir
- Homepage:
- Size: 27.3 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Transfusion [![Build Status][travis-img]][travis] [![License][license-img]][license]
[travis-img]: https://travis-ci.org/doomspork/transfusion.svg?branch=master
[travis]: https://travis-ci.org/doomspork/transfusion
[license-img]: http://img.shields.io/badge/license-MIT-brightgreen.svg
[license]: http://opensource.org/licenses/MIT
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:
```elixir
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.
```elixir
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!
```elixir
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}`.
```elixir
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][issues] and [Pull Requests][pulls]. All code
should have accompanying tests.
[issues]: https://github.com/doomspork/transfusion/issues
[pulls]: https://github.com/doomspork/transfusion/pulls
## License
MIT license. Please see [LICENSE][license] for details.
[LICENSE]: https://github.com/doomspork/transfusion/blob/master/LICENSE