{"action":"created","comment":{"url":"https://api.github.com/repos/symfony/symfony/pulls/comments/94204434","pull_request_review_id":14735254,"id":94204434,"diff_hunk":"@@ -0,0 +1,110 @@\n+<?php\n+\n+/*\n+ * This file is part of the Symfony package.\n+ *\n+ * (c) Fabien Potencier <fabien@symfony.com>\n+ *\n+ * For the full copyright and license information, please view the LICENSE\n+ * file that was distributed with this source code.\n+ */\n+\n+namespace Symfony\\Component\\Lock;\n+\n+use Psr\\Log\\LoggerAwareTrait;\n+use Psr\\Log\\NullLogger;\n+use Symfony\\Component\\Lock\\Exception\\InvalidArgumentException;\n+use Symfony\\Component\\Lock\\Exception\\LockAcquiringException;\n+use Symfony\\Component\\Lock\\Exception\\LockConflictedException;\n+use Symfony\\Component\\Lock\\Exception\\LockReleasingException;\n+use Symfony\\Component\\Lock\\Store\\BlockingStoreInterface;\n+use Symfony\\Component\\Lock\\Store\\ExpirableStoreInterface;\n+\n+/**\n+ * Lock is the default implementation of the LockInterface.\n+ *\n+ * @author Jérémy Derussé <jeremy@derusse.com>\n+ */\n+final class Lock implements LockInterface\n+{\n+    use LoggerAwareTrait;\n+\n+    private $store;\n+    private $key;\n+\n+    public function __construct(Key $key, StoreInterface $store)\n+    {\n+        $this->store = $store;\n+        $this->key = $key;\n+\n+        $this->logger = new NullLogger();\n+    }\n+\n+    /**\n+     * {@inheritdoc}\n+     */\n+    public function acquire($blocking = false, $ttl = 300.0)\n+    {\n+        try {\n+            if (!$blocking) {\n+                $this->store->save($this->key);\n+            } elseif ($this->store instanceof BlockingStoreInterface) {\n+                $this->store->waitAndSave($this->key);\n+            } else {\n+                throw new InvalidArgumentException(sprintf('Unable to acquire a blocking lock on a instance of \"%s\".', get_class($this->store)));\n+            }\n+\n+            $this->logger->info('Lock successfully acquired for \"{resource}\"', array('resource' => $this->key->getResource()));\n+            $this->refresh($ttl);\n+        } catch (LockConflictedException $e) {\n+            $this->logger->info('Fail to lock the \"{resource}\". Someone else already acquired the lock', array('resource' => $this->key->getResource()));\n+            throw $e;\n+        } catch (\\Exception $e) {\n+            $this->logger->info('Fail to lock the \"{resource}\"', array('resource' => $this->key->getResource(), 'exception' => $e));\n+            throw new LockAcquiringException('', 0, $e);\n+        }\n+    }\n+\n+    /**\n+     * {@inheritdoc}\n+     */\n+    public function refresh($ttl = 300.0)\n+    {\n+        if (!$this->store instanceof ExpirableStoreInterface) {\n+            return;\n+        }\n+\n+        try {\n+            $this->store->expire($this->key, $ttl);\n+            $this->logger->info('Expiration defined for lock \"{resource}\" for \"{ttl}\" seconds', array('resource' => $this->key->getResource(), 'ttl' => $ttl));\n+        } catch (LockConflictedException $e) {\n+            $this->logger->info('Fail to define a expiration for the lock \"{resource}\". Someone else acquired the lock', array('resource' => $this->key->getResource()));","path":"src/Symfony/Component/Lock/Lock.php","position":81,"original_position":81,"commit_id":"ba20b1280169cc5b9a0639c7adf855eb1c9d0dbd","original_commit_id":"ba20b1280169cc5b9a0639c7adf855eb1c9d0dbd","user":{"login":"fabpot","id":47313,"avatar_url":"https://avatars.githubusercontent.com/u/47313?v=3","gravatar_id":"","url":"https://api.github.com/users/fabpot","html_url":"https://github.com/fabpot","followers_url":"https://api.github.com/users/fabpot/followers","following_url":"https://api.github.com/users/fabpot/following{/other_user}","gists_url":"https://api.github.com/users/fabpot/gists{/gist_id}","starred_url":"https://api.github.com/users/fabpot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fabpot/subscriptions","organizations_url":"https://api.github.com/users/fabpot/orgs","repos_url":"https://api.github.com/users/fabpot/repos","events_url":"https://api.github.com/users/fabpot/events{/privacy}","received_events_url":"https://api.github.com/users/fabpot/received_events","type":"User","site_admin":false},"body":"`an expiration`, same below.","created_at":"2016-12-30T06:00:43Z","updated_at":"2016-12-30T06:10:18Z","html_url":"https://github.com/symfony/symfony/pull/21093#discussion_r94204434","pull_request_url":"https://api.github.com/repos/symfony/symfony/pulls/21093","_links":{"self":{"href":"https://api.github.com/repos/symfony/symfony/pulls/comments/94204434"},"html":{"href":"https://github.com/symfony/symfony/pull/21093#discussion_r94204434"},"pull_request":{"href":"https://api.github.com/repos/symfony/symfony/pulls/21093"}}},"pull_request":{"url":"https://api.github.com/repos/symfony/symfony/pulls/21093","id":99649813,"html_url":"https://github.com/symfony/symfony/pull/21093","diff_url":"https://github.com/symfony/symfony/pull/21093.diff","patch_url":"https://github.com/symfony/symfony/pull/21093.patch","issue_url":"https://api.github.com/repos/symfony/symfony/issues/21093","number":21093,"state":"open","locked":false,"title":"[Lock] Create a lock component","user":{"login":"jderusse","id":578547,"avatar_url":"https://avatars.githubusercontent.com/u/578547?v=3","gravatar_id":"","url":"https://api.github.com/users/jderusse","html_url":"https://github.com/jderusse","followers_url":"https://api.github.com/users/jderusse/followers","following_url":"https://api.github.com/users/jderusse/following{/other_user}","gists_url":"https://api.github.com/users/jderusse/gists{/gist_id}","starred_url":"https://api.github.com/users/jderusse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jderusse/subscriptions","organizations_url":"https://api.github.com/users/jderusse/orgs","repos_url":"https://api.github.com/users/jderusse/repos","events_url":"https://api.github.com/users/jderusse/events{/privacy}","received_events_url":"https://api.github.com/users/jderusse/received_events","type":"User","site_admin":false},"body":"| Q             | A\r\n| ------------- | ---\r\n| Branch?       | master\r\n| Bug fix?      | no\r\n| New feature?  | yes\r\n| BC breaks?    | no\r\n| Deprecations? | yes\r\n| Tests pass?   | they will\r\n| Fixed tickets | #20382 #4976\r\n| License       | MIT\r\n| Doc PR        | will do\r\n\r\nThis PR aim to add a new component Lock going further than the FileSystem\\LockHandler by allowing remote backend (like Redis, memcache, etc)\r\nInspired by \r\n\r\n## Usage\r\n\r\nThe simplest way to use lock is to inject an instance of a Lock in your service\r\n```php\r\nclass MyService\r\n{\r\n    private $lock;\r\n\r\n    public function __construct(LockInterface $lock)\r\n    {\r\n        $this->lock = $lock;\r\n    }\r\n\r\n    public function run()\r\n    {\r\n        $this->lock->acquire(true);\r\n\r\n        // If I'm here, no exception had been raised. Lock is acquired\r\n        try {\r\n            // do my job\r\n        } finally {\r\n            $this->lock->release();\r\n        }\r\n    }\r\n}\r\n```\r\nConfigured with something like\r\n```yaml\r\nservices:\r\n    app.my_service:\r\n        class: AppBundle\\MyService\r\n        arguments:\r\n            - app.lock.my_service\r\n    app.lock.my_service:\r\n        class: Symfony\\Component\\Lock\\Lock\r\n        factory: ['@locker', createLock]\r\n        arguments: ['my_service']\r\n```\r\n\r\nIf you need to lock serveral resource on runtime, wou'll nneed to inject the LockFactory.\r\n```php\r\nclass MyService\r\n{\r\n    private $lockFactory;\r\n\r\n    public function __construct(LockFactoryInterface $lockFactory)\r\n    {\r\n        $this->lockFactory = $lockFactory;\r\n    }\r\n\r\n    public function run()\r\n    {\r\n        foreach ($this->items as $item) {\r\n            $lock = $this->lockFactory->createLock((string) $item);\r\n\r\n            try {\r\n                $lock->acquire();\r\n            } catch (LockConflictedException $e) {\r\n                continue;\r\n            }\r\n\r\n            // When I'm here, no exception had been, raised. Lock is acquired\r\n            try {\r\n                // do my job\r\n            } finally {\r\n                $lock->release();\r\n            }\r\n        }\r\n    }\r\n}\r\n```\r\nConfigured with something like\r\n```yaml\r\nservices:\r\n    app.my_service:\r\n        class: AppBundle\\MyService\r\n        arguments:\r\n            - '@locker'\r\n```\r\n\r\nThis component allow you to refresh an expirable lock.\r\nThis is usefull, if you run a long operation split in several small parts.\r\nIf you lock with a ttl for the overall operatoin time and your process crash, the lock will block everybody for the defined TTL.\r\nBut thank to the refresh method, you're able to lock for a small TTL, and refresh it between each parts.\r\n```php\r\nclass MyService\r\n{\r\n    private $lock;\r\n\r\n    public function __construct(LockInterface $lock)\r\n    {\r\n        $this->lock = $lock;\r\n    }\r\n\r\n    public function run()\r\n    {\r\n        $this->lock->acquire(true);\r\n\r\n        try {\r\n            do {\r\n                $finished = $this->performLongTask();\r\n\r\n                // Increase the expire date by 300 more seconds\r\n                $this->lock->refresh();\r\n            } while (!$finished)\r\n            // do my job\r\n        } finally {\r\n            $this->lock->release();\r\n        }\r\n    }\r\n}\r\n```\r\n\r\n## Naming anc implementation choise\r\n\r\n```\r\n$lock->acquire()\r\nvs\r\n$lock->lock()\r\n```\r\n\r\nChoose to use acquire, because this component is full of `lock` Symfony\\Component\\Lock\\Lock::Lock` raised a E_TOO_MANY_LOCK in my head.\r\n\r\n\r\n\r\n```\r\n$lock->acquire(false);\r\n$lock->acquire(true);\r\nvs\r\n$lock->aquire()\r\n$lock->waitAndAquire()\r\n```\r\n\r\nNot a big fan of flag feature and 2. But I choose to use the blocking flag to offer a simple (and common usecase) implementation\r\n\r\n\r\n```\r\n$lock = $factory->createLock($key);\r\n$lock->acquire();\r\nvs\r\n$lock->aquire($key)\r\n```\r\n\r\nI choose to a the pool of locks implementation. It allow the user to create 2 instances and use cross lock even in the same process. \r\n\r\n```\r\ninterface LockInterface \r\nfinal class Lock implements LockInterface\r\nvs\r\nfinal class Lock\r\n```\r\n\r\nI choose to use a Interface even if there is only one implementaiton to offer an extension point here\r\n\r\n\r\n\r\n# TODO\r\n\r\n* [ ] tests\r\n* [ ] documentation\r\n\r\n* [ ] add configuration in framework bundle\r\n* [ ] add stop watch in the debug bar\r\n* [x] add logs\r\n* [x] offer several redis connectors\r\n* [ ] improve the combined store (takes the drift into account and elapsed time between each store)\r\n* [ ] implement other stores (memcache, ...)\r\n\r\n\r\n* [ ] replace and deprecate Symfony\\Component\\Filesystem\\LockHandler\r\n* [ ] use this component in session manipulation\r\n","created_at":"2016-12-29T12:21:23Z","updated_at":"2016-12-30T06:10:18Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e1324d2de834b17d5c99ed0580b038b58af20960","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/symfony/symfony/pulls/21093/commits","review_comments_url":"https://api.github.com/repos/symfony/symfony/pulls/21093/comments","review_comment_url":"https://api.github.com/repos/symfony/symfony/pulls/comments{/number}","comments_url":"https://api.github.com/repos/symfony/symfony/issues/21093/comments","statuses_url":"https://api.github.com/repos/symfony/symfony/statuses/ba20b1280169cc5b9a0639c7adf855eb1c9d0dbd","head":{"label":"jderusse:lock","ref":"lock","sha":"ba20b1280169cc5b9a0639c7adf855eb1c9d0dbd","user":{"login":"jderusse","id":578547,"avatar_url":"https://avatars.githubusercontent.com/u/578547?v=3","gravatar_id":"","url":"https://api.github.com/users/jderusse","html_url":"https://github.com/jderusse","followers_url":"https://api.github.com/users/jderusse/followers","following_url":"https://api.github.com/users/jderusse/following{/other_user}","gists_url":"https://api.github.com/users/jderusse/gists{/gist_id}","starred_url":"https://api.github.com/users/jderusse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jderusse/subscriptions","organizations_url":"https://api.github.com/users/jderusse/orgs","repos_url":"https://api.github.com/users/jderusse/repos","events_url":"https://api.github.com/users/jderusse/events{/privacy}","received_events_url":"https://api.github.com/users/jderusse/received_events","type":"User","site_admin":false},"repo":{"id":19909670,"name":"symfony","full_name":"jderusse/symfony","owner":{"login":"jderusse","id":578547,"avatar_url":"https://avatars.githubusercontent.com/u/578547?v=3","gravatar_id":"","url":"https://api.github.com/users/jderusse","html_url":"https://github.com/jderusse","followers_url":"https://api.github.com/users/jderusse/followers","following_url":"https://api.github.com/users/jderusse/following{/other_user}","gists_url":"https://api.github.com/users/jderusse/gists{/gist_id}","starred_url":"https://api.github.com/users/jderusse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jderusse/subscriptions","organizations_url":"https://api.github.com/users/jderusse/orgs","repos_url":"https://api.github.com/users/jderusse/repos","events_url":"https://api.github.com/users/jderusse/events{/privacy}","received_events_url":"https://api.github.com/users/jderusse/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jderusse/symfony","description":"The Symfony PHP framework","fork":true,"url":"https://api.github.com/repos/jderusse/symfony","forks_url":"https://api.github.com/repos/jderusse/symfony/forks","keys_url":"https://api.github.com/repos/jderusse/symfony/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jderusse/symfony/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jderusse/symfony/teams","hooks_url":"https://api.github.com/repos/jderusse/symfony/hooks","issue_events_url":"https://api.github.com/repos/jderusse/symfony/issues/events{/number}","events_url":"https://api.github.com/repos/jderusse/symfony/events","assignees_url":"https://api.github.com/repos/jderusse/symfony/assignees{/user}","branches_url":"https://api.github.com/repos/jderusse/symfony/branches{/branch}","tags_url":"https://api.github.com/repos/jderusse/symfony/tags","blobs_url":"https://api.github.com/repos/jderusse/symfony/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jderusse/symfony/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jderusse/symfony/git/refs{/sha}","trees_url":"https://api.github.com/repos/jderusse/symfony/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jderusse/symfony/statuses/{sha}","languages_url":"https://api.github.com/repos/jderusse/symfony/languages","stargazers_url":"https://api.github.com/repos/jderusse/symfony/stargazers","contributors_url":"https://api.github.com/repos/jderusse/symfony/contributors","subscribers_url":"https://api.github.com/repos/jderusse/symfony/subscribers","subscription_url":"https://api.github.com/repos/jderusse/symfony/subscription","commits_url":"https://api.github.com/repos/jderusse/symfony/commits{/sha}","git_commits_url":"https://api.github.com/repos/jderusse/symfony/git/commits{/sha}","comments_url":"https://api.github.com/repos/jderusse/symfony/comments{/number}","issue_comment_url":"https://api.github.com/repos/jderusse/symfony/issues/comments{/number}","contents_url":"https://api.github.com/repos/jderusse/symfony/contents/{+path}","compare_url":"https://api.github.com/repos/jderusse/symfony/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jderusse/symfony/merges","archive_url":"https://api.github.com/repos/jderusse/symfony/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jderusse/symfony/downloads","issues_url":"https://api.github.com/repos/jderusse/symfony/issues{/number}","pulls_url":"https://api.github.com/repos/jderusse/symfony/pulls{/number}","milestones_url":"https://api.github.com/repos/jderusse/symfony/milestones{/number}","notifications_url":"https://api.github.com/repos/jderusse/symfony/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jderusse/symfony/labels{/name}","releases_url":"https://api.github.com/repos/jderusse/symfony/releases{/id}","deployments_url":"https://api.github.com/repos/jderusse/symfony/deployments","created_at":"2014-05-18T12:08:19Z","updated_at":"2014-06-30T19:48:25Z","pushed_at":"2016-12-30T00:25:39Z","git_url":"git://github.com/jderusse/symfony.git","ssh_url":"git@github.com:jderusse/symfony.git","clone_url":"https://github.com/jderusse/symfony.git","svn_url":"https://github.com/jderusse/symfony","homepage":"symfony.com","size":79116,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"symfony:master","ref":"master","sha":"64e1da05810e22a04c06976d009e579f3e8d4d01","user":{"login":"symfony","id":143937,"avatar_url":"https://avatars.githubusercontent.com/u/143937?v=3","gravatar_id":"","url":"https://api.github.com/users/symfony","html_url":"https://github.com/symfony","followers_url":"https://api.github.com/users/symfony/followers","following_url":"https://api.github.com/users/symfony/following{/other_user}","gists_url":"https://api.github.com/users/symfony/gists{/gist_id}","starred_url":"https://api.github.com/users/symfony/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/symfony/subscriptions","organizations_url":"https://api.github.com/users/symfony/orgs","repos_url":"https://api.github.com/users/symfony/repos","events_url":"https://api.github.com/users/symfony/events{/privacy}","received_events_url":"https://api.github.com/users/symfony/received_events","type":"Organization","site_admin":false},"repo":{"id":458058,"name":"symfony","full_name":"symfony/symfony","owner":{"login":"symfony","id":143937,"avatar_url":"https://avatars.githubusercontent.com/u/143937?v=3","gravatar_id":"","url":"https://api.github.com/users/symfony","html_url":"https://github.com/symfony","followers_url":"https://api.github.com/users/symfony/followers","following_url":"https://api.github.com/users/symfony/following{/other_user}","gists_url":"https://api.github.com/users/symfony/gists{/gist_id}","starred_url":"https://api.github.com/users/symfony/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/symfony/subscriptions","organizations_url":"https://api.github.com/users/symfony/orgs","repos_url":"https://api.github.com/users/symfony/repos","events_url":"https://api.github.com/users/symfony/events{/privacy}","received_events_url":"https://api.github.com/users/symfony/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/symfony/symfony","description":"The Symfony PHP framework","fork":false,"url":"https://api.github.com/repos/symfony/symfony","forks_url":"https://api.github.com/repos/symfony/symfony/forks","keys_url":"https://api.github.com/repos/symfony/symfony/keys{/key_id}","collaborators_url":"https://api.github.com/repos/symfony/symfony/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/symfony/symfony/teams","hooks_url":"https://api.github.com/repos/symfony/symfony/hooks","issue_events_url":"https://api.github.com/repos/symfony/symfony/issues/events{/number}","events_url":"https://api.github.com/repos/symfony/symfony/events","assignees_url":"https://api.github.com/repos/symfony/symfony/assignees{/user}","branches_url":"https://api.github.com/repos/symfony/symfony/branches{/branch}","tags_url":"https://api.github.com/repos/symfony/symfony/tags","blobs_url":"https://api.github.com/repos/symfony/symfony/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/symfony/symfony/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/symfony/symfony/git/refs{/sha}","trees_url":"https://api.github.com/repos/symfony/symfony/git/trees{/sha}","statuses_url":"https://api.github.com/repos/symfony/symfony/statuses/{sha}","languages_url":"https://api.github.com/repos/symfony/symfony/languages","stargazers_url":"https://api.github.com/repos/symfony/symfony/stargazers","contributors_url":"https://api.github.com/repos/symfony/symfony/contributors","subscribers_url":"https://api.github.com/repos/symfony/symfony/subscribers","subscription_url":"https://api.github.com/repos/symfony/symfony/subscription","commits_url":"https://api.github.com/repos/symfony/symfony/commits{/sha}","git_commits_url":"https://api.github.com/repos/symfony/symfony/git/commits{/sha}","comments_url":"https://api.github.com/repos/symfony/symfony/comments{/number}","issue_comment_url":"https://api.github.com/repos/symfony/symfony/issues/comments{/number}","contents_url":"https://api.github.com/repos/symfony/symfony/contents/{+path}","compare_url":"https://api.github.com/repos/symfony/symfony/compare/{base}...{head}","merges_url":"https://api.github.com/repos/symfony/symfony/merges","archive_url":"https://api.github.com/repos/symfony/symfony/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/symfony/symfony/downloads","issues_url":"https://api.github.com/repos/symfony/symfony/issues{/number}","pulls_url":"https://api.github.com/repos/symfony/symfony/pulls{/number}","milestones_url":"https://api.github.com/repos/symfony/symfony/milestones{/number}","notifications_url":"https://api.github.com/repos/symfony/symfony/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/symfony/symfony/labels{/name}","releases_url":"https://api.github.com/repos/symfony/symfony/releases{/id}","deployments_url":"https://api.github.com/repos/symfony/symfony/deployments","created_at":"2010-01-04T14:21:21Z","updated_at":"2016-12-30T05:47:35Z","pushed_at":"2016-12-30T02:09:34Z","git_url":"git://github.com/symfony/symfony.git","ssh_url":"git@github.com:symfony/symfony.git","clone_url":"https://github.com/symfony/symfony.git","svn_url":"https://github.com/symfony/symfony","homepage":"symfony.com","size":82272,"stargazers_count":13548,"watchers_count":13548,"language":"PHP","has_issues":true,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":5266,"mirror_url":null,"open_issues_count":885,"forks":5266,"open_issues":885,"watchers":13548,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/symfony/symfony/pulls/21093"},"html":{"href":"https://github.com/symfony/symfony/pull/21093"},"issue":{"href":"https://api.github.com/repos/symfony/symfony/issues/21093"},"comments":{"href":"https://api.github.com/repos/symfony/symfony/issues/21093/comments"},"review_comments":{"href":"https://api.github.com/repos/symfony/symfony/pulls/21093/comments"},"review_comment":{"href":"https://api.github.com/repos/symfony/symfony/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/symfony/symfony/pulls/21093/commits"},"statuses":{"href":"https://api.github.com/repos/symfony/symfony/statuses/ba20b1280169cc5b9a0639c7adf855eb1c9d0dbd"}}}}
{"actor":{"display_login":"fabpot"}}