Skip to content

Instantly share code, notes, and snippets.

Avatar

Ben Smith slashdotdash

View GitHub Profile
@slashdotdash
slashdotdash / README.md
Created May 28, 2020
Elixir GenServer with a `handle_info/2` callback will crash when receiving an unexpected message
View README.md

Elixir GenServer with a handle_info/2 callback will crash when receiving an unexpected message

Define a GenServer with a handle_info callback function:

defmodule Echo do
  use GenServer

  def start_link(reply_to) do
    GenServer.start_link(__MODULE__, reply_to)
@slashdotdash
slashdotdash / json_serializer.ex
Created Feb 21, 2020
Commanded JSON serializer supporting string keys
View json_serializer.ex
defmodule JsonSerializer do
alias Commanded.EventStore.TypeProvider
alias Commanded.Serialization.JsonDecoder
@doc """
Serialize given term to JSON binary data.
"""
def serialize(term) do
Jason.encode!(term)
end
@slashdotdash
slashdotdash / README.md
Created Feb 18, 2020
Publish to an Absinthe GraphQL subscription
View README.md
@slashdotdash
slashdotdash / open_account.ex
Last active Dec 30, 2019
Use Ecto Changeset to validate a command
View open_account.ex
defmodule OpenAccount do
defstruct [:account_number, :initial_balance]
@types %{
account_number: :string,
initial_balance: :integer
}
def validate(params) do
{params, @types}
@slashdotdash
slashdotdash / README.md
Created Sep 11, 2019
Commanded event handler default error handling
View README.md

Commanded event handler default error handling

Implement a default event handler error/3 callback function with optional support for retrying failed events before eventually skipping it.

Usage

defmodule MyApp.ExampleHandler do
  use Commanded.Event.Handler, name: __MODULE__
  use Commanded.Event.DefaultErrorHandling, retries: 3
@slashdotdash
slashdotdash / CloudWatchReporter.ex
Last active Jul 8, 2019
AWS CloudWatch reporter for Commanded telemetry.
View CloudWatchReporter.ex
defmodule CloudWatchReporter do
use GenServer
require Logger
alias ExAws.Cloudwatch
@namespace "My/App"
def start_link(args) do
@slashdotdash
slashdotdash / event_telemetry.ex
Created May 3, 2019
A Commanded event handler to produce `:telemetry` events for each recorded event.
View event_telemetry.ex
defmodule Commanded.EventTelemetry do
@moduledoc """
A Commanded event handler to produce `:telemetry` events for each recorded
event.
It produces the following event:
- `[:commanded, :event, :published]`
"""
@slashdotdash
slashdotdash / telemetry.ex
Last active Jul 13, 2020
A Commanded middleware to instrument the command dispatch pipeline with `:telemetry` events.
View telemetry.ex
defmodule Commanded.Middleware.Telemetry do
@moduledoc """
A Commanded middleware to instrument the command dispatch pipeline with
`:telemetry` events.
It produces the following three events:
- `[:commanded, :command, :dispatch, :start]`
- `[:commanded, :command, :dispatch, :success]`
- `[:commanded, :command, :dispatch, :failure]`
@slashdotdash
slashdotdash / architecture.md
Created Mar 26, 2019
Architecture principles from "Design It!" by Michael Keeling
View architecture.md

Architecture principles

From Design It! From Programmer to Software Architect by Michael Keeling.

  • Partition system and assign responsibilities.
  • Decide trade-offs among quality attributes:
    • Performance (response time)
    • Scalability (annual growth)
    • Availability
    • Security
@slashdotdash
slashdotdash / aggregate_case.ex
Last active Aug 8, 2019
Commanded aggregate ExUnit case template
View aggregate_case.ex
defmodule AggregateCase do
@moduledoc """
Defines a test case to be used by aggregate tests.
"""
use ExUnit.CaseTemplate
alias Commanded.Aggregate.Multi
using opts do
You can’t perform that action at this time.