Skip to content

Instantly share code, notes, and snippets.

View sorentwo's full-sized avatar
🏡

Parker Selbert sorentwo

🏡
View GitHub Profile
@sorentwo
sorentwo / simple_migration.exs
Created September 2, 2024 16:47
A simplified Oban migration that's compatible with CockroachDB
execute """
CREATE TYPE oban_job_state AS ENUM (
'available',
'scheduled',
'executing',
'retryable',
'completed',
'cancelled',
'discarded'
)
@sorentwo
sorentwo / count_operations.exs
Created January 16, 2024 18:10
Benchmark for counting queries and transactions with Oban's Basic and Smart engines
defmodule Oban.Pro.Repo do
use Ecto.Repo, otp_app: :oban_pro, adapter: Ecto.Adapters.Postgres
end
Application.ensure_all_started(:postgrex)
Oban.Pro.Repo.start_link()
defmodule BenchWorker do
@moduledoc false
@sorentwo
sorentwo / gigalixir.ex
Created January 12, 2024 11:51
Gigalixir Cloud for Oban Auto Scaling
defmodule Oban.Pro.Clouds.Gigalixir do
@moduledoc """
Cloud scaler for Gigalixir.
## Usage
```elixir
alias Oban.Pro.DynamicScaler
alias Oban.Pro.Clouds.Gigalixir
@sorentwo
sorentwo / ec2_asg.ex
Created March 19, 2023 20:57
EC2 ASG Cloud for Oban Auto Scaling
defmodule Oban.Pro.Clouds.EC2ASG do
@behaviour Oban.Pro.Cloud
@impl Oban.Pro.Cloud
def init(opts), do: Map.new(opts)
@impl Oban.Pro.Cloud
def scale(desired, conf) do
params = %{
"Action" => "SetDesiredCapacity",
@sorentwo
sorentwo / heroku.ex
Last active March 19, 2023 20:54
Heroku Cloud for Oban Auto Scaling
defmodule Oban.Pro.Clouds.Heroku do
@moduledoc false
@behaviour Oban.Pro.Cloud
@enforce_keys [:app, :auth_token, :dyno]
defstruct @enforce_keys
@impl Oban.Pro.Cloud
def init(opts) do
@sorentwo
sorentwo / fly.ex
Last active August 13, 2024 14:27
Fly Cloud for Oban Auto Scaling
defmodule Oban.Pro.Clouds.Fly do
@moduledoc false
@behaviour Oban.Pro.Cloud
@enforce_keys [:app_id, :auth_token, :regions]
defstruct @enforce_keys
@url "https://api.fly.io/graphql"
@sorentwo
sorentwo / dependabot.yml
Created November 29, 2022 15:38
Dependabot example for Oban Web+Pro
version: 2
updates:
- package-ecosystem: mix
directory: "/"
schedule:
interval: daily
time: "11:00"
open-pull-requests-limit: 10
insecure-external-code-execution: allow
registries:
@sorentwo
sorentwo / oban_from_rails.md
Created August 22, 2022 20:02
Guide for inserting Oban jobs from Rails

Migrating background jobs to Elixir is easy with Oban because everything lives in your PostgreSQL database. Oban relies on a structured oban_jobs table as its job queue, and purposefully uses portable data structures (JSON) for serialization. That makes it enqueueing jobs into Oban simple for any language with a PostgreSQL adapter, no Oban client necessary.

As a demonstration, let's explore inserting Oban jobs from a Rails application.

To start, define a skeletal ActiveRecord model with a few conveniences for scheduling jobs:

@sorentwo
sorentwo / queue_breaker.ex
Last active August 2, 2022 14:33
Oban Queue Circuit Breaker
defmodule QueueBreaker do
@moduledoc """
Automatic queue pausing/resuming based on accumulated errors.
## Example
Pause after 1000 errors:
QueueBreaker.attach("risky-queue", 1000)
"""
@sorentwo
sorentwo / start_oban_web.livemd
Created July 27, 2022 16:38
Single Page Oban Web

Single Page Oban Web

Application.put_env(:sample, Sample.Repo, database: "oban_dev")

Application.put_env(:phoenix, :json_library, Jason)

Application.put_env(:sample, Sample.Endpoint,
  http: [ip: {127, 0, 0, 1}, port: 5001],
  server: true,