There are basically two options to avoid this race condition. They both have trade-offs.
Option A: Make two separate UPDATE
queries to update these fields, and let your database server handle the locking for you. Zend's DB Framework already has built-in methods for making these sorts of updates. Here's two example functions to get you started:
function paymentProcessingUpdate($incrementId, $paymentId) {
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$table = 'sales_flat_order';
$data = array('payment_id' => $paymentId);
$where = $db->quoteInto('increment_id = ?', $incrementId);
$db->update($table, $data, $where);
}
function assignCustomerToOrder($incrementId, $customerId) {
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$table = 'sales_flat_order';
$data = array('customer_id' => $customerId);
$where = $db->quoteInto('increment_id = ?', $incrementId);
$db->update($table, $data, $where);
}
Downsides to this approach:
- If your fields are used in the
sales_flat_order_grid
table, you must update that table too. This is something that Magento's models normally would do for you. You could add a JOIN
to handle this in a single UPDATE
query.
- It bypasses the Magento models, which means that observers like
sales_order_save_commit_after
and sales_order_save_after
don't get triggered.
Advantages:
- It's much faster to run a single update than save the entire model.
Option B: You can use a locking mechanism like Mage_Index_Model_Lock
, which is what Magento does while refreshing an index.
How it works: Let's assume we're processing order number 100002185
. Process A and B are interchangeable in this example.
- Process A starts and checks if there is a lock for
automation_order_id_100002185
.
- Process A does not find a lock, so it sets a lock for
automation_order_id_100002185
and begins doing its work.
- Process B starts, before process A has completed, and checks for a lock for
automation_order_id_100002185
.
- Process B finds a lock, so it sleeps for 3 seconds.
- Process A is still working. Process B checks for a lock again after its 3 second sleep, but process A has not released it yet, so it does another 3 second sleep.
- Process A finishes doing its work, and releases the lock.
- Process B checks for a lock again after its 3 second sleep. This time there is no lock, so process B sets a lock, and begins to do its work.
- Process B finishes its work and releases the lock.
Downsides to this approach:
- It sacrifices speed. If the fields being updated by each process are different, un-related, and independent, they could be doing their work in parallell.
- A catastrophic server failure (or reboot) can result in the lock never being released.
- If the lock is never released, the process may sleep indefinitely, and its work may never get done. This can cause processes to stack up and eventually crash the server, causing a chain reaction for other processes.
- If the server crashes, and you don't have a queue of some sort to fall back on, your processing may never actually get done. Magento 2 uses RabbitMQ to solve this issue, but you could implement a similar mechanism using a database table and cron scripts.