Divide up refreshing credits and debits

pull/42/head
Doc Ritezel 7 years ago
parent fc513ccb91
commit 668098821a
  1. 23
      lib/explorer/workers/refresh_balance.ex
  2. 26
      test/explorer/workers/refresh_balance_test.exs

@ -3,15 +3,30 @@ defmodule Explorer.Workers.RefreshBalance do
Refreshes the Credit and Debit balance views.
"""
alias Ecto.Adapters.SQL
alias Explorer.Credit
alias Explorer.Debit
alias Explorer.Repo
def perform("credit"), do: unless refreshing("credits"), do: Credit.refresh()
def perform("debit"), do: unless refreshing("debits"), do: Debit.refresh()
def perform do
Credit.refresh()
Debit.refresh()
perform_later("credit")
perform_later("debit")
end
def perform_later(args \\ []) do
Exq.enqueue(Exq.Enqueuer, "default", __MODULE__, args)
end
def perform_later do
Exq.enqueue(Exq.Enqueuer, "default", __MODULE__, [])
def refreshing(table) do
query = "REFRESH MATERIALIZED VIEW CONCURRENTLY #{table}%"
result = SQL.query!(
Repo,
"SELECT TRUE FROM pg_stat_activity WHERE query ILIKE '$#{query}'",
[]
)
Enum.count(result.rows) > 0
end
end

@ -1,25 +1,31 @@
defmodule Explorer.Workers.RefreshBalanceTest do
use Explorer.DataCase
import Mock
alias Explorer.Credit
alias Explorer.Debit
alias Explorer.Workers.RefreshBalance
describe "perform/1" do
test "refreshes credit balances" do
address = insert(:address)
transaction = insert(:transaction, value: 20)
insert(:to_address, address: address, transaction: transaction)
RefreshBalance.perform
assert Repo.one(Credit).value == Decimal.new(20)
with_mock Exq, [enqueue: fn (_, _, _, _) -> Credit.refresh() end] do
address = insert(:address)
transaction = insert(:transaction, value: 20)
insert(:to_address, address: address, transaction: transaction)
RefreshBalance.perform
assert Repo.one(Credit).value == Decimal.new(20)
end
end
test "refreshes debit balances" do
address = insert(:address)
transaction = insert(:transaction, value: 20)
insert(:from_address, address: address, transaction: transaction)
RefreshBalance.perform
assert Repo.one(Debit).value == Decimal.new(20)
with_mock Exq, [enqueue: fn (_, _, _, _) -> Debit.refresh() end] do
address = insert(:address)
transaction = insert(:transaction, value: 20)
insert(:from_address, address: address, transaction: transaction)
RefreshBalance.perform
assert Repo.one(Debit).value == Decimal.new(20)
end
end
end
end

Loading…
Cancel
Save