Skip to content

Instantly share code, notes, and snippets.

View slashdotdash's full-sized avatar

Ben Smith slashdotdash

View GitHub Profile
@slashdotdash
slashdotdash / README.md
Last active November 21, 2017 16:24
Using Commanded Ecto projections with Elixir's Registry for pub/sub read model notifications

Using Commanded Ecto projections

Example read model projections using Commanded Ecto projections where Elixir's Registry is used for pub/sub notifications of read model updates.

This alleviates the problem of async read model updates.

The command dispatcher can wait until the read model has been updated to the exact aggregate version (as returned by the dispatch command):

with {:ok, version} <- Router.dispatch(register_user, include_aggregate_version: true) do
@slashdotdash
slashdotdash / gist:190361
Created September 21, 2009 16:29
MySQL, Alter Table, and How to Observe Progress
#!/bin/bash
(while(true); do \
(mysql -e 'show innodb status \G' | grep undo\ log\ entries ; sleep 1 ; \
mysql -e 'show innodb status \G' | grep undo\ log\ entries ) | \
egrep '[0-9][0-9][0-9][0-9]' |awk '{print $10;}' ; done ) | \
perl -e '$t = ROWS_IN_TABLE; while(1) { \
$n ++; $nn; $a = <>; $b = <>; $nn += ($b-$a); \
printf "Rate: %d, avg: %d, %0.3f%% complete, done in %d sec\n", \
$b-$a, $nn/$n, ($b/$t)*100, ($t-$b)/($nn/$n); }';
@slashdotdash
slashdotdash / config.ex
Created February 2, 2018 15:18
Environment specific consistency setting for Commanded event handlers
# config/config.exs
use Mix.Config
config :my_app, consistency: :eventual
@slashdotdash
slashdotdash / limited_ring.ex
Last active April 2, 2018 11:50
Swarm ring distribution strategy limited to a pre-defined node list
defmodule Swarm.Distribution.LimitedRing do
@moduledoc false
use Swarm.Distribution.Strategy
@app_nodes [
:"myapp1@127.0.0.1",
:"myapp2@127.0.0.1",
:"myapp3@127.0.0.1"
]
@slashdotdash
slashdotdash / data_case.ex
Created May 15, 2018 08:21
Reset event store and read model store databases during test execution
# test/support/data_case.ex
defmodule MyApp.DataCase do
use ExUnit.CaseTemplate
using do
quote do
import Ecto
import Ecto.Changeset
import Ecto.Query
import Commanded.Assertions.EventAssertions
@slashdotdash
slashdotdash / task1.exs
Created July 6, 2018 12:09 — forked from moklett/task1.exs
Elixir Task - Crash Handling
# This demonstrates that, when using async/await, a crash in the task will crash the caller
defmodule Tasker do
def good(message) do
IO.puts message
end
def bad(message) do
IO.puts message
raise "I'm BAD!"
end
@slashdotdash
slashdotdash / bank_account.ex
Created July 11, 2018 13:58
Eventide style command handling in Commanded
defmodule Withdraw do
defstruct [:account_number, :amount]
end
defmodule Withdrawn do
defstruct [:account_number, :amount]
end
defmodule WithdrawalRejected do
defstruct [:account_number, :amount]
@slashdotdash
slashdotdash / article.ex
Last active August 15, 2018 12:37
Commanded multi-entity aggregates
defmodule Article do
defstruct [:content, comments: []]
# public commands
def execute(%Article{}, %PublishArticle{content: content}) do
%ArticlePublished{content: content}
end
def execute(%Article{}, %CommentOnArticle{} = comment) do
# Elixir v1.0
defmodule Rules do
defmacro __using__(_) do
quote do
import unquote(__MODULE__)
@before_compile unquote(__MODULE__)
@rules []
end
end
defmodule MyApp.DataCase do
use ExUnit.CaseTemplate
using do
quote do
import Commanded.Assertions.EventAssertions
end
end
setup do