Skip to content

Commit

Permalink
implement transtions and state machine for cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Jun 21, 2024
1 parent a3d7ca7 commit e0b2556
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/polar/machines/cluster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ defmodule Polar.Machines.Cluster do
import Ecto.Changeset

alias __MODULE__.Credential
alias __MODULE__.Transitions
alias __MODULE__.Event

use Eventful.Transitable

Transitions
|> governs(:current_state, on: Event)

@valid_attrs ~w(
name
Expand Down
12 changes: 12 additions & 0 deletions lib/polar/machines/cluster/event.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Polar.Machines.Cluster.Event do
alias Polar.Machines.Cluster
alias Polar.Accounts.User

use Eventful,
parent: {:cluster, Cluster},
actor: {:user, User}

alias Cluster.Transitions

handle(:transitions, using: Transitions)
end
2 changes: 2 additions & 0 deletions lib/polar/machines/cluster/transit.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defimpl Eventful.Transit, for: Polar.Machines.Cluster do
alias Polar.Machines.Cluster.Event

def perform(cluster, user, event_name, options \\ []) do
comment = Keyword.get(options, :comment)
domain = Keyword.get(options, :domain, "transitions")
Expand Down
1 change: 1 addition & 0 deletions lib/polar/machines/cluster/triggers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Polar.Machines.Cluster.Triggers do
use Eventful.Trigger

alias Polar.Machines.Cluster
alias Polar.Machines.Cluster.Connect

Cluster
|> trigger([currently: "connecting"], fn event, cluster ->
Expand Down
28 changes: 28 additions & 0 deletions priv/repo/migrations/20240621115038_create_cluster_events.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Polar.Repo.Migrations.CreateClusterEvents do
use Ecto.Migration

def change do
create table(:cluster_events) do
add(:name, :citext, null: false)
add(:domain, :citext, null: false)
add(:metadata, :map, default: "{}")

add(
:cluster_id,
references(:clusters, on_delete: :restrict),
null: false
)

add(
:user_id,
references(:users, on_delete: :restrict),
null: false
)

timestamps(type: :utc_datetime_usec)
end

create index(:cluster_events, [:cluster_id])
create index(:cluster_events, [:user_id])
end
end

0 comments on commit e0b2556

Please sign in to comment.