Oneill
Oneill

Reputation: 339

Concurrency model mongodb doctrine symfony

I’m working on platform - MarketPlace with reservation. There are professionals who create availabilities. Availability is create with periods for example 8:00am to 5:00pm. It is the period that is saved in mongodb. The customer select a period of 1 hour. (The calendar for customer is generated dynamically with period of 1Hour) for example: for 8:00am to 5:00pm period there are: 8:00-9:00am, 9:00am-10:00am etc…

so it is not possible that 2 customers reserve the same period at the same time.

When customer select a period and click in «book button “the reservation object is saved in session. The customer buy this reservation and after the reservation object is saved in Reservation collection.

How to do for "lock" the period select by the first customer and prevent the second customer who want to reserve the same period?

And how I can fix the timer for 10 minutes and if there no payment for the period “availability” , unlock that period in the calendar? How create a chronometer synchronized with server and redirect to homepage when timeout is expired? Must i create a temporary collection with reservation in progress? Or save the object reservation in my collection Reservation with status "in progress"?

I use symfony 3 and mongodb with doctrine.

Thank you.

Upvotes: 1

Views: 117

Answers (1)

vuliad
vuliad

Reputation: 2152

How to do for "lock" the period select by the first customer and prevent the second customer who want to reserve the same period?

An update to a single document in MongoDB is atomic, that's why you can set reservation flag something like:

db.time_reservation.update(
    {_id: "<period primary key>", reserved: false,}, {
        $set: {
            reserved: true,
            reservedAt: new Date()
        }
    });

And how I can fix the timer for 10 minutes and if there no payment for the period “availability” , unlock that period in the calendar?

You can use cron command that will execute something like

db.time_reservation.update({
    reservedAt: {
        $lt: new Date(ISODate().getTime() - 1000 * 60 * 10)
    }
}, {$set: {reserved: false}});

How create a chronometer synchronized with server and redirect to homepage when timeout is expired?

Many possible solutions:

  • You can check this at every reservation stage

  • You can get timestamp from backend and validate it(save it to cookies if this is does'nt SPA)

Upvotes: 1

Related Questions