Skip to content

Commit ea7b49c

Browse files
authored
Merge pull request #1184 from code-corps/add-processor
Add processor that switches between sync/async
2 parents bc690a3 + ce9efc1 commit ea7b49c

File tree

26 files changed

+62
-109
lines changed

26 files changed

+62
-109
lines changed

config/config.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ config :sentry,
8282
included_environments: ~w(prod staging)a,
8383
use_error_logger: true
8484

85+
config :code_corps, :processor, CodeCorps.Processor.Async
86+
8587
# Import environment specific config. This must remain at the bottom
8688
# of this file so it overrides the configuration defined above.
8789
import_config "#{Mix.env}.exs"

config/test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ config :code_corps,
5757
config :sentry,
5858
environment_name: Mix.env || :test
5959

60+
config :code_corps, :processor, CodeCorps.Processor.Sync
61+
6062
config :code_corps, CodeCorps.Mailer,
6163
adapter: Bamboo.TestAdapter
6264

lib/code_corps/accounts/accounts.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ defmodule CodeCorps.Accounts do
55
All actions to accounts should go through here.
66
"""
77

8-
alias Task.Supervisor, as: TaskSupervisor
98
alias CodeCorps.{
109
Accounts.Changesets,
1110
Comment,
1211
GitHub.Adapters,
1312
GithubAppInstallation,
1413
GithubUser,
14+
Processor,
1515
Task,
1616
User,
1717
Repo
@@ -124,7 +124,7 @@ defmodule CodeCorps.Accounts do
124124
end
125125

126126
defp upload_github_photo_async(%User{cloudinary_public_id: nil} = user) do
127-
TaskSupervisor.start_child(:background_processor, fn -> upload_github_photo(user) end)
127+
Processor.process(fn -> upload_github_photo(user) end)
128128
end
129129
defp upload_github_photo_async(%User{} = user), do: user
130130

lib/code_corps/github/webhook/processor.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule CodeCorps.GitHub.Webhook.Processor do
55
Can process them synchronously or asynchronously.
66
"""
77

8-
alias CodeCorps.GitHub.Webhook.Handler
8+
alias CodeCorps.{GitHub.Webhook.Handler, Processor}
99

1010
@doc """
1111
Used to process a Github webhook event in an async manner.
@@ -15,7 +15,7 @@ defmodule CodeCorps.GitHub.Webhook.Processor do
1515
Returns `{:ok, pid}`
1616
"""
1717
def process_async(type, id, payload) do
18-
Task.Supervisor.start_child(:background_processor, fn -> process(type, id, payload) end)
18+
Processor.process(fn -> process(type, id, payload) end)
1919
end
2020

2121
defdelegate process(type, id, payload), to: Handler, as: :handle

lib/code_corps/processor/async.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule CodeCorps.Processor.Async do
2+
@behaviour CodeCorps.Processor
3+
4+
def process(fun) do
5+
Task.Supervisor.start_child(:background_processor, fn ->
6+
apply(fun, [])
7+
end)
8+
end
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule CodeCorps.Processor do
2+
@processor Application.get_env(:code_corps, :processor)
3+
4+
@callback process(fun :: (() -> any)) :: any
5+
6+
def process(fun) do
7+
@processor.process(fun)
8+
end
9+
end

lib/code_corps/processor/sync.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule CodeCorps.Processor.Sync do
2+
@behaviour CodeCorps.Processor
3+
4+
def process(fun) do
5+
apply(fun, [])
6+
end
7+
end

lib/code_corps/stripe_service/webhook_processing/webhook_processor.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule CodeCorps.StripeService.WebhookProcessing.WebhookProcessor do
33
Used to process a Stripe webhook request.
44
"""
55

6-
alias CodeCorps.StripeService.WebhookProcessing.EventHandler
6+
alias CodeCorps.{Processor, StripeService.WebhookProcessing.EventHandler}
77

88
@api Application.get_env(:code_corps, :stripe)
99

@@ -18,7 +18,7 @@ defmodule CodeCorps.StripeService.WebhookProcessing.WebhookProcessor do
1818
Returns `{:ok, pid}`
1919
"""
2020
def process_async(event_params, handler) do
21-
Task.Supervisor.start_child(:background_processor, fn -> process(event_params, handler) end)
21+
Processor.process(fn -> process(event_params, handler) end)
2222
end
2323

2424
@doc """

lib/code_corps_web/controllers/project_github_repo_controller.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule CodeCorpsWeb.ProjectGithubRepoController do
33
use CodeCorpsWeb, :controller
44

55
alias Task.Supervisor, as: TaskSupervisor
6-
alias CodeCorps.{Analytics.SegmentTracker, ProjectGithubRepo, User, Helpers.Query}
6+
alias CodeCorps.{Analytics.SegmentTracker, Processor, ProjectGithubRepo, User, Helpers.Query}
77

88
action_fallback CodeCorpsWeb.FallbackController
99
plug CodeCorpsWeb.Plug.DataToAttributes
@@ -29,7 +29,7 @@ defmodule CodeCorpsWeb.ProjectGithubRepoController do
2929
{:ok, :authorized} <- current_user |> Policy.authorize(:create, %ProjectGithubRepo{}, params),
3030
{:ok, %ProjectGithubRepo{} = project_github_repo} <- create_project_repo_changeset(params) |> Repo.insert do
3131

32-
TaskSupervisor.start_child(:background_processor, fn ->
32+
Processor.process(fn ->
3333
CodeCorps.GitHub.Sync.sync_project_github_repo(project_github_repo)
3434
end)
3535

mix.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [], []},
1616
"credo": {:hex, :credo, "0.8.6", "335f723772d35da499b5ebfdaf6b426bfb73590b6fcbc8908d476b75f8cbca3f", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, optional: false]}]},
1717
"db_connection": {:hex, :db_connection, "1.1.2", "2865c2a4bae0714e2213a0ce60a1b12d76a6efba0c51fbda59c9ab8d1accc7a8", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]},
18-
"decimal": {:hex, :decimal, "1.4.0", "fac965ce71a46aab53d3a6ce45662806bdd708a4a95a65cde8a12eb0124a1333", [:mix], []},
18+
"decimal": {:hex, :decimal, "1.4.0", "fac965ce71a46aab53d3a6ce45662806bdd708a4a95a65cde8a12eb0124a1333", [:mix], [], "hexpm"},
1919
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], []},
2020
"earmark": {:hex, :earmark, "1.2.3", "206eb2e2ac1a794aa5256f3982de7a76bf4579ff91cb28d0e17ea2c9491e46a4", [:mix], []},
21-
"ecto": {:hex, :ecto, "2.2.2", "e9bd6ebc044eaaab1cb369e3465686d8aca830aa5bf545ef2bae000a3d42c54b", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]},
21+
"ecto": {:hex, :ecto, "2.2.2", "e9bd6ebc044eaaab1cb369e3465686d8aca830aa5bf545ef2bae000a3d42c54b", [:mix], [{:db_connection, "~> 1.1", [repo: "hexpm", hex: :db_connection, optional: true]}, {:decimal, "~> 1.2", [repo: "hexpm", hex: :decimal, optional: false]}, {:mariaex, "~> 0.8.0", [repo: "hexpm", hex: :mariaex, optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [repo: "hexpm", hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [repo: "hexpm", hex: :poolboy, optional: false]}, {:postgrex, "~> 0.13.0", [repo: "hexpm", hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0", [repo: "hexpm", hex: :sbroker, optional: true]}], "hexpm"},
2222
"ecto_ordered": {:hex, :ecto_ordered, "0.2.0-beta1", "cb066bc608f1c8913cea85af8293261720e6a88e3c99061e6877d7025352f045", [:mix], [{:ecto, "~> 2.0", [hex: :ecto, optional: false]}]},
2323
"elixir_make": {:hex, :elixir_make, "0.4.0", "992f38fabe705bb45821a728f20914c554b276838433349d4f2341f7a687cddf", [:mix], []},
2424
"ex_aws": {:hex, :ex_aws, "1.1.4", "4bdc4fff91f8d35c7fe2355b9da54cc51f980c92f1137715d8b2d70d8e8511cc", [:mix], [{:configparser_ex, "~> 0.2.1", [hex: :configparser_ex, optional: true]}, {:hackney, "1.6.3 or 1.6.5 or 1.7.1 or 1.8.6 or ~> 1.9", [hex: :hackney, optional: true]}, {:jsx, "~> 2.8", [hex: :jsx, optional: true]}, {:poison, ">= 1.2.0", [hex: :poison, optional: true]}, {:sweet_xml, "~> 0.6", [hex: :sweet_xml, optional: true]}, {:xml_builder, "~> 0.1.0", [hex: :xml_builder, optional: true]}]},
@@ -44,8 +44,8 @@
4444
"money": {:hex, :money, "1.2.1", "fdcc7b021b894dbcc2cd0f57d2ccdd2224eced747231337a849424ffaa196b13", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 2.1", [hex: :ecto, optional: true]}, {:phoenix_html, "~> 2.0", [hex: :phoenix_html, optional: true]}]},
4545
"msgpax": {:hex, :msgpax, "1.1.0", "e31625e256db2decca1ae2b841f21b4d2483b1332649ce3ebc96c7ff7a4986e3", [:mix], [{:plug, "~> 1.0", [hex: :plug, optional: true]}]},
4646
"phoenix": {:hex, :phoenix, "1.3.0", "1c01124caa1b4a7af46f2050ff11b267baa3edb441b45dbf243e979cd4c5891b", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.3.3 or ~> 1.4", [hex: :plug, optional: false]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, optional: false]}]},
47-
"phoenix_ecto": {:hex, :phoenix_ecto, "3.2.3", "450c749876ff1de4a78fdb305a142a76817c77a1cd79aeca29e5fc9a6c630b26", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, optional: true]}, {:plug, "~> 1.0", [hex: :plug, optional: false]}]},
48-
"phoenix_html": {:hex, :phoenix_html, "2.10.4", "d4f99c32d5dc4918b531fdf163e1fd7cf20acdd7703f16f5d02d4db36de803b7", [:mix], [{:plug, "~> 1.0", [hex: :plug, optional: false]}]},
47+
"phoenix_ecto": {:hex, :phoenix_ecto, "3.2.3", "450c749876ff1de4a78fdb305a142a76817c77a1cd79aeca29e5fc9a6c630b26", [:mix], [{:ecto, "~> 2.1", [repo: "hexpm", hex: :ecto, optional: false]}, {:phoenix_html, "~> 2.9", [repo: "hexpm", hex: :phoenix_html, optional: true]}, {:plug, "~> 1.0", [repo: "hexpm", hex: :plug, optional: false]}], "hexpm"},
48+
"phoenix_html": {:hex, :phoenix_html, "2.10.4", "d4f99c32d5dc4918b531fdf163e1fd7cf20acdd7703f16f5d02d4db36de803b7", [:mix], [{:plug, "~> 1.0", [repo: "hexpm", hex: :plug, optional: false]}], "hexpm"},
4949
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.0.8", "4333f9c74190f485a74866beff2f9304f069d53f047f5fbb0fb8d1ee4c495f73", [:mix], [{:fs, "~> 0.9.1", [hex: :fs, optional: false]}, {:phoenix, "~> 1.0 or ~> 1.2-rc", [hex: :phoenix, optional: false]}]},
5050
"phoenix_pubsub": {:hex, :phoenix_pubsub, "1.0.2", "bfa7fd52788b5eaa09cb51ff9fcad1d9edfeb68251add458523f839392f034c1", [:mix], []},
5151
"plug": {:hex, :plug, "1.4.3", "236d77ce7bf3e3a2668dc0d32a9b6f1f9b1f05361019946aae49874904be4aed", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, optional: true]}, {:mime, "~> 1.0", [hex: :mime, optional: false]}]},

0 commit comments

Comments
 (0)