Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9021583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:17:22+00:00 2026-06-16T05:17:22+00:00

A little explanation, I have a Symfony2 setup. I’m using an abstract command class

  • 0

A little explanation, I have a Symfony2 setup. I’m using an abstract command class that I extend. I want any of those batches to be able to run only once. My goal is to make a lock file which I open and flock so that the lock is automatically released when the php script dies in any way possible.

To realize this, I have created a class named Lock. This class extends SplFileObject and is basically a wrapper to create a *.lock somewhere (usually /var/lock/*). Now I have a problem detecting this lock. I did have a setup where it worked using fopen and flock. For some reason it won’t detect it any more.

I have created an OOP structure to basically do what I want:

  • determine a name for the lock file (uses folders)
  • create a Lock object
    • create the directory and lock file if they don’t exist
    • call the SplFileObject::__construct()
  • lock the file

I can’t get this to work with neither the handle nor the spl file object. If I run the script, let it sleep for 15 seconds and run the same script in another console I will get a result that the script managed to lock the file, flock returns true. If I make 2 Lock objects on the same lock file in the same script, I get true on the first lock and false on the second, meaning the second time it failed to obtain the lock. The script seems to work.

However, when I run the script 2 times with 2 locks in both scripts, I get True and false on both scripts… Meaning it seems like it doesn’t lock the file properly across scripts :/

Is there someone that can tell me what I’m doing wrong? I’ve checked the file name and it’s equal for both times I run the script. I have tried multiple permissions such as 777, 755, 733 but no difference.

The way I call it (just a part of the class):

abstract class AbstractTripolisCommand extends ContainerAwareCommand
{
[...]
  /**
   * Locks the current file based on environments
   *
   * @param string $application_env
   * @param string $symfony_env
   */
  private function lockCommand($application_env, $symfony_env)
  {
    $lock_name  = "tripolis/$application_env/$symfony_env/" . crc32(get_class($this));
    $lock = new Lock($lock_name, 'w+', $this->getContainer()->get('filesystem'));
    var_dump($lock->lock());
    $lock2 = new Lock($lock_name, 'w+', $this->getContainer()->get('filesystem'));
    var_dump($lock2->lock());
    // results when ran 2 times at the same time
    // bool(true)
    // bool(false)
    // when I run this script twice I expect the second run at the same time
    // bool(false)
    // bool(false)

    if(!$lock->lock()) {
      throw new Tripolis\Exception('Unable to obtain lock, script is already running');
    }
  }
[...]
}

Lock.php

namespace Something\Component\File;

use Symfony\Component\Filesystem\Filesystem;

/**
 * Creates a new SplFileObject with access to lock and release locks
 * Upon creation it will create the lock file if not exists
 *
 * The lock works by keeping a stream open to the lock file instead
 * of creating/deleting the lock file. This way the lock is always
 * released when the script ends or crashes.
 *
 * create a file named /var/lock/something/something.lock
 * <example>
 *   $lock = new Lock('something');
 *   $lock->lock();
 *   $lock->release();
 * </example>
 *
 * create a file named /var/lock/something/my-lock-file.lock
 * <example>
 *   $lock = new Lock('something/my-lock-file');
 *   $lock->lock();
 * </example>
 *
 * NOTE: locks that are not released are released
 * automatically when the php script ends
 *
 * @author Iltar van der Berg <ivanderberg@something.nl>
 */
class Lock extends \SplFileObject implements Lockable
{
  /**
   * @param string     $file_name
   * @param string     $open_mode
   * @param Filesystem $filesystem
   * @param string     $lock_directory
   */
  public function __construct($file_name, $open_mode = 'r+', Filesystem $filesystem = null, $lock_directory = '/var/lock')
  {
    $filesystem = $filesystem ?: new Filesystem();
    $file = self::touchLockFile($file_name, $lock_directory, $filesystem);
    parent::__construct($file, $open_mode);

  }

  /**
   * Returns true if the lock is placed, false if unable to
   *
   * @return boolean
   */
  public function lock()
  {
    return $this->flock(LOCK_EX | LOCK_NB);
  }

  /**
   * Returns true if the lock is released
   *
   * @return bool
   */
  public function release()
  {
    return $this->flock(LOCK_UN);
  }

  /**
   * Attempts to create a lock file for a given filename and directory
   * it will return a string if the file is touched
   *
   * @param  string     $file_name
   * @param  string     $lock_directory
   * @param  Filesystem $filesystem
   * @return string
   */
  private static function touchLockFile($file_name, $lock_directory, Filesystem $filesystem)
  {
    $lock_file_path = explode('/', $file_name);
    $lock_file      = array_pop($lock_file_path);

    $path = "$lock_directory/" . (empty($lock_file_path)
      ? $lock_file
      :  implode('/', $lock_file_path));

    $lock_file = "$path/$lock_file.lock";

    if(!$filesystem->exists($path) || !is_dir($path)) {
      $filesystem->mkdir($path);
    }

    return $lock_file;
  }
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:17:24+00:00Added an answer on June 16, 2026 at 5:17 am

    My bet is that either the lock or the file is being destroyed earlier than you think because the functions that own those objects return, thus making the objects eligible for garbage collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a homework question please help with a little explanation so that I
Little trouble in using css, in the below code, i have used odd and
First, a little explanation about my situation: I have a sample interface which is
I have some little test for Ruby On Rails 3.2.1 Here is explanation for
Little help if you don't mind. Basically, I have 3 radio boxes and a
Little Background: I have csv file which has lots of rows and each row
A little knowledge can be a dangerous thing. Now that I've had a run
A little background: I have a perl script which is performing a number of
I have a .NET 3.5 website that is uploading files to a SQL Server
On Mac OSX 5.8 I have a Java program that runs at 100% CPU

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.