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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With