oban-bg / oban

πŸ’Ž Robust job processing in Elixir, backed by modern PostgreSQL and SQLite3
https://oban.pro
Apache License 2.0
3.37k stars 314 forks source link

Support `before_process/1` #1100

Closed up2jj closed 5 months ago

up2jj commented 5 months ago

Feature

Add Oban.Pro.Worker.before_process/1 callback.

Problem to solve

We are working on a multi-tenancy implementation and would like to be able to set the dynamic repo automatically for the process, without having to tediously set it manually in process/1

Rationale:

A callback before_process/1 could be useful in a situation where the process/1 logic is still worker-specific, but the worker belongs to a group for which we would like to perform generic logic for some business reasons, e.g. modifying a meta, setting up dynamic repo, etc. The callback could be run immediately before the process/1 is executed and would return the job itself. The whole thing could work similarly to Oban.Pro.Worker.after_process/3

Example

With a callback like the one above, we could use functionality like the following:

defmodule MyCustomWorkerGroup do
  defmacro __using__(_opts) do
    quote do
      @impl Oban.Pro.Worker
      def before_process(%Oban.Job{} = _job) do
        # do smth with the job or the worker process itself
      end
    end
  end
end

defmodule MyWorker do
  use Oban.Worker, queue: :default
  use MyCustomWorkerGroup

  @impl Oban.Pro.Worker
  def process(job) do
  # perform work
  end
end
sorentwo commented 5 months ago

Agreed, it's a good idea that's frequently requested. It's ready to go for Pro v1.5 which doesn't have a scheduled release yet.

up2jj commented 5 months ago

@sorentwo Whoa, that was quick! Thank you 😍