Saltar al contenido principal

Oban Overview

Oban is a job processing library for Elixir that provides a simple and reliable way to perform background processing tasks.

It is built on top of PostgreSQL and leverages the power and scalability of the database to handle job processing efficiently.

In this tutorial, we will explore the history, features, and examples of Oban, along with code snippets to demonstrate its capabilities.

History of Oban

Oban was created by Sasa Juric, an experienced Elixir developer, and was first released in 2017. It was designed to address the need for a robust and efficient job processing solution in Elixir, leveraging the power and reliability of PostgreSQL.

Since its initial release, Oban has gained popularity in the Elixir community due to its simplicity, performance, and reliability. It has become the go-to choice for many Elixir developers when it comes to background job processing.

Features of Oban

Oban offers a wide range of features that make it a powerful tool for handling background job processing in Elixir applications. Let's explore some of its key features:

1. PostgreSQL-backed

Oban uses PostgreSQL as its backend, allowing it to take advantage of the database's built-in job queuing and locking mechanisms. This ensures reliable and scalable job processing, as PostgreSQL is known for its performance and durability.

2. Simple API

Oban provides a simple and intuitive API for defining and scheduling jobs. You can define jobs using Elixir modules and functions, making it easy to reason about the job logic and dependencies.

3. Job Prioritization

Oban allows you to define job priorities, ensuring that high-priority jobs are processed before lower-priority ones. This feature is particularly useful when dealing with time-sensitive tasks or handling a large number of jobs in a multi-tenant application.

4. Retries and Timeouts

Oban supports automatic job retries and timeouts. You can configure the number of retries for a job, as well as the delay between retries. Additionally, you can set a maximum execution time for a job, after which it will be automatically canceled.

5. Job Dependencies

Oban allows you to define dependencies between jobs, ensuring that a job is only executed when its dependencies have been successfully completed. This feature is useful when you have a complex workflow that requires multiple steps to be executed in a specific order.

6. Job Supervision

Oban integrates seamlessly with Elixir's supervision tree, allowing you to supervise and monitor your jobs along with the rest of your application's processes. This ensures that your jobs are automatically restarted in case of failures or crashes.

7. Dashboard

Oban provides a built-in dashboard that allows you to monitor and manage your jobs. The dashboard provides real-time metrics, job status, and the ability to manually control job processing. It is a valuable tool for debugging and monitoring your background job processing.

Examples of Oban

Now let's dive into some examples to demonstrate the features of Oban.

Example 1: Defining and Scheduling a Job

defmodule MyJob do
def perform(args) do
# Job logic goes here
end
end

Oban.insert_job(%{
module: MyJob,
args: %{key: "value"},
priority: :high
})

In this example, we define a job using the MyJob module and the perform function. We then insert the job into Oban's job queue with a high priority.

Example 2: Job Dependencies

defmodule JobA do
def perform(args) do
# Job A logic goes here
end
end

defmodule JobB do
def perform(args) do
# Job B logic goes here
end
end

Oban.insert_job(%{
module: JobA,
args: %{key: "value"}
})

Oban.insert_job(%{
module: JobB,
args: %{key: "value"},
depends: [JobA]
})

In this example, we define two jobs, JobA and JobB, with JobB depending on JobA. This ensures that JobB is only executed when JobA has been successfully completed.

Example 3: Job Retries and Timeouts

defmodule MyJob do
def perform(args) do
# Job logic goes here
raise "Something went wrong"
end
end

Oban.insert_job(%{
module: MyJob,
args: %{key: "value"},
retries: 3,
retry_delay: 5000,
max_runtime: 10000
})

In this example, we define a job that raises an error. Oban will automatically retry the job three times with a delay of 5 seconds between retries. If the job exceeds the maximum runtime of 10 seconds, it will be automatically canceled.

Conclusion

In this tutorial, we explored the introduction, history, features, and examples of Oban, the job processing library for Elixir. We learned about its PostgreSQL-backed architecture, simple API, job prioritization, retries and timeouts, job dependencies, job supervision, and built-in dashboard.

Oban provides a robust and reliable solution for handling background job processing in Elixir applications. Its simplicity, performance, and scalability make it a popular choice among Elixir developers.

To learn more about Oban, you can visit the official website: https://hexdocs.pm/oban.