fireproofsocks

fireproofsocks

Oban Pro: overriding Oban.Pro.Worker.new/2

I’m exploring structured workers in Oban Pro – this is a nice feature!

I’m trying to reduce redundant code and have reasonably clear interfaces for my functions. One concern is that the struct for the structured worker looks nearly identical to one of our Ecto structs. I think it’s probably good to have a degree of abstraction separating the 2 structs, but I’m wondering if there’s any way to reuse some of the definitions?

Secondly, I find it a bit more clear if we pass one of our Ecto structs to enqueue as a (structured) Oban job. We don’t want to implement the Jason encoding protocol, so I’ve found it useful to override the new/2 function in the worker module (i.e. the one that uses Oban.Pro.Worker) so I can handle the input in a way that makes sense for our app. However, the new/2 function doesn’t seem to like the default opts that come through…

I would expect this to work:

def new(args, opts) do
    # ... custom tweaks to args here ...
    Job.new(args, opts)
  end

but this generates some errors:

     Expected args and opts to build a valid job, got validation errors:

     base: unknown option :stages provided

So I have found myself doing something like this:

def new(args, opts) do
    opts = __opts__() |> Worker.merge_opts(opts) |> Keyword.delete(:stages)
    # ... custom tweaks to args here ...
    Job.new(args, opts)
  end

That seems to work, but it looks a bit hacky. Can someone point out a cleaner way to do this? Did I improperly implement the new/2 function? Is there a better way to override this?

Marked As Solved

fireproofsocks

fireproofsocks

Got it – thanks! I had forgotten about this trick

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New

Other popular topics Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement