Skip to content

Commit 316f5c1

Browse files
committed
grab tokens from tokenpool while fetching top contributions
1 parent 49dfa97 commit 316f5c1

File tree

6 files changed

+44
-21
lines changed

6 files changed

+44
-21
lines changed

config/config.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ config :algora, Oban,
5959
activity_discord: 10,
6060
campaign_emails: 1,
6161
fetch_top_contributions: 1,
62-
sync_contribution: 3
62+
sync_contribution: 20
6363
]
6464

6565
# Configures the mailer

lib/algora/integrations/github/token_pool.ex

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ defmodule Algora.Github.TokenPool do
1515
end
1616

1717
def get_token do
18-
GenServer.call(__MODULE__, :get_token)
18+
case maybe_get_token() do
19+
token when is_binary(token) -> token
20+
_ -> get_token()
21+
end
22+
end
23+
24+
def maybe_get_token do
25+
GenServer.call(__MODULE__, :maybe_get_token)
1926
end
2027

2128
def refresh_tokens do
@@ -35,7 +42,7 @@ defmodule Algora.Github.TokenPool do
3542
end
3643

3744
@impl true
38-
def handle_call(:get_token, _from, %{current_token_index: index, tokens: tokens} = state) do
45+
def handle_call(:maybe_get_token, _from, %{current_token_index: index, tokens: tokens} = state) do
3946
token = Enum.at(tokens, index)
4047

4148
if token == nil do
@@ -44,7 +51,16 @@ defmodule Algora.Github.TokenPool do
4451
next_index = rem(index + 1, length(tokens))
4552
if next_index == 0, do: refresh_tokens()
4653

47-
{:reply, token, %{state | current_token_index: next_index}}
54+
case Github.get_current_user(token) do
55+
{:ok, _} ->
56+
dbg(token)
57+
dbg("token is valid")
58+
{:reply, token, %{state | current_token_index: next_index}}
59+
60+
_ ->
61+
dbg("token is invalid")
62+
{:reply, nil, %{state | current_token_index: next_index}}
63+
end
4864
end
4965
end
5066

lib/algora/workspace/jobs/fetch_top_contributions.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ defmodule Algora.Workspace.Jobs.FetchTopContributions do
66
# 30 days
77
unique: [period: 30 * 24 * 60 * 60, fields: [:args]]
88

9+
alias Algora.Github
10+
911
@impl Oban.Worker
1012
def perform(%Oban.Job{args: %{"provider_login" => provider_login}}) do
11-
Algora.Workspace.fetch_top_contributions_async(provider_login)
13+
Algora.Workspace.fetch_top_contributions_async(Github.TokenPool.get_token(), provider_login)
1214
end
1315

1416
def timeout(_), do: :timer.seconds(30)

lib/algora/workspace/jobs/sync_contribution.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ defmodule Algora.Workspace.Jobs.SyncContribution do
66
# 30 days
77
unique: [period: 30 * 24 * 60 * 60, fields: [:args]]
88

9+
alias Algora.Github
910
alias Algora.Workspace
1011

1112
@impl Oban.Worker
1213
def perform(%Oban.Job{
1314
args: %{"user_id" => user_id, "repo_full_name" => repo_full_name, "contribution_count" => contribution_count}
1415
}) do
15-
token = Algora.Admin.token()
16+
token = Github.TokenPool.get_token()
1617

1718
with [repo_owner, repo_name] <- String.split(repo_full_name, "/"),
1819
{:ok, repo} <- Workspace.ensure_repository(token, repo_owner, repo_name),

lib/algora/workspace/workspace.ex

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -654,12 +654,10 @@ defmodule Algora.Workspace do
654654
end
655655
end
656656

657-
def fetch_top_contributions(provider_login) do
658-
token = Algora.Admin.token()
659-
657+
def fetch_top_contributions(token, provider_login) do
660658
with {:ok, contributions} <- Algora.Cloud.top_contributions(provider_login),
661659
{:ok, user} <- ensure_user(token, provider_login),
662-
:ok <- add_contributions(user.id, contributions) do
660+
:ok <- add_contributions(token, user.id, contributions) do
663661
{:ok, contributions}
664662
else
665663
{:error, reason} ->
@@ -668,12 +666,10 @@ defmodule Algora.Workspace do
668666
end
669667
end
670668

671-
def fetch_top_contributions_async(provider_login) do
672-
token = Algora.Admin.token()
673-
669+
def fetch_top_contributions_async(token, provider_login) do
674670
with {:ok, contributions} <- Algora.Cloud.top_contributions(provider_login),
675671
{:ok, user} <- ensure_user(token, provider_login),
676-
{:ok, _} <- add_contributions_async(user.id, contributions) do
672+
{:ok, _} <- add_contributions_async(token, user.id, contributions) do
677673
{:ok, nil}
678674
else
679675
{:error, reason} ->
@@ -682,7 +678,7 @@ defmodule Algora.Workspace do
682678
end
683679
end
684680

685-
def add_contributions_async(user_id, opts) do
681+
def add_contributions_async(_token, user_id, opts) do
686682
Repo.transact(fn ->
687683
Enum.reduce_while(opts, :ok, fn contribution, _ ->
688684
case %{
@@ -699,18 +695,26 @@ defmodule Algora.Workspace do
699695
end)
700696
end
701697

702-
def add_contributions(user_id, opts) do
698+
def add_contributions(token, user_id, opts) do
703699
results =
704700
Enum.map(opts, fn %{repo_name: repo_name, contribution_count: contribution_count} ->
705-
add_contribution(%{user_id: user_id, repo_full_name: repo_name, contribution_count: contribution_count})
701+
add_contribution(%{
702+
token: token,
703+
user_id: user_id,
704+
repo_full_name: repo_name,
705+
contribution_count: contribution_count
706+
})
706707
end)
707708

708709
if Enum.any?(results, fn result -> result == :ok end), do: :ok, else: {:error, :failed}
709710
end
710711

711-
def add_contribution(%{user_id: user_id, repo_full_name: repo_full_name, contribution_count: contribution_count}) do
712-
token = Algora.Admin.token()
713-
712+
def add_contribution(%{
713+
token: token,
714+
user_id: user_id,
715+
repo_full_name: repo_full_name,
716+
contribution_count: contribution_count
717+
}) do
714718
with [repo_owner, repo_name] <- String.split(repo_full_name, "/"),
715719
{:ok, repo} <- ensure_repository(token, repo_owner, repo_name),
716720
{:ok, _tech_stack} <- ensure_repo_tech_stack(token, repo),

lib/algora_web/live/org/job_live.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ defmodule AlgoraWeb.Org.JobLive do
731731
fn handle ->
732732
broadcast(socket.assigns.job, {:contributions_fetching, handle})
733733

734-
case Algora.Workspace.fetch_top_contributions(handle) do
734+
case Algora.Workspace.fetch_top_contributions(Algora.Admin.token(), handle) do
735735
{:ok, contributions} -> broadcast(socket.assigns.job, {:contributions_fetched, handle, contributions})
736736
{:error, _reason} -> broadcast(socket.assigns.job, {:contributions_failed, handle})
737737
end

0 commit comments

Comments
 (0)