Hi friends, I have used Oban in a library, and it is defined as a dependency in my umbrella project.
Now I need to create new Periodic Jobs worker, but Oban was configured in the library before, and I do not want to change the library; hence I decided to start a queue manual in one of my init Genserver when I start a server or user sends a request like this:
defmodule MishkaUser.Worker.ExpireTokenWorker do
use Oban.Worker, queue: :expire_token, max_attempts: 1
require Logger
@spec perform(Oban.Job.t()) :: :ok
def perform(%Oban.Job{}) do
IO.inspect("this is what is it")
:ok
end
end
And for Starting
Oban.start_queue(queue: :expire_token, limit: 10)
As I said, I do not want to change my library config, so how can I declare MishkaUser.Worker.ExpireTokenWorker settings on this particular module as a Periodic Jobs?
You didn’t write if there’s something not working with your approach and/or if you’re getting any error… it looks like it should be working. What’s the problem?
It is an example from my mind, not a right config, the problem is how can start a Periodic Jobs worker with Oban.start_queue and without changing the config
Ah okay. I don’t know if you can do it. I suspect you may need to update the Oban config, which can be done at runtime with Application.get_env/Application.put_env, then restart Oban application.
If your Oban is a “pro” version, and you do already have Cron set up as Dynamic Cron Plugin, you can however do what you want as it allows you to insert / remove scheduled jobs on the fly: Dynamic Cron Plugin — Oban Pro v0.11.1
But yeah, if you’re locked to the oban provided by library, and it’s not pro, you probably need to update its config and restart it…
Considering how simple the use-case is, you don’t need to dynamically define a periodic schedule for that job. Instead, define the crontab statically in your overall config, leave the job in the default queue (or another queue you are already running), and then check whether it should run from within the worker.
defmodule MishkaUser.Worker.ExpireTokenWorker do
use Oban.Worker, queue: :default, max_attempts: 1
@impl Oban.Worker # Don't define a new spec, declare it as an implementation
def perform(_job) do
if Application.get_env(:my_app, :expire_token_enabled?) do
IO.puts("Expire token is enabled")
end
:ok
end
end
From outside your worker, where you were dynamically starting the queue, update the application environment:
Dynamically starting Oban supervisors and queues is advanced, and I really recommend using a simpler path until you get things working in your application.
No, absolutely not. Queues are for concurrency control
You can have as many entries (workers) as you need in the crontab. If you want something that runs every 24 hours, use 0 0 * * * to run it at midnight.