Skip to content

Commit

Permalink
refactor GenServer to use function on client side instead of GenServe…
Browse files Browse the repository at this point in the history
…r fct, #129
  • Loading branch information
SimonLab committed Jun 29, 2017
1 parent 51eb119 commit 5df7589
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 11 deletions.
1 change: 1 addition & 0 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ config :dwylbot, Dwylbot.Repo,
pool_size: 10

config :dwylbot, :github_api, Dwylbot.GithubAPI.HTTPClient
config :dwylbot, :time_merge_errors, 5000
1 change: 1 addition & 0 deletions config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ config :dwylbot, Dwylbot.Repo,
ssl: true

config :dwylbot, :github_api, Dwylbot.GithubAPI.HTTPClient
config :dwylbot, :time_merge_errors, 5000

# ## SSL Support
#
Expand Down
1 change: 1 addition & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ config :dwylbot, Dwylbot.Repo,
pool: Ecto.Adapters.SQL.Sandbox

config :dwylbot, :github_api, Dwylbot.GithubAPI.InMemory
config :dwylbot, :time_merge_errors, 500
19 changes: 19 additions & 0 deletions test/controllers/merge_errors_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule Dwylbot.MergeErrorsTest do
use ExUnit.Case

test "send error" do
error = %{
token: 42,
error_type: "issue_inprogress_noassignees",
actions: [
%{
comment: "test comment",
url: ""
},
]
}
assert :ok == Dwylbot.MergeErrors.send_error(error)
Process.sleep(1000)
end

end
25 changes: 17 additions & 8 deletions web/controllers/processes/merge_errors.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,41 @@ defmodule Dwylbot.MergeErrors do
use GenServer

@github_api Application.get_env(:dwylbot, :github_api)
@time Application.get_env(:dwylbot, :time_merge_errors)

def start_link(errors) do
GenServer.start_link(__MODULE__, errors, name: __MODULE__)
end

def init(errors) do
Process.send_after(self(), :errors, 1000)
process_errors()
{:ok, errors}
end

def handle_info(:errors, errors) do
GenServer.cast(self(), {:process_errors, errors})
Process.send_after(self(), :errors, 5000)
def process_errors do
Process.send(__MODULE__, :errors, [])
end

def send_error(error) do
GenServer.cast(__MODULE__, {:error, error})
end

def handle_info(:errors, errors_state) do
GenServer.cast(__MODULE__, {:report_errors, errors_state})
Process.send_after(__MODULE__, :errors, @time)
{:noreply, []}
end

def handle_cast({:process_errors, errors}, state) do
def handle_cast({:report_errors, errors}, errors_state) do
errors
|> Enum.each(fn(err) ->
@github_api.report_error(err.token, err)
end)

{:noreply, state}
{:noreply, errors_state}
end

def handle_cast({:error, error}, errors) do
{:noreply, [error | errors]}
def handle_cast({:error, error}, errors_state) do
{:noreply, [error | errors_state]}
end
end
6 changes: 3 additions & 3 deletions web/controllers/processes/wait.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Dwylbot.WaitProcess do
@moduledoc """
The delay function is used to create new checking rules processes
"""
use GenServer
alias Dwylbot.MergeErrors
alias Dwylbot.Rules

def delay(error, payload, event_type, token) do
Expand All @@ -12,9 +12,9 @@ defmodule Dwylbot.WaitProcess do
if error.verify do
check_errors = Rules.check_errors(payload, event_type, token)
Rules.any_error?(check_errors, error)
&& GenServer.cast(Dwylbot.MergeErrors, {:error, error_token})
&& MergeErrors.send_error(error_token)
else
GenServer.cast(Dwylbot.MergeErrors, {:error, error_token})
MergeErrors.send_error(error_token)
end
end
end
12 changes: 12 additions & 0 deletions web/controllers/rules/join_rules.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# defmodule Dwylbot.JoinRules do
# @moduledoc """
# join rules together when necesssary
# """
# def join(rules) do
# # rules is a list of errors with action
#
# # group by apply to
#
# # for each group merge same rule
# end
# end

0 comments on commit 5df7589

Please sign in to comment.