When you just want to run some code in a background task then you can use PetalPro.BackgroundTask.run/1:
1
PetalPro.BackgroundTask.run(fn->
2
do_some_time_instensive_stuff()
3
end)
Copied!
This can be useful for speeding up responses in a HTTP request or LiveView action - you delegate anything that takes time to a background task (eg sending emails, sending to Slack, creating a log, etc.). Since we want to respond to a user request as soon as possible, you run these other non-essential tasks in a background task and it won't hold up the response.
Background jobs with Oban
A background job is a something you want to run outside of a normal HTTP request/response cycle. Things like sending weekly emails or processing images. We use Oban for this.
A background job is performed by a "worker". Each worker gets its own file. Petal Pro comes with an example worker you can duplicate: