-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from mbta/gg-1202207406750018-worker-for-sched…
…ule-dmap [elixir] feat: schedule dmap worker
- Loading branch information
Showing
6 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
ex_cubic_ingestion/lib/ex_cubic_ingestion/workers/schedule_dmap.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule ExCubicIngestion.Workers.ScheduleDmap do | ||
@moduledoc """ | ||
This worker will be run daily, and it will queue up fetch DMAP jobs for | ||
each active feed. | ||
""" | ||
|
||
use Oban.Worker, | ||
queue: :schedule_dmap, | ||
max_attempts: 1 | ||
|
||
alias ExCubicIngestion.Repo | ||
alias ExCubicIngestion.Schema.CubicDmapFeed | ||
alias ExCubicIngestion.Workers.FetchDmap | ||
|
||
require Oban | ||
require Oban.Job | ||
|
||
@impl Oban.Worker | ||
def perform(_job) do | ||
Repo.transaction(fn -> | ||
Enum.each(CubicDmapFeed.all(), &Oban.insert(FetchDmap.new(%{feed_id: &1.id}))) | ||
end) | ||
|
||
:ok | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
ex_cubic_ingestion/priv/repo/migrations/20220607181113_add_use_transaction_location_dmap.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule ExCubicIngestion.Repo.Migrations.AddUseTransactionDmap do | ||
use Ecto.Migration | ||
|
||
alias ExCubicIngestion.Repo | ||
alias ExCubicIngestion.Schema.CubicDmapFeed | ||
alias ExCubicIngestion.Schema.CubicTable | ||
|
||
def up do | ||
create unique_index("cubic_dmap_feeds", :relative_url) | ||
|
||
Repo.insert!(%CubicDmapFeed{ | ||
relative_url: "/controlledresearchusersapi/transactional/use_transaction_location", | ||
last_updated_at: ~U[2022-06-01 00:00:00.000000Z] | ||
}) | ||
|
||
Repo.insert!(%CubicTable{ | ||
name: "cubic_dmap__use_transaction_location", | ||
s3_prefix: "cubic/dmap/use_transaction_location/" | ||
}) | ||
end | ||
|
||
def down do | ||
Repo.delete!(CubicTable.get_by!( | ||
name: "cubic_dmap__use_transaction_location" | ||
)) | ||
|
||
Repo.delete!(CubicDmapFeed.get_by!( | ||
relative_url: "/controlledresearchusersapi/transactional/use_transaction_location" | ||
)) | ||
|
||
drop index("cubic_dmap_feeds", [:relative_url]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
ex_cubic_ingestion/test/ex_cubic_ingestion/workers/schedule_dmap_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule ExCubicIngestion.Workers.ScheduleDmapTest do | ||
use ExCubicIngestion.DataCase, async: true | ||
use Oban.Testing, repo: ExCubicIngestion.Repo | ||
|
||
alias ExCubicIngestion.Schema.CubicDmapFeed | ||
alias ExCubicIngestion.Workers.FetchDmap | ||
alias ExCubicIngestion.Workers.ScheduleDmap | ||
|
||
describe "perform/1" do | ||
test "run job without error" do | ||
Repo.insert!(%CubicDmapFeed{ | ||
relative_url: "/controlledresearchusersapi/sample1" | ||
}) | ||
|
||
assert :ok == perform_job(ScheduleDmap, %{}) | ||
end | ||
|
||
test "fetch dmap jobs are queued" do | ||
dmap_feed_1 = | ||
Repo.insert!(%CubicDmapFeed{ | ||
relative_url: "/controlledresearchusersapi/sample1" | ||
}) | ||
|
||
dmap_feed_2 = | ||
Repo.insert!(%CubicDmapFeed{ | ||
relative_url: "/controlledresearchusersapi/sample2" | ||
}) | ||
|
||
dmap_feed_deleted = | ||
Repo.insert!(%CubicDmapFeed{ | ||
relative_url: "/deleted", | ||
deleted_at: ~U[2022-05-01 10:49:50Z] | ||
}) | ||
|
||
:ok = perform_job(ScheduleDmap, %{}) | ||
|
||
assert_enqueued(worker: FetchDmap, args: %{feed_id: dmap_feed_1.id}) | ||
|
||
assert_enqueued(worker: FetchDmap, args: %{feed_id: dmap_feed_2.id}) | ||
|
||
refute_enqueued(worker: FetchDmap, args: %{feed_id: dmap_feed_deleted.id}) | ||
end | ||
end | ||
end |