{"id":18929491,"url":"https://github.com/thecodingmachine/utils.common.lock","last_synced_at":"2026-03-16T14:30:18.505Z","repository":{"id":15888949,"uuid":"18630207","full_name":"thecodingmachine/utils.common.lock","owner":"thecodingmachine","description":"A simple package that provides functions to get a lock. Typically, you want to use locks when you want to be sure that 2 actions do not happen at the same time. For instance, if you regularly schedule cron tasks,\tyou might want to be sure the last cron task finished before running the new one. A lock can help you do that.","archived":false,"fork":false,"pushed_at":"2016-03-10T10:01:50.000Z","size":88,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-02-16T12:30:30.792Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thecodingmachine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-10T09:00:46.000Z","updated_at":"2015-04-26T07:10:24.000Z","dependencies_parsed_at":"2022-08-04T07:00:19.896Z","dependency_job_id":null,"html_url":"https://github.com/thecodingmachine/utils.common.lock","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Futils.common.lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Futils.common.lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Futils.common.lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Futils.common.lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thecodingmachine","download_url":"https://codeload.github.com/thecodingmachine/utils.common.lock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239927823,"owners_count":19719835,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T11:32:59.947Z","updated_at":"2026-03-16T14:30:18.459Z","avatar_url":"https://github.com/thecodingmachine.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Lock Manager\r\n============\r\n\r\nThe LockManager package is a simple package that provides functions to get a lock.\r\nTypically, you want to use locks when you want to be sure that 2 actions do not\r\nhappen at the same time. For instance, if you regularly schedule cron tasks,\r\nyou might want to be sure the last cron task is finished before running a new one.\r\nA lock can help you do that.\r\n\r\nThe Lock class\r\n--------------\r\n\r\nThe Lock class represents a lock. You can **acquire** a lock to lock the resource and **release** the lock\r\nto set it free. Of course, if the lock has been already acquired by another user,\r\nyou won't be able to acquire it yourself. You can optionnaly wait for the lock\r\nto be freed by the other process to acquire it yourself.\r\n\r\nInternally, locks are acquired by putting a lock on a \"file\" that is hidden in the temp\r\ndirectory. When creating a `Lock` instance in Mouf, you will therefore have to find a unique temp name\r\nfor that file.\r\n\r\nIf your PHP script crashes or exits without explicitly releasing the lock, the lock will be \r\nautomatically released, so that other processes can use the lock.\r\n\r\nExample\r\n-------\r\n\r\nA first example: trying to acquire a lock without waiting\r\n\r\n```php\r\n// Let's create the lock instance\r\n$lock = new Lock(\"my_lock_name\");\r\n\r\n// Try to acquire lock without waiting\r\ntry {\r\n\t$lock-\u003eacquireLock();\r\n\t// ... Do some stuff ...\r\n\t$lock-\u003ereleaseLock();\r\n} catch (LockException $e) {\r\n\t// The lock could not be acquired... Let's ignore this.\r\n}\r\n```\r\n\r\nA second example: acquire a lock and wait if the lock is not available\r\n\r\n```php\r\n// Let's create the lock instance\r\n$lock = new Lock(\"my_lock_name\");\r\n\r\n// Try to acquire lock and wait if the lock is not available\r\n$lock-\u003eacquireLock(true);\r\n// ... Do some stuff ...\r\n$lock-\u003ereleaseLock();\r\n\r\n```\r\n\r\nGood practices\r\n--------------\r\n\r\nA good practice is to create the lock object via a dependency injection mechanism.\r\nThis way, you can share instances of your lock accross services that require it.\r\nThe [Mouf framework](http://mouf-php.com) let's you do that. Let's assume you create a \"myLock\" instance:\r\n\r\n![mouf instance](doc/images/instance.png)\r\n\r\nYour code to use the lock would look this:\r\n\r\n```php\r\n// You need to create an instance \"myLock\" of the Lock class in Mouf first.\r\n$lock = Mouf::getMyLock();\r\n\r\n// Try to acquire lock without waiting\r\ntry {\r\n\t$lock-\u003eacquireLock();\r\n\t// ... Do some stuff ...\r\n\t$lock-\u003ereleaseLock();\r\n} catch (LockException $e) {\r\n\t// The lock could not be acquired... Let's ignore this.\r\n}\r\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Futils.common.lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecodingmachine%2Futils.common.lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Futils.common.lock/lists"}