Skip to content

Instantly share code, notes, and snippets.

View slashdotdash's full-sized avatar

Ben Smith slashdotdash

View GitHub Profile
@slashdotdash
slashdotdash / json_serializer.ex
Created February 21, 2020 15:33
Commanded JSON serializer supporting string keys
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 February 18, 2020 15:23
Publish to an Absinthe GraphQL subscription
@slashdotdash
slashdotdash / open_account.ex
Last active December 30, 2019 12:09
Use Ecto Changeset to validate a command
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 September 11, 2019 14:07
Commanded event handler default error handling

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 July 8, 2019 11:59
AWS CloudWatch reporter for Commanded telemetry.
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 11:13
A Commanded event handler to produce `:telemetry` events for each recorded event.
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 July 13, 2020 08:42
A Commanded middleware to instrument the command dispatch pipeline with `:telemetry` events.
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 March 26, 2019 16:29
Architecture principles from "Design It!" by Michael Keeling

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 August 8, 2019 11:47
Commanded aggregate ExUnit case template
defmodule AggregateCase do
@moduledoc """
Defines a test case to be used by aggregate tests.
"""
use ExUnit.CaseTemplate
alias Commanded.Aggregate.Multi
using opts do
@slashdotdash
slashdotdash / jsonb_serializer.ex
Last active December 6, 2019 14:49
Commanded jsonb serializer with decode support
defmodule JsonbSerializer do
@moduledoc """
Serialize to/from PostgreSQL's native `jsonb` format.
"""
@behaviour EventStore.Serializer
alias Commanded.EventStore.TypeProvider
alias Commanded.Serialization.JsonDecoder