forked from commanded/commanded
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request commanded#352 from commanded/feature/application-m…
…iddleware Allow Commanded Application name to be set dynamically in middleware
- Loading branch information
Showing
8 changed files
with
117 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule Commanded.Middleware.ApplicationMiddlewareTest do | ||
use Commanded.StorageCase | ||
|
||
alias Commanded.Middleware.Tenant.Commands.RegisterTenant | ||
alias Commanded.Middleware.TenantApp | ||
|
||
setup do | ||
for tenant_id <- 1..3 do | ||
start_supervised!({TenantApp, name: TenantApp.tenant_application_name(tenant_id)}) | ||
end | ||
|
||
:ok | ||
end | ||
|
||
test "dispatch command to dynamic application set in middleware" do | ||
for tenant_id <- 1..3 do | ||
command = %RegisterTenant{tenant_id: tenant_id, name: "Tenant #{tenant_id}"} | ||
|
||
assert :ok = TenantApp.dispatch(command) | ||
|
||
name = TenantApp.tenant_application_name(tenant_id) | ||
pid = Process.whereis(name) | ||
assert is_pid(pid) | ||
end | ||
end | ||
|
||
test "raise `RuntimeError` when application not started" do | ||
command = %RegisterTenant{tenant_id: 4, name: "Tenant 4"} | ||
|
||
assert_raise RuntimeError, fn -> | ||
:ok = TenantApp.dispatch(command) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Commanded.Middleware.Tenant do | ||
defmodule Commands do | ||
defmodule RegisterTenant do | ||
@enforce_keys [:tenant_id, :name] | ||
defstruct [:tenant_id, :name] | ||
end | ||
end | ||
|
||
defmodule Events do | ||
defmodule TenantRegistered do | ||
@derive Jason.Encoder | ||
defstruct [:tenant_id, :name] | ||
end | ||
end | ||
|
||
alias Commanded.Middleware.Tenant | ||
alias Commanded.Middleware.Tenant.Commands.RegisterTenant | ||
alias Commanded.Middleware.Tenant.Events.TenantRegistered | ||
|
||
defstruct [:tenant_id, :name] | ||
|
||
def execute(%Tenant{tenant_id: nil}, %RegisterTenant{} = command) do | ||
%RegisterTenant{tenant_id: tenant_id, name: name} = command | ||
|
||
%TenantRegistered{tenant_id: tenant_id, name: name} | ||
end | ||
|
||
def apply(%Tenant{} = tenant, %TenantRegistered{} = event) do | ||
%TenantRegistered{tenant_id: tenant_id, name: name} = event | ||
|
||
%Tenant{tenant | tenant_id: tenant_id, name: name} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Commanded.Middleware.TenantApp do | ||
use Commanded.Application, otp_app: :commanded | ||
|
||
alias Commanded.Middleware.TenantRouter | ||
|
||
router(TenantRouter) | ||
|
||
def tenant_application_name(tenant_id) do | ||
Module.concat([__MODULE__, "tenant#{tenant_id}"]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule Commanded.Middleware.TenantMiddleware do | ||
@behaviour Commanded.Middleware | ||
|
||
alias Commanded.Middleware.Pipeline | ||
alias Commanded.Middleware.TenantApp | ||
|
||
def before_dispatch(%Pipeline{} = pipeline) do | ||
%Pipeline{command: command} = pipeline | ||
|
||
# Dynamically set application name from `tenant_id` in command | ||
tenant_id = Map.fetch!(command, :tenant_id) | ||
application = TenantApp.tenant_application_name(tenant_id) | ||
|
||
%Pipeline{pipeline | application: application} | ||
end | ||
|
||
def before_dispatch(pipeline), do: pipeline | ||
|
||
def after_dispatch(pipeline), do: pipeline | ||
def after_failure(pipeline), do: pipeline | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Commanded.Middleware.TenantRouter do | ||
use Commanded.Commands.Router | ||
|
||
alias Commanded.Middleware.Tenant | ||
alias Commanded.Middleware.Tenant.Commands.RegisterTenant | ||
alias Commanded.Middleware.TenantMiddleware | ||
|
||
middleware(TenantMiddleware) | ||
|
||
dispatch(RegisterTenant, to: Tenant, identity: :tenant_id) | ||
end |