Use Oban Task To Schedule Pending Order Cancellation
Introduction
In a typical e-commerce system, orders are automatically cancelled after a certain period if they are not paid. This is a crucial feature to prevent "leaking" reserved orders, which can lead to inventory discrepancies and other issues. However, when the server restarts, and the user does not pay the order, the cancellation process may not be triggered, resulting in a reserved order that is never cancelled. In this article, we will explore how to use Oban task to schedule pending order cancellation, ensuring that orders are cancelled even if the server restarts.
Problem Statement
Our current system uses Tiki.PurchaseMonitor
to automatically cancel orders after 10 minutes if they are not paid. However, this process is not persisted, which means that if the server restarts, and the user does not pay the order, the cancellation process may not be triggered. This can lead to "leaking" reserved orders, which can cause issues with inventory management and other aspects of the system.
Solution Overview
To address this issue, we can use Oban task to schedule pending order cancellation. Oban is a reliable scheduling library that allows us to schedule tasks to run at a later time. We can use Oban to create a task that checks for pending orders and cancels them after a certain period. This approach ensures that orders are cancelled even if the server restarts, and the user does not pay the order.
Setting Up Oban
Before we can use Oban to schedule pending order cancellation, we need to set it up in our system. Oban is already being used for mail scheduling, so we can leverage this existing setup. We need to add the Oban dependency to our project and configure it to run in the background.
Step 1: Add Oban Dependency
To add Oban to our project, we need to add the following dependency to our mix.exs
file:
defp deps do
[
{:oban, "~> 2.0"}
]
end
Step 2: Configure Oban
We need to configure Oban to run in the background. We can do this by adding the following configuration to our config.exs
file:
config :oban,
repo: Tiki.Repo,
queues: [
mail: [
max_size: 100,
max_run_time: 10_000
]
]
Creating an Oban Task
Now that we have Oban set up, we can create a task that checks for pending orders and cancels them after a certain period. We can create a new task using the Oban.Task
module:
defmodule Tiki.Oban.Tasks.CancelPendingOrders do
use Oban.Task
@impl Oban.Task
def perform(%Oban.Job{args: args}) do
# Check for pending orders
pending_orders = Tiki.PurchaseMonitor.get_pending_orders()
# Cancel pending orders
Tiki.PurchaseMonitor.cancel_orders(pending_orders)
end
end
Scheduling the Task
We need to schedule the task to run at a later time. We can use the Oban.schedule
function to schedule the task:
defmodule Tiki.Oban do use Oban
def schedule do
Oban.schedule(
every: 10 * 60, # Run every 10 minutes
queue: "mail",
task: Tiki.Oban.Tasks.CancelPendingOrders,
args: []
)
end
end
Conclusion
In this article, we explored how to use Oban task to schedule pending order cancellation. We set up Oban in our system, created a task that checks for pending orders and cancels them after a certain period, and scheduled the task to run at a later time. This approach ensures that orders are cancelled even if the server restarts, and the user does not pay the order. By using Oban to schedule pending order cancellation, we can prevent "leaking" reserved orders and ensure that our inventory management system is accurate and reliable.
Benefits of Using Oban
Using Oban to schedule pending order cancellation has several benefits:
- Reliability: Oban ensures that tasks are run reliably, even if the server restarts.
- Scalability: Oban allows us to scale our system to handle a large number of tasks.
- Flexibility: Oban provides a flexible scheduling system that allows us to schedule tasks at different intervals.
- Easy to use: Oban has a simple and intuitive API that makes it easy to use.
Future Work
In the future, we can improve the pending order cancellation system by:
- Adding more features: We can add more features to the pending order cancellation system, such as sending notifications to users when their orders are cancelled.
- Improving performance: We can improve the performance of the pending order cancellation system by optimizing the database queries and reducing the number of tasks that need to be run.
- Adding more queues: We can add more queues to the Oban system to handle different types of tasks.
Conclusion
In conclusion, using Oban task to schedule pending order cancellation is a reliable and scalable solution that ensures that orders are cancelled even if the server restarts. By using Oban, we can prevent "leaking" reserved orders and ensure that our inventory management system is accurate and reliable.
Introduction
In our previous article, we explored how to use Oban task to schedule pending order cancellation. We set up Oban in our system, created a task that checks for pending orders and cancels them after a certain period, and scheduled the task to run at a later time. In this article, we will answer some frequently asked questions about using Oban task to schedule pending order cancellation.
Q: What is Oban, and how does it work?
A: Oban is a reliable scheduling library that allows us to schedule tasks to run at a later time. It uses a queue-based system to handle tasks, which ensures that tasks are run reliably, even if the server restarts.
Q: Why do I need to use Oban to schedule pending order cancellation?
A: Using Oban to schedule pending order cancellation ensures that orders are cancelled even if the server restarts, and the user does not pay the order. This prevents "leaking" reserved orders, which can cause issues with inventory management and other aspects of the system.
Q: How do I set up Oban in my system?
A: To set up Oban in your system, you need to add the Oban dependency to your project and configure it to run in the background. You can do this by adding the following dependency to your mix.exs
file:
defp deps do
[
{:oban, "~> 2.0"}
]
end
You also need to configure Oban to run in the background by adding the following configuration to your config.exs
file:
config :oban,
repo: Tiki.Repo,
queues: [
mail: [
max_size: 100,
max_run_time: 10_000
]
]
Q: How do I create an Oban task to cancel pending orders?
A: To create an Oban task to cancel pending orders, you need to create a new task using the Oban.Task
module. You can do this by creating a new module that implements the Oban.Task
behavior:
defmodule Tiki.Oban.Tasks.CancelPendingOrders do
use Oban.Task
@impl Oban.Task
def perform(%Oban.Job{args: args}) do
# Check for pending orders
pending_orders = Tiki.PurchaseMonitor.get_pending_orders()
# Cancel pending orders
Tiki.PurchaseMonitor.cancel_orders(pending_orders)
end
end
Q: How do I schedule the task to run at a later time?
A: To schedule the task to run at a later time, you need to use the Oban.schedule
function. You can do this by adding the following code to your Tiki.Oban
module:
defmodule Tiki.Oban do use Oban
def schedule do
Oban.schedule(
every: 10 * 60, # Run every 10 minutes
queue: "mail",
task: Tiki.Oban.Tasks.CancelPendingOrders,
args: []
)
end
end
Q: What are the benefits of using Oban to schedule pending order cancellation?
A: Using Oban to schedule pending order cancellation has several benefits, including:
- Reliability: Oban ensures that tasks are run reliably, even if the server restarts.
- Scalability: Oban allows us to scale our system to handle a large number of tasks.
- Flexibility: Oban provides a flexible scheduling system that allows us to schedule tasks at different intervals.
- Easy to use: Oban has a simple and intuitive API that makes it easy to use.
Q: What are some potential issues with using Oban to schedule pending order cancellation?
A: Some potential issues with using Oban to schedule pending order cancellation include:
- Task failures: If a task fails, it may not be retried, which can cause issues with pending order cancellation.
- Task timeouts: If a task takes too long to run, it may timeout, which can cause issues with pending order cancellation.
- Queue congestion: If the queue becomes congested, tasks may not be run in a timely manner, which can cause issues with pending order cancellation.
Q: How can I troubleshoot issues with Oban?
A: To troubleshoot issues with Oban, you can use the following tools and techniques:
- Oban logs: Oban logs can provide valuable information about task failures and other issues.
- Task metrics: Oban provides metrics about task performance, which can help you identify issues.
- Debugging: You can use debugging tools to step through the code and identify issues.
Conclusion
In this article, we answered some frequently asked questions about using Oban task to schedule pending order cancellation. We covered topics such as setting up Oban, creating an Oban task, scheduling the task, and troubleshooting issues. By using Oban to schedule pending order cancellation, you can ensure that orders are cancelled even if the server restarts, and the user does not pay the order.