Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform a task in 15 minutes from now in Rails

I'm creating a Rails application that let users book tickets for an event.

Once the user selects the ticket he wants to buy, he has 15 minutes to complete the checkout, otherwise the ticket will be released and can be booked by someone else.

How can I "block" the ticket for 15 minutes and make it available again after 15 minutes?

like image 956
Fred Collins Avatar asked Sep 20 '25 12:09

Fred Collins


2 Answers

Have a status field in your ticket model(or whatever model you are using) and mark it as partially_booked when user selects it.

If you are using delayed jobs,

handle_asynchronously :your_method_here, :run_at => Proc.new { 15.minutes.from_now }

If you are using sidekiq,

YourWorker.perform_in(15.minutes, ticket_id)

where 'your_method_here' or 'YourWorker' will contain the logic to check if booking was completed or not, and mark it as unassigned if it was not completed.

like image 77
rohan Avatar answered Sep 22 '25 13:09

rohan


ActiveJob helps doing jobs in the background. Using ActiveJob you can generate jobs for your delayed operations and enqueue them for later execution. You can add a boolean field to your model like reserved and make it true when the user first reserves it and run a background job so that if in 15 minutes later user won't complete the purchase, set it back to false:

TicketJob.set(wait_until: 15.minutes.from_now).perform_later(@ticket)

Active Job is a framework for declaring jobs and making them run on a variety of queuing backends. For setting up a backend for ActiveJob, take a look at DelayedJob. It is simple and good enough.

like image 29
Aref Aslani Avatar answered Sep 22 '25 12:09

Aref Aslani