Automattic\WooCommerce\Internal\ProductFeed\Storage
JsonFileFeed::acquire_lock
Acquires an exclusive, non-blocking lock on the open feed file handle.
A feed file is written across separate, short-lived processes (one Action Scheduler action per chunk). A stuck in-progress job can be treated as stuck and have a fresh generation enqueued while the original is still running, so two processes can end up writing the same file at once; without mutual exclusion their unbuffered writes interleave into malformed JSON. The lock makes each chunk's write to the shared file exclusive, so overlapping generations cannot interleave.
The lock is advisory, which is sufficient because every writer goes through this class. It is released when the handle is closed in flush()/end(), including when a process is killed and the OS closes its descriptors — so a killed job leaves no stale lock, and its partial file is handled by the heartbeat-based stuck-job recovery instead.
Method of the class: JsonFileFeed{}
No Hooks.
Returns
void. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->acquire_lock( $handle ): void;
- $handle(resource) (required)
- The open feed file handle.
JsonFileFeed::acquire_lock() JsonFileFeed::acquire lock code WC 11.1.2
private function acquire_lock( $handle ): void {
if ( ! flock( $handle, LOCK_EX | LOCK_NB ) ) {
fclose( $handle );
$this->file_handle = null;
throw new FeedLockException(
esc_html(
sprintf(
/* translators: %s: file path */
__( 'Feed file is locked by another generation process: %s', 'woocommerce' ),
$this->file_path
)
)
);
}
}