{"action":"created","comment":{"url":"https://api.github.com/repos/vikingco/django-triggers/pulls/comments/74403551","id":74403551,"diff_hunk":"@@ -82,28 +89,88 @@ def set_source(self, *args):\n     def get_source(self):\n         return tuple(x for x in self.source.split('$') if x != '')\n \n-    def process(self, force=False, logger=None, dictionary={}):\n-        if logger:\n-            self.logger = get_logger(logger)\n-        now = timezone.now()\n-        if not force and self.date_processed is not None:\n-            raise AlreadyProcessedError()\n-        if not force and self.process_after and self.process_after >= now:\n-            raise ProcessLaterError(self.process_after)\n-\n+    @shared_task(filter=task_method)\n+    def process(self, force=False, logger=None, dictionary={}, use_statsd=False):\n+        \"\"\"\n+        Executes the Trigger\n+        :param bool force: force the execution\n+        :param string logger: slug of preferred logger\n+        :param dict dictionary: dictionary needed by trigger to execute\n+        :param bool use_statsd: whether to use statsd\n+        :return: None\n+        \"\"\"\n+        # The task gets locked because multiple tasks in the queue can process the same trigger.\n+        # The lock assures no two tasks can process a trigger simultaneously.\n+        # The check for date_processed assures a trigger is not executed multiple times.\n         try:\n-            self.logger.log_result(self, self._process(dictionary))\n-        except ProcessLaterError as e:\n-            self.process_after = e.process_after\n-            self.save()\n-            raise\n-        if self.date_processed is None:\n-            self.date_processed = now\n-        self.save()\n+            with NonBlockingLock.objects.acquire_lock(self):\n+                if logger:\n+                    self.logger = get_logger(logger)\n+                now = timezone.now()\n+                if not force and self.date_processed is not None:\n+                    raise AlreadyProcessedError()\n+                if not force and self.process_after and self.process_after >= now:\n+                    raise ProcessLaterError(self.process_after)\n+\n+                try:\n+                    # execute trigger\n+                    self.logger.log_result(self, self._process(dictionary))\n+                    self._handle_execution_success(use_statsd)\n+                except ProcessLaterError as e:\n+                    self.process_after = e.process_after\n+                    self.save()\n+                    raise\n+                except Exception as e:\n+                    self._handle_execution_failure(e, use_statsd)\n+                    raise\n+        except AlreadyLocked:\n+            pass\n \n     def _process(self, dictionary):\n         raise NotImplementedError()\n \n+    def _handle_execution_failure(self, exception, use_statsd=False):\n+        \"\"\"\n+        Handle execution failure of the trigger\n+        :param Exception exception: the exception raised during failure\n+        :param bool use_statsd: whether to use statsd\n+        :return: None\n+        \"\"\"\n+        self.number_of_tries += 1\n+        if self.number_of_tries >= getattr(settings, 'DJTRIGGERS_TRIES_BEFORE_WARNING', 3):\n+            message = 'Processing of %s %s raised a %s (try nb. %s) ' % (self.trigger_type,\n+                                                                         self.pk,\n+                                                                         type(exception).__name__,\n+                                                                         self.number_of_tries)\n+            self.logger.log_message(self, message, level=WARNING)\n+\n+        # Send stats to statsd if necessary\n+        if use_statsd:\n+            from django_statsd.clients import statsd\n+            statsd.incr('triggers.%s.failed' % self.trigger_type)\n+\n+        self.save()\n+\n+    def _handle_execution_success(self, use_statsd=False):\n+        \"\"\"\n+        Handle execution success of the trigger\n+        :param bool use_statsd: whether to use statsd\n+        :return: None\n+        \"\"\"\n+        now = timezone.now()\n+        if self.date_processed is None:\n+            self.date_processed = now\n+\n+        # Send stats to statsd if necessary\n+        if use_statsd:\n+            from django_statsd.clients import statsd\n+            statsd.incr('triggers.%s.processed' % self.trigger_type)","path":"djtriggers/models.py","position":133,"original_position":133,"commit_id":"724c52c4a7a707ea6a39480a0180d6439e08f405","original_commit_id":"724c52c4a7a707ea6a39480a0180d6439e08f405","user":{"login":"WimCrols","id":16096822,"avatar_url":"https://avatars.githubusercontent.com/u/16096822?v=3","gravatar_id":"","url":"https://api.github.com/users/WimCrols","html_url":"https://github.com/WimCrols","followers_url":"https://api.github.com/users/WimCrols/followers","following_url":"https://api.github.com/users/WimCrols/following{/other_user}","gists_url":"https://api.github.com/users/WimCrols/gists{/gist_id}","starred_url":"https://api.github.com/users/WimCrols/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WimCrols/subscriptions","organizations_url":"https://api.github.com/users/WimCrols/orgs","repos_url":"https://api.github.com/users/WimCrols/repos","events_url":"https://api.github.com/users/WimCrols/events{/privacy}","received_events_url":"https://api.github.com/users/WimCrols/received_events","type":"User","site_admin":false},"body":"Please prefer _format()_ function over _%_.","created_at":"2016-08-11T11:08:03Z","updated_at":"2016-08-11T11:08:03Z","html_url":"https://github.com/vikingco/django-triggers/pull/14#discussion_r74403551","pull_request_url":"https://api.github.com/repos/vikingco/django-triggers/pulls/14","_links":{"self":{"href":"https://api.github.com/repos/vikingco/django-triggers/pulls/comments/74403551"},"html":{"href":"https://github.com/vikingco/django-triggers/pull/14#discussion_r74403551"},"pull_request":{"href":"https://api.github.com/repos/vikingco/django-triggers/pulls/14"}}},"pull_request":{"url":"https://api.github.com/repos/vikingco/django-triggers/pulls/14","id":80599582,"html_url":"https://github.com/vikingco/django-triggers/pull/14","diff_url":"https://github.com/vikingco/django-triggers/pull/14.diff","patch_url":"https://github.com/vikingco/django-triggers/pull/14.patch","issue_url":"https://api.github.com/repos/vikingco/django-triggers/issues/14","number":14,"state":"open","locked":false,"title":"Mvbe 9586/handle django triggers with celery","user":{"login":"berre94","id":6872974,"avatar_url":"https://avatars.githubusercontent.com/u/6872974?v=3","gravatar_id":"","url":"https://api.github.com/users/berre94","html_url":"https://github.com/berre94","followers_url":"https://api.github.com/users/berre94/followers","following_url":"https://api.github.com/users/berre94/following{/other_user}","gists_url":"https://api.github.com/users/berre94/gists{/gist_id}","starred_url":"https://api.github.com/users/berre94/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/berre94/subscriptions","organizations_url":"https://api.github.com/users/berre94/orgs","repos_url":"https://api.github.com/users/berre94/repos","events_url":"https://api.github.com/users/berre94/events{/privacy}","received_events_url":"https://api.github.com/users/berre94/received_events","type":"User","site_admin":false},"body":"JIRA: https://vikingco.atlassian.net/browse/MVBE-9586","created_at":"2016-08-09T10:52:11Z","updated_at":"2016-08-11T11:08:03Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3928b38732b72f34f161dfb3e6943add6ad8969c","assignee":{"login":"WimCrols","id":16096822,"avatar_url":"https://avatars.githubusercontent.com/u/16096822?v=3","gravatar_id":"","url":"https://api.github.com/users/WimCrols","html_url":"https://github.com/WimCrols","followers_url":"https://api.github.com/users/WimCrols/followers","following_url":"https://api.github.com/users/WimCrols/following{/other_user}","gists_url":"https://api.github.com/users/WimCrols/gists{/gist_id}","starred_url":"https://api.github.com/users/WimCrols/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WimCrols/subscriptions","organizations_url":"https://api.github.com/users/WimCrols/orgs","repos_url":"https://api.github.com/users/WimCrols/repos","events_url":"https://api.github.com/users/WimCrols/events{/privacy}","received_events_url":"https://api.github.com/users/WimCrols/received_events","type":"User","site_admin":false},"assignees":[{"login":"WimCrols","id":16096822,"avatar_url":"https://avatars.githubusercontent.com/u/16096822?v=3","gravatar_id":"","url":"https://api.github.com/users/WimCrols","html_url":"https://github.com/WimCrols","followers_url":"https://api.github.com/users/WimCrols/followers","following_url":"https://api.github.com/users/WimCrols/following{/other_user}","gists_url":"https://api.github.com/users/WimCrols/gists{/gist_id}","starred_url":"https://api.github.com/users/WimCrols/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/WimCrols/subscriptions","organizations_url":"https://api.github.com/users/WimCrols/orgs","repos_url":"https://api.github.com/users/WimCrols/repos","events_url":"https://api.github.com/users/WimCrols/events{/privacy}","received_events_url":"https://api.github.com/users/WimCrols/received_events","type":"User","site_admin":false}],"milestone":null,"commits_url":"https://api.github.com/repos/vikingco/django-triggers/pulls/14/commits","review_comments_url":"https://api.github.com/repos/vikingco/django-triggers/pulls/14/comments","review_comment_url":"https://api.github.com/repos/vikingco/django-triggers/pulls/comments{/number}","comments_url":"https://api.github.com/repos/vikingco/django-triggers/issues/14/comments","statuses_url":"https://api.github.com/repos/vikingco/django-triggers/statuses/724c52c4a7a707ea6a39480a0180d6439e08f405","head":{"label":"vikingco:MVBE-9586/handle-django-triggers-with-celery","ref":"MVBE-9586/handle-django-triggers-with-celery","sha":"724c52c4a7a707ea6a39480a0180d6439e08f405","user":{"login":"vikingco","id":145713,"avatar_url":"https://avatars.githubusercontent.com/u/145713?v=3","gravatar_id":"","url":"https://api.github.com/users/vikingco","html_url":"https://github.com/vikingco","followers_url":"https://api.github.com/users/vikingco/followers","following_url":"https://api.github.com/users/vikingco/following{/other_user}","gists_url":"https://api.github.com/users/vikingco/gists{/gist_id}","starred_url":"https://api.github.com/users/vikingco/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vikingco/subscriptions","organizations_url":"https://api.github.com/users/vikingco/orgs","repos_url":"https://api.github.com/users/vikingco/repos","events_url":"https://api.github.com/users/vikingco/events{/privacy}","received_events_url":"https://api.github.com/users/vikingco/received_events","type":"Organization","site_admin":false},"repo":{"id":6056373,"name":"django-triggers","full_name":"vikingco/django-triggers","owner":{"login":"vikingco","id":145713,"avatar_url":"https://avatars.githubusercontent.com/u/145713?v=3","gravatar_id":"","url":"https://api.github.com/users/vikingco","html_url":"https://github.com/vikingco","followers_url":"https://api.github.com/users/vikingco/followers","following_url":"https://api.github.com/users/vikingco/following{/other_user}","gists_url":"https://api.github.com/users/vikingco/gists{/gist_id}","starred_url":"https://api.github.com/users/vikingco/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vikingco/subscriptions","organizations_url":"https://api.github.com/users/vikingco/orgs","repos_url":"https://api.github.com/users/vikingco/repos","events_url":"https://api.github.com/users/vikingco/events{/privacy}","received_events_url":"https://api.github.com/users/vikingco/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/vikingco/django-triggers","description":"Triggering, persistent scheduling and processing of events.","fork":false,"url":"https://api.github.com/repos/vikingco/django-triggers","forks_url":"https://api.github.com/repos/vikingco/django-triggers/forks","keys_url":"https://api.github.com/repos/vikingco/django-triggers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vikingco/django-triggers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vikingco/django-triggers/teams","hooks_url":"https://api.github.com/repos/vikingco/django-triggers/hooks","issue_events_url":"https://api.github.com/repos/vikingco/django-triggers/issues/events{/number}","events_url":"https://api.github.com/repos/vikingco/django-triggers/events","assignees_url":"https://api.github.com/repos/vikingco/django-triggers/assignees{/user}","branches_url":"https://api.github.com/repos/vikingco/django-triggers/branches{/branch}","tags_url":"https://api.github.com/repos/vikingco/django-triggers/tags","blobs_url":"https://api.github.com/repos/vikingco/django-triggers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vikingco/django-triggers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vikingco/django-triggers/git/refs{/sha}","trees_url":"https://api.github.com/repos/vikingco/django-triggers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vikingco/django-triggers/statuses/{sha}","languages_url":"https://api.github.com/repos/vikingco/django-triggers/languages","stargazers_url":"https://api.github.com/repos/vikingco/django-triggers/stargazers","contributors_url":"https://api.github.com/repos/vikingco/django-triggers/contributors","subscribers_url":"https://api.github.com/repos/vikingco/django-triggers/subscribers","subscription_url":"https://api.github.com/repos/vikingco/django-triggers/subscription","commits_url":"https://api.github.com/repos/vikingco/django-triggers/commits{/sha}","git_commits_url":"https://api.github.com/repos/vikingco/django-triggers/git/commits{/sha}","comments_url":"https://api.github.com/repos/vikingco/django-triggers/comments{/number}","issue_comment_url":"https://api.github.com/repos/vikingco/django-triggers/issues/comments{/number}","contents_url":"https://api.github.com/repos/vikingco/django-triggers/contents/{+path}","compare_url":"https://api.github.com/repos/vikingco/django-triggers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vikingco/django-triggers/merges","archive_url":"https://api.github.com/repos/vikingco/django-triggers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vikingco/django-triggers/downloads","issues_url":"https://api.github.com/repos/vikingco/django-triggers/issues{/number}","pulls_url":"https://api.github.com/repos/vikingco/django-triggers/pulls{/number}","milestones_url":"https://api.github.com/repos/vikingco/django-triggers/milestones{/number}","notifications_url":"https://api.github.com/repos/vikingco/django-triggers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vikingco/django-triggers/labels{/name}","releases_url":"https://api.github.com/repos/vikingco/django-triggers/releases{/id}","deployments_url":"https://api.github.com/repos/vikingco/django-triggers/deployments","created_at":"2012-10-03T06:43:12Z","updated_at":"2016-03-18T16:06:03Z","pushed_at":"2016-08-10T13:58:38Z","git_url":"git://github.com/vikingco/django-triggers.git","ssh_url":"git@github.com:vikingco/django-triggers.git","clone_url":"https://github.com/vikingco/django-triggers.git","svn_url":"https://github.com/vikingco/django-triggers","homepage":null,"size":64,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":1,"default_branch":"master"}},"base":{"label":"vikingco:master","ref":"master","sha":"2f40c384d266c4b4ca2542dbb569fa996c666a37","user":{"login":"vikingco","id":145713,"avatar_url":"https://avatars.githubusercontent.com/u/145713?v=3","gravatar_id":"","url":"https://api.github.com/users/vikingco","html_url":"https://github.com/vikingco","followers_url":"https://api.github.com/users/vikingco/followers","following_url":"https://api.github.com/users/vikingco/following{/other_user}","gists_url":"https://api.github.com/users/vikingco/gists{/gist_id}","starred_url":"https://api.github.com/users/vikingco/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vikingco/subscriptions","organizations_url":"https://api.github.com/users/vikingco/orgs","repos_url":"https://api.github.com/users/vikingco/repos","events_url":"https://api.github.com/users/vikingco/events{/privacy}","received_events_url":"https://api.github.com/users/vikingco/received_events","type":"Organization","site_admin":false},"repo":{"id":6056373,"name":"django-triggers","full_name":"vikingco/django-triggers","owner":{"login":"vikingco","id":145713,"avatar_url":"https://avatars.githubusercontent.com/u/145713?v=3","gravatar_id":"","url":"https://api.github.com/users/vikingco","html_url":"https://github.com/vikingco","followers_url":"https://api.github.com/users/vikingco/followers","following_url":"https://api.github.com/users/vikingco/following{/other_user}","gists_url":"https://api.github.com/users/vikingco/gists{/gist_id}","starred_url":"https://api.github.com/users/vikingco/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vikingco/subscriptions","organizations_url":"https://api.github.com/users/vikingco/orgs","repos_url":"https://api.github.com/users/vikingco/repos","events_url":"https://api.github.com/users/vikingco/events{/privacy}","received_events_url":"https://api.github.com/users/vikingco/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/vikingco/django-triggers","description":"Triggering, persistent scheduling and processing of events.","fork":false,"url":"https://api.github.com/repos/vikingco/django-triggers","forks_url":"https://api.github.com/repos/vikingco/django-triggers/forks","keys_url":"https://api.github.com/repos/vikingco/django-triggers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vikingco/django-triggers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vikingco/django-triggers/teams","hooks_url":"https://api.github.com/repos/vikingco/django-triggers/hooks","issue_events_url":"https://api.github.com/repos/vikingco/django-triggers/issues/events{/number}","events_url":"https://api.github.com/repos/vikingco/django-triggers/events","assignees_url":"https://api.github.com/repos/vikingco/django-triggers/assignees{/user}","branches_url":"https://api.github.com/repos/vikingco/django-triggers/branches{/branch}","tags_url":"https://api.github.com/repos/vikingco/django-triggers/tags","blobs_url":"https://api.github.com/repos/vikingco/django-triggers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vikingco/django-triggers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vikingco/django-triggers/git/refs{/sha}","trees_url":"https://api.github.com/repos/vikingco/django-triggers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vikingco/django-triggers/statuses/{sha}","languages_url":"https://api.github.com/repos/vikingco/django-triggers/languages","stargazers_url":"https://api.github.com/repos/vikingco/django-triggers/stargazers","contributors_url":"https://api.github.com/repos/vikingco/django-triggers/contributors","subscribers_url":"https://api.github.com/repos/vikingco/django-triggers/subscribers","subscription_url":"https://api.github.com/repos/vikingco/django-triggers/subscription","commits_url":"https://api.github.com/repos/vikingco/django-triggers/commits{/sha}","git_commits_url":"https://api.github.com/repos/vikingco/django-triggers/git/commits{/sha}","comments_url":"https://api.github.com/repos/vikingco/django-triggers/comments{/number}","issue_comment_url":"https://api.github.com/repos/vikingco/django-triggers/issues/comments{/number}","contents_url":"https://api.github.com/repos/vikingco/django-triggers/contents/{+path}","compare_url":"https://api.github.com/repos/vikingco/django-triggers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vikingco/django-triggers/merges","archive_url":"https://api.github.com/repos/vikingco/django-triggers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vikingco/django-triggers/downloads","issues_url":"https://api.github.com/repos/vikingco/django-triggers/issues{/number}","pulls_url":"https://api.github.com/repos/vikingco/django-triggers/pulls{/number}","milestones_url":"https://api.github.com/repos/vikingco/django-triggers/milestones{/number}","notifications_url":"https://api.github.com/repos/vikingco/django-triggers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vikingco/django-triggers/labels{/name}","releases_url":"https://api.github.com/repos/vikingco/django-triggers/releases{/id}","deployments_url":"https://api.github.com/repos/vikingco/django-triggers/deployments","created_at":"2012-10-03T06:43:12Z","updated_at":"2016-03-18T16:06:03Z","pushed_at":"2016-08-10T13:58:38Z","git_url":"git://github.com/vikingco/django-triggers.git","ssh_url":"git@github.com:vikingco/django-triggers.git","clone_url":"https://github.com/vikingco/django-triggers.git","svn_url":"https://github.com/vikingco/django-triggers","homepage":null,"size":64,"stargazers_count":1,"watchers_count":1,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":1,"forks":1,"open_issues":1,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/vikingco/django-triggers/pulls/14"},"html":{"href":"https://github.com/vikingco/django-triggers/pull/14"},"issue":{"href":"https://api.github.com/repos/vikingco/django-triggers/issues/14"},"comments":{"href":"https://api.github.com/repos/vikingco/django-triggers/issues/14/comments"},"review_comments":{"href":"https://api.github.com/repos/vikingco/django-triggers/pulls/14/comments"},"review_comment":{"href":"https://api.github.com/repos/vikingco/django-triggers/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/vikingco/django-triggers/pulls/14/commits"},"statuses":{"href":"https://api.github.com/repos/vikingco/django-triggers/statuses/724c52c4a7a707ea6a39480a0180d6439e08f405"}}}}
{"actor":{"display_login":"WimCrols"}}