Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use GenServer #72

Merged
merged 7 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
import_deps: [:typed_struct]
import_deps: [:typed_struct],
plugins: [Styler]
]
23 changes: 12 additions & 11 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,19 @@ jobs:
- name: Test
run: mix test

credo:
runs-on: ubuntu-latest
needs: mix
steps:
- name: Setup
uses: byzanteam/jet-actions/setup-elixir@main
with:
otp-version: ${{ env.OTP_VERSION }}
elixir-version: ${{ env.ELIXIR_VERSION }}
# FIXME: fix all credo issues
# credo:
# runs-on: ubuntu-latest
# needs: mix
# steps:
# - name: Setup
# uses: byzanteam/jet-actions/setup-elixir@main
# with:
# otp-version: ${{ env.OTP_VERSION }}
# elixir-version: ${{ env.ELIXIR_VERSION }}

- name: Credo
run: mix credo --strict
# - name: Credo
# run: mix credo --strict

dialyzer:
runs-on: ubuntu-latest
Expand Down
16 changes: 11 additions & 5 deletions lib/workflow_metal/application/config.ex
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
defmodule WorkflowMetal.Application.Config do
@moduledoc false

@type t :: map()
@type application :: WorkflowMetal.Application.t()
@type t() :: map()
@type application() :: WorkflowMetal.Application.t()

@typep key() :: atom()
@typep value() :: term()
# credo:disable-for-previous-line JetCredo.Checks.ExplicitAnyType

@doc false
@spec start_link(application, t) :: Agent.on_start()
@spec start_link(application(), t()) :: Agent.on_start()
def start_link(application, initial_value) do
Agent.start_link(fn -> initial_value end, name: config_name(application))
end

@doc """
Store settings of an application.
"""
@spec set(application, atom, term) :: :ok
@spec set(application(), key(), value()) :: :ok
def set(application, key, value) when is_atom(application) do
Agent.update(config_name(application), fn state ->
Keyword.put(state, key, value)
Expand All @@ -23,7 +27,7 @@ defmodule WorkflowMetal.Application.Config do
@doc """
Retrieve settings of an application.
"""
@spec get(application, atom) :: term
@spec get(application(), key()) :: value()
def get(application, key) when is_atom(application) and is_atom(key) do
Agent.get(config_name(application), fn state ->
Keyword.get(state, key)
Expand All @@ -33,11 +37,13 @@ defmodule WorkflowMetal.Application.Config do
@doc """
Retrieves the compile time configuration.
"""
@spec compile_config(application(), Keyword.t()) :: Keyword.t()
def compile_config(_application, config) do
Keyword.take(config, [:name, :registry, :storage])
end

defp config_name(application) do
# credo:disable-for-next-line Credo.Check.Warning.UnsafeToAtom
Module.concat(application, Config)
end
end
14 changes: 6 additions & 8 deletions lib/workflow_metal/application/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ defmodule WorkflowMetal.Application.Supervisor do

config_child_spec = config_child_spec(application, config)

children =
[
config_child_spec,
registry_child_spec,
storage_child_spec,
{WorkflowMetal.Application.WorkflowsSupervisor, application}
]
|> Enum.filter(& &1)
children = [
config_child_spec,
registry_child_spec,
storage_child_spec,
{WorkflowMetal.Application.WorkflowsSupervisor, application}
]

Supervisor.init(children, strategy: :one_for_one)
end
Expand Down
4 changes: 1 addition & 3 deletions lib/workflow_metal/application/workflows_supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ defmodule WorkflowMetal.Application.WorkflowsSupervisor do
WorkflowMetal.Registration.Adapter.on_start_child()
| {:error, :workflow_not_found}
def open_workflow(application, workflow_id) do
with(
{:ok, workflow_schema} <- WorkflowMetal.Storage.fetch_workflow(application, workflow_id)
) do
with({:ok, workflow_schema} <- WorkflowMetal.Storage.fetch_workflow(application, workflow_id)) do
workflows_supervisor = supervisor_name(application)
workflow_supervisor = {WorkflowMetal.Workflow.Supervisor, workflow_id: workflow_schema.id}

Expand Down
Loading
Loading