Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit e7742ee

Browse files
committed
chore: Merge branch 'async-job' into dev
2 parents 606618c + 1ff7354 commit e7742ee

File tree

9 files changed

+106
-41
lines changed

9 files changed

+106
-41
lines changed

config/config.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ config :groupher_server, GroupherServer.Mailer,
7575
adapter: Bamboo.MailgunAdapter,
7676
domain: "mailer.coderplanets.com"
7777

78+
# handle background jobs
79+
config :rihanna,
80+
jobs_table_name: "background_jobs",
81+
producer_postgres_connection: {Ecto, GroupherServer.Repo}
82+
7883
import_config "#{Mix.env()}.exs"
7984

8085
if File.exists?("config/#{Mix.env()}.secret.exs") do

lib/groupher_server/application.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ defmodule GroupherServer.Application do
3030
options: []
3131
)
3232
]
33-
])
33+
]),
34+
{Rihanna.Supervisor, [postgrex: GroupherServer.Repo.config()]}
3435
]
3536

3637
# See https://hexdocs.pm/elixir/Supervisor.html

lib/groupher_server/statistics/delegates/contribute.ex

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
defmodule GroupherServer.Statistics.Delegate.Contribute do
2+
@moduledoc """
3+
contribute statistics for user and community, record how many content
4+
has been add to it
5+
"""
26
import Ecto.Query, warn: false
37
import Helper.Utils
48
import ShortMaps
59

6-
alias GroupherServer.Repo
7-
alias GroupherServer.Accounts.User
8-
alias GroupherServer.CMS.Community
9-
alias GroupherServer.Statistics.{UserContribute, CommunityContribute}
10-
alias Helper.{Cache, ORM, QueryBuilder}
10+
alias GroupherServer.{Accounts, CMS, Repo, Statistics}
11+
12+
alias Accounts.User
13+
alias CMS.Community
14+
alias Statistics.{CommunityContribute, UserContribute}
15+
16+
alias Helper.{Cache, Later, ORM, QueryBuilder}
1117

1218
@community_contribute_days get_config(:general, :community_contribute_days)
1319
@user_contribute_months get_config(:general, :user_contribute_months)
1420

1521
@doc """
1622
update user's contributes record
1723
"""
18-
def make_contribute(%User{id: id}) do
24+
def make_contribute(%User{id: id} = user) do
1925
today = Timex.today() |> Date.to_iso8601()
2026

2127
case ORM.find_by(UserContribute, user_id: id, date: today) do
2228
{:ok, contribute} ->
23-
contribute |> inc_contribute_count(:user) |> done
29+
update_contribute_record(contribute)
2430

2531
{:error, _} ->
26-
UserContribute |> ORM.create(%{user_id: id, date: today, count: 1})
32+
insert_contribute_record(user)
2733
end
2834
end
2935

@@ -35,20 +41,10 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
3541

3642
case ORM.find_by(CommunityContribute, community_id: id, date: today) do
3743
{:ok, contribute} ->
38-
result = contribute |> inc_contribute_count(:community) |> done
39-
40-
# TODO: move to background job
41-
list_contributes_digest(%Community{id: id})
42-
result
44+
update_contribute_record(contribute)
4345

4446
{:error, _} ->
45-
result =
46-
CommunityContribute
47-
|> ORM.create(%{community_id: id, date: today, count: 1})
48-
49-
# TODO: move to background job
50-
list_contributes_digest(%Community{id: id})
51-
result
47+
insert_contribute_record(%Community{id: id})
5248
end
5349
end
5450

@@ -78,26 +74,52 @@ defmodule GroupherServer.Statistics.Delegate.Contribute do
7874
{:ok, result}
7975

8076
{:error, _} ->
81-
%Community{id: id}
82-
|> get_contributes()
83-
|> to_counts_digest(days: @community_contribute_days)
84-
|> done
85-
|> cache_result(scope)
77+
get_contributes_then_cache(%Community{id: id})
8678
end
8779
end
8880

89-
# TODO: mv to helper/cache, also 规范一下 scope
90-
defp cache_result({:ok, result}, scope) do
91-
Cache.put(scope, result)
92-
{:ok, result}
81+
# NOTE* must be public, cause it will be exec by background job
82+
def get_contributes_then_cache(%Community{id: id}) do
83+
scope = Cache.get_scope(:community_contributes, id)
84+
85+
%Community{id: id}
86+
|> do_get_contributes()
87+
|> to_counts_digest(days: @community_contribute_days)
88+
|> done_and_cache(scope)
89+
end
90+
91+
defp update_contribute_record(%UserContribute{} = contribute) do
92+
contribute |> inc_contribute_count(:user) |> done
93+
end
94+
95+
defp insert_contribute_record(%User{id: id}) do
96+
today = Timex.today() |> Date.to_iso8601()
97+
98+
UserContribute |> ORM.create(%{user_id: id, date: today, count: 1})
99+
end
100+
101+
defp update_contribute_record(%CommunityContribute{community_id: community_id} = contribute) do
102+
with {:ok, result} <- inc_contribute_count(contribute, :community) do
103+
cache_contribute_later(%Community{id: community_id})
104+
{:ok, result}
105+
end
106+
end
107+
108+
defp insert_contribute_record(%Community{id: id}) do
109+
today = Timex.today() |> Date.to_iso8601()
110+
111+
with {:ok, result} <-
112+
ORM.create(CommunityContribute, %{community_id: id, date: today, count: 1}) do
113+
cache_contribute_later(%Community{id: id})
114+
{:ok, result}
115+
end
93116
end
94117

95-
# TODO: mv to helper/cache, also 规范一下 scope
96-
defp cache_result({:error, result}, _scope) do
97-
{:error, result}
118+
defp cache_contribute_later(%Community{id: id}) do
119+
Later.exec({__MODULE__, :get_contributes_then_cache, [%Community{id: id}]})
98120
end
99121

100-
defp get_contributes(%Community{id: id}) do
122+
defp do_get_contributes(%Community{id: id}) do
101123
community_id = integerfy(id)
102124

103125
CommunityContribute

lib/helper/later.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule Helper.Later do
2+
@moduledoc """
3+
background jobs support, currently using https://github.com/samphilipd/rihanna
4+
"""
5+
6+
@doc """
7+
## Example
8+
iex> Later.exec({__MODULE__, :get_contributes_then_cache, [%Community{id: id}]})
9+
{:ok, _}
10+
"""
11+
def exec({mod, func, args}) do
12+
Rihanna.enqueue({mod, func, args})
13+
end
14+
end

lib/helper/utils.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ defmodule Helper.Utils do
66
import Helper.ErrorHandler
77
import Helper.ErrorCode
88

9+
alias Helper.Cache
10+
911
def get_config(section, key, app \\ :groupher_server)
1012

1113
def get_config(section, :all, app) do
@@ -50,6 +52,13 @@ defmodule Helper.Utils do
5052
# def done({:error, error}), do: {:error, error}
5153
def done(result), do: {:ok, result}
5254

55+
def done_and_cache(result, scope) do
56+
with {:ok, res} <- done(result) do
57+
Cache.put(scope, res)
58+
{:ok, res}
59+
end
60+
end
61+
5362
@doc """
5463
see: https://hexdocs.pm/absinthe/errors.html#content for error format
5564
"""

mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ defmodule GroupherServer.Mixfile do
9797
{:recase, "~> 0.4.0"},
9898
{:nanoid, "~> 2.0.0"},
9999
{:bamboo, "1.2.0"},
100-
{:cachex, "3.1.3"}
100+
{:cachex, "3.1.3"},
101+
{:rihanna, "1.3.5"}
101102
]
102103
end
103104

mix.lock

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
1515
"cors_plug": {:hex, :cors_plug, "1.5.0", "6311ea6ac9fb78b987df52a7654136626a7a0c3b77f83da265f952a24f2fc1b0", [], [{:plug, "~> 1.4", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
1616
"corsica": {:hex, :corsica, "1.1.2", "5ad8b9dcbeeda4762d78a57c0c8c2f88e1eef8741508517c98cb79e0db1f107d", [:mix], [{:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
17-
"cowboy": {:hex, :cowboy, "2.6.3", "99aa50e94e685557cad82e704457336a453d4abcb77839ad22dbe71f311fcc06", [], [{:cowlib, "~> 2.7.3", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
18-
"cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [], [], "hexpm"},
17+
"cowboy": {:hex, :cowboy, "2.6.3", "99aa50e94e685557cad82e704457336a453d4abcb77839ad22dbe71f311fcc06", [:rebar3], [{:cowlib, "~> 2.7.3", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
18+
"cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [:rebar3], [], "hexpm"},
1919
"credo": {:hex, :credo, "1.1.0", "e0c07b2fd7e2109495f582430a1bc96b2c71b7d94c59dfad120529f65f19872f", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
2020
"dataloader": {:hex, :dataloader, "1.0.6", "fb724d6d3fb6acb87d27e3b32dea3a307936ad2d245faf9cf5221d1323d6a4ba", [:mix], [{:ecto, ">= 0.0.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm"},
2121
"db_connection": {:hex, :db_connection, "2.1.0", "122e2f62c4906bf2e49554f1e64db5030c19229aa40935f33088e7d543aa79d0", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},
@@ -59,6 +59,7 @@
5959
"pre_commit": {:hex, :pre_commit, "0.3.4", "e2850f80be8090d50ad8019ef2426039307ff5dfbe70c736ad0d4d401facf304", [:mix], [], "hexpm"},
6060
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"},
6161
"recase": {:hex, :recase, "0.4.0", "8fb52846f75948156385af2dfdc12f69e5ce27b022a9d1682c70a2fb3ed149c7", [:mix], [], "hexpm"},
62+
"rihanna": {:hex, :rihanna, "1.3.5", "5f5e6c5b1e514978a29a6791f338f4bb963401959fc212bd18d4a2c92d79a7a4", [:mix], [{:ecto, ">= 2.0.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_sql, ">= 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.13.3", [hex: :postgrex, repo: "hexpm", optional: false]}], "hexpm"},
6263
"scrivener": {:hex, :scrivener, "2.5.0", "e1f78c62b6806d91cc9c4778deef1ea4e80aa9fadfce2c16831afe0468cc8a2c", [:mix], [], "hexpm"},
6364
"scrivener_ecto": {:git, "https://github.com/mastani-stack/scrivener_ecto", "a6ebbbe9585e9829219ed0f73a3afcb4edde3558", [branch: "dev"]},
6465
"sentry": {:hex, :sentry, "6.4.1", "882287f1f3167dc4794865124977e2d88878d51d19930c0d0e7cc3a663a4a181", [:mix], [{:hackney, "~> 1.8 or 1.6.5", [hex: :hackney, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.3", [hex: :phoenix, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: true]}, {:poison, "~> 1.5 or ~> 2.0 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
defmodule GroupherServer.Repo.Migrations.CreateBackgroundJobs do
2+
use Rihanna.Migration
3+
end

test/groupher_server/statistics/statistics_test.exs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule GroupherServer.Test.Statistics do
33

44
import Helper.Utils, only: [get_config: 2]
55

6-
alias Helper.ORM
6+
alias Helper.{Cache, Later, ORM}
77
alias GroupherServer.Accounts.User
88
alias GroupherServer.{CMS, Repo, Statistics}
99

@@ -148,7 +148,6 @@ defmodule GroupherServer.Test.Statistics do
148148
assert length(contributes) == @community_contribute_days + 1
149149
end
150150

151-
alias Helper.Cache
152151
@tag :wip
153152
test "the contributes data should be cached after first query", ~m(community)a do
154153
scope = Cache.get_scope(:community_contributes, community.id)
@@ -159,14 +158,24 @@ defmodule GroupherServer.Test.Statistics do
159158
assert {:ok, contributes} = Cache.get(scope)
160159
end
161160

162-
@tag :wip
161+
@tag :wip2
162+
test "Rihanna should work in test sandbox", ~m(community)a do
163+
res = Rihanna.enqueue({IO, :puts, ["Work, work, work, work, work."]})
164+
Process.sleep(1000)
165+
# IO.inspect(res, label: "res")
166+
end
167+
163168
test "cache should be update after make contributes", ~m(community)a do
164169
scope = Cache.get_scope(:community_contributes, community.id)
165170
assert {:error, nil} = Cache.get(scope)
166171

167172
Statistics.make_contribute(%Community{id: community.id})
168173

169-
assert {:ok, _} = Cache.get(scope)
174+
# res = Later.exec({IO, :puts, ["Work, work, work, work, work."]})
175+
# Process.sleep(1000)
176+
# IO.inspect(res, label: "res")
177+
178+
# assert {:ok, _} = Cache.get(scope)
170179
end
171180
end
172181
end

0 commit comments

Comments
 (0)