{"action":"created","comment":{"url":"https://api.github.com/repos/jembi/openhim-core-js/pulls/comments/26655815","id":26655815,"diff_hunk":"@@ -0,0 +1,314 @@\n+TaskModel = require('./model/tasks').Task\n+Channel = require('./model/channels').Channel\n+Q = require(\"q\")\n+logger = require(\"winston\")\n+config = require(\"./config/config\")\n+config.rerun = config.get('rerun')\n+http = require 'http'\n+TransactionModel = require(\"./model/transactions\").Transaction\n+net = require \"net\"\n+rerunMiddleware = require \"./middleware/rerunUpdateTransactionTask\"\n+\n+\n+live = false\n+activeTasks = 0\n+\n+\n+findAndProcessAQueuedTask = ->\n+  TaskModel.findOneAndUpdate { status: 'Queued' }, { status: 'Processing' }, (err, task) ->\n+    if err\n+      logger.err \"An error occurred while looking for rerun tasks: #{err}\"\n+    else if task\n+      activeTasks++\n+      processNextTaskRound task, (err) ->\n+        logger.err \"An error occurred while processing rerun task #{task._id}: #{err}\" if err\n+        activeTasks--\n+        # task has finished its current round, pick up the next one\n+        if live then findAndProcessAQueuedTask()\n+\n+      # task has started processing, pick up the next one\n+      if live then findAndProcessAQueuedTask()\n+\n+rerunTaskProcessor = ->\n+  if live\n+    findAndProcessAQueuedTask()\n+    setTimeout rerunTaskProcessor, config.rerun.processor.pollPeriodMillis\n+\n+\n+exports.start = (callback) ->\n+  live = true\n+  setTimeout rerunTaskProcessor, config.rerun.processor.pollPeriodMillis\n+\n+  logger.info \"Started rerun task processor\"\n+  callback()\n+\n+\n+exports.stop = (callback) ->\n+  live = false\n+\n+  waitForActiveTasks = ->","path":"src/tasks.coffee","position":49,"original_position":49,"commit_id":"555d8affe6885c67c3241afc3e260c2f3f6378d4","original_commit_id":"555d8affe6885c67c3241afc3e260c2f3f6378d4","user":{"login":"rcrichton","id":266576,"avatar_url":"https://avatars.githubusercontent.com/u/266576?v=3","gravatar_id":"","url":"https://api.github.com/users/rcrichton","html_url":"https://github.com/rcrichton","followers_url":"https://api.github.com/users/rcrichton/followers","following_url":"https://api.github.com/users/rcrichton/following{/other_user}","gists_url":"https://api.github.com/users/rcrichton/gists{/gist_id}","starred_url":"https://api.github.com/users/rcrichton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rcrichton/subscriptions","organizations_url":"https://api.github.com/users/rcrichton/orgs","repos_url":"https://api.github.com/users/rcrichton/repos","events_url":"https://api.github.com/users/rcrichton/events{/privacy}","received_events_url":"https://api.github.com/users/rcrichton/received_events","type":"User","site_admin":false},"body":"It may be useful to use a array of promises for running tasks rather than setting a timeout. Not sure how feasible this is.","created_at":"2015-03-18T11:44:14Z","updated_at":"2015-03-18T11:44:14Z","html_url":"https://github.com/jembi/openhim-core-js/pull/442#discussion_r26655815","pull_request_url":"https://api.github.com/repos/jembi/openhim-core-js/pulls/442","_links":{"self":{"href":"https://api.github.com/repos/jembi/openhim-core-js/pulls/comments/26655815"},"html":{"href":"https://github.com/jembi/openhim-core-js/pull/442#discussion_r26655815"},"pull_request":{"href":"https://api.github.com/repos/jembi/openhim-core-js/pulls/442"}}},"pull_request":{"url":"https://api.github.com/repos/jembi/openhim-core-js/pulls/442","id":31335437,"html_url":"https://github.com/jembi/openhim-core-js/pull/442","diff_url":"https://github.com/jembi/openhim-core-js/pull/442.diff","patch_url":"https://github.com/jembi/openhim-core-js/pull/442.patch","issue_url":"https://api.github.com/repos/jembi/openhim-core-js/issues/442","number":442,"state":"open","locked":false,"title":"Tasks batching strategy","user":{"login":"devcritter","id":1872071,"avatar_url":"https://avatars.githubusercontent.com/u/1872071?v=3","gravatar_id":"","url":"https://api.github.com/users/devcritter","html_url":"https://github.com/devcritter","followers_url":"https://api.github.com/users/devcritter/followers","following_url":"https://api.github.com/users/devcritter/following{/other_user}","gists_url":"https://api.github.com/users/devcritter/gists{/gist_id}","starred_url":"https://api.github.com/users/devcritter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/devcritter/subscriptions","organizations_url":"https://api.github.com/users/devcritter/orgs","repos_url":"https://api.github.com/users/devcritter/repos","events_url":"https://api.github.com/users/devcritter/events{/privacy}","received_events_url":"https://api.github.com/users/devcritter/received_events","type":"User","site_admin":false},"body":"Closes #438 \r\nCloses #435 \r\nCloses #434 \r\n\r\n@rcrichton could you please review?\r\n\r\nNote I've replace monq with a custom processor. The model works a bit differently now, only the tasks collection is used to manage reruns and there is no separate queue.\r\n\r\nWhen a task is first inserted, it's marked as queued. When \"dequeued\" (i.e. picked up and marked as \"Processing\"), tasks are processed in rounds where a `batchSize` number of transactions are ran. After completing the round, the task is then again marked as queued and made available to be picked up again.\r\n\r\nThis is more scalable as multiple core instances can then process tasks (different tasks are processed in parallel, but there'll never be more than a batchSize number of transactions running at a moment for a specific task). The tasks API now only inserts tasks in the collection, marked as Queued, but doesn't do anything else. The rerun process (or processes on multiple instances) simply monitor the collection for queued items.\r\n\r\nIt's also more robust as it allows for graceful shutdowns - between rounds we can check whether the task has been paused, cancelled or if the server has been stopped; but since the check is done between rounds, currently running transactions will first finish before stopping a task.\r\n\r\n@BMartinos @debonair since you guys done a lot of work on this, it would be great if you could take a look as well to see if it makes sense?","created_at":"2015-03-17T13:42:32Z","updated_at":"2015-03-18T11:44:14Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7ae46b7abae63c4e177eefbbdd8341e51ef919e9","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/jembi/openhim-core-js/pulls/442/commits","review_comments_url":"https://api.github.com/repos/jembi/openhim-core-js/pulls/442/comments","review_comment_url":"https://api.github.com/repos/jembi/openhim-core-js/pulls/comments{/number}","comments_url":"https://api.github.com/repos/jembi/openhim-core-js/issues/442/comments","statuses_url":"https://api.github.com/repos/jembi/openhim-core-js/statuses/555d8affe6885c67c3241afc3e260c2f3f6378d4","head":{"label":"jembi:tasks-batching-strategy","ref":"tasks-batching-strategy","sha":"555d8affe6885c67c3241afc3e260c2f3f6378d4","user":{"login":"jembi","id":1931511,"avatar_url":"https://avatars.githubusercontent.com/u/1931511?v=3","gravatar_id":"","url":"https://api.github.com/users/jembi","html_url":"https://github.com/jembi","followers_url":"https://api.github.com/users/jembi/followers","following_url":"https://api.github.com/users/jembi/following{/other_user}","gists_url":"https://api.github.com/users/jembi/gists{/gist_id}","starred_url":"https://api.github.com/users/jembi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jembi/subscriptions","organizations_url":"https://api.github.com/users/jembi/orgs","repos_url":"https://api.github.com/users/jembi/repos","events_url":"https://api.github.com/users/jembi/events{/privacy}","received_events_url":"https://api.github.com/users/jembi/received_events","type":"Organization","site_admin":false},"repo":{"id":16686826,"name":"openhim-core-js","full_name":"jembi/openhim-core-js","owner":{"login":"jembi","id":1931511,"avatar_url":"https://avatars.githubusercontent.com/u/1931511?v=3","gravatar_id":"","url":"https://api.github.com/users/jembi","html_url":"https://github.com/jembi","followers_url":"https://api.github.com/users/jembi/followers","following_url":"https://api.github.com/users/jembi/following{/other_user}","gists_url":"https://api.github.com/users/jembi/gists{/gist_id}","starred_url":"https://api.github.com/users/jembi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jembi/subscriptions","organizations_url":"https://api.github.com/users/jembi/orgs","repos_url":"https://api.github.com/users/jembi/repos","events_url":"https://api.github.com/users/jembi/events{/privacy}","received_events_url":"https://api.github.com/users/jembi/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/jembi/openhim-core-js","description":"The open health information mediator core component","fork":false,"url":"https://api.github.com/repos/jembi/openhim-core-js","forks_url":"https://api.github.com/repos/jembi/openhim-core-js/forks","keys_url":"https://api.github.com/repos/jembi/openhim-core-js/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jembi/openhim-core-js/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jembi/openhim-core-js/teams","hooks_url":"https://api.github.com/repos/jembi/openhim-core-js/hooks","issue_events_url":"https://api.github.com/repos/jembi/openhim-core-js/issues/events{/number}","events_url":"https://api.github.com/repos/jembi/openhim-core-js/events","assignees_url":"https://api.github.com/repos/jembi/openhim-core-js/assignees{/user}","branches_url":"https://api.github.com/repos/jembi/openhim-core-js/branches{/branch}","tags_url":"https://api.github.com/repos/jembi/openhim-core-js/tags","blobs_url":"https://api.github.com/repos/jembi/openhim-core-js/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jembi/openhim-core-js/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jembi/openhim-core-js/git/refs{/sha}","trees_url":"https://api.github.com/repos/jembi/openhim-core-js/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jembi/openhim-core-js/statuses/{sha}","languages_url":"https://api.github.com/repos/jembi/openhim-core-js/languages","stargazers_url":"https://api.github.com/repos/jembi/openhim-core-js/stargazers","contributors_url":"https://api.github.com/repos/jembi/openhim-core-js/contributors","subscribers_url":"https://api.github.com/repos/jembi/openhim-core-js/subscribers","subscription_url":"https://api.github.com/repos/jembi/openhim-core-js/subscription","commits_url":"https://api.github.com/repos/jembi/openhim-core-js/commits{/sha}","git_commits_url":"https://api.github.com/repos/jembi/openhim-core-js/git/commits{/sha}","comments_url":"https://api.github.com/repos/jembi/openhim-core-js/comments{/number}","issue_comment_url":"https://api.github.com/repos/jembi/openhim-core-js/issues/comments{/number}","contents_url":"https://api.github.com/repos/jembi/openhim-core-js/contents/{+path}","compare_url":"https://api.github.com/repos/jembi/openhim-core-js/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jembi/openhim-core-js/merges","archive_url":"https://api.github.com/repos/jembi/openhim-core-js/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jembi/openhim-core-js/downloads","issues_url":"https://api.github.com/repos/jembi/openhim-core-js/issues{/number}","pulls_url":"https://api.github.com/repos/jembi/openhim-core-js/pulls{/number}","milestones_url":"https://api.github.com/repos/jembi/openhim-core-js/milestones{/number}","notifications_url":"https://api.github.com/repos/jembi/openhim-core-js/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jembi/openhim-core-js/labels{/name}","releases_url":"https://api.github.com/repos/jembi/openhim-core-js/releases{/id}","created_at":"2014-02-10T06:41:52Z","updated_at":"2015-03-18T11:16:35Z","pushed_at":"2015-03-18T11:16:35Z","git_url":"git://github.com/jembi/openhim-core-js.git","ssh_url":"git@github.com:jembi/openhim-core-js.git","clone_url":"https://github.com/jembi/openhim-core-js.git","svn_url":"https://github.com/jembi/openhim-core-js","homepage":"","size":7653,"stargazers_count":3,"watchers_count":3,"language":"CoffeeScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":28,"forks":3,"open_issues":28,"watchers":3,"default_branch":"master"}},"base":{"label":"jembi:master","ref":"master","sha":"d75bf32257c023d36b1d78700822f447aadaf597","user":{"login":"jembi","id":1931511,"avatar_url":"https://avatars.githubusercontent.com/u/1931511?v=3","gravatar_id":"","url":"https://api.github.com/users/jembi","html_url":"https://github.com/jembi","followers_url":"https://api.github.com/users/jembi/followers","following_url":"https://api.github.com/users/jembi/following{/other_user}","gists_url":"https://api.github.com/users/jembi/gists{/gist_id}","starred_url":"https://api.github.com/users/jembi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jembi/subscriptions","organizations_url":"https://api.github.com/users/jembi/orgs","repos_url":"https://api.github.com/users/jembi/repos","events_url":"https://api.github.com/users/jembi/events{/privacy}","received_events_url":"https://api.github.com/users/jembi/received_events","type":"Organization","site_admin":false},"repo":{"id":16686826,"name":"openhim-core-js","full_name":"jembi/openhim-core-js","owner":{"login":"jembi","id":1931511,"avatar_url":"https://avatars.githubusercontent.com/u/1931511?v=3","gravatar_id":"","url":"https://api.github.com/users/jembi","html_url":"https://github.com/jembi","followers_url":"https://api.github.com/users/jembi/followers","following_url":"https://api.github.com/users/jembi/following{/other_user}","gists_url":"https://api.github.com/users/jembi/gists{/gist_id}","starred_url":"https://api.github.com/users/jembi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jembi/subscriptions","organizations_url":"https://api.github.com/users/jembi/orgs","repos_url":"https://api.github.com/users/jembi/repos","events_url":"https://api.github.com/users/jembi/events{/privacy}","received_events_url":"https://api.github.com/users/jembi/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/jembi/openhim-core-js","description":"The open health information mediator core component","fork":false,"url":"https://api.github.com/repos/jembi/openhim-core-js","forks_url":"https://api.github.com/repos/jembi/openhim-core-js/forks","keys_url":"https://api.github.com/repos/jembi/openhim-core-js/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jembi/openhim-core-js/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jembi/openhim-core-js/teams","hooks_url":"https://api.github.com/repos/jembi/openhim-core-js/hooks","issue_events_url":"https://api.github.com/repos/jembi/openhim-core-js/issues/events{/number}","events_url":"https://api.github.com/repos/jembi/openhim-core-js/events","assignees_url":"https://api.github.com/repos/jembi/openhim-core-js/assignees{/user}","branches_url":"https://api.github.com/repos/jembi/openhim-core-js/branches{/branch}","tags_url":"https://api.github.com/repos/jembi/openhim-core-js/tags","blobs_url":"https://api.github.com/repos/jembi/openhim-core-js/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jembi/openhim-core-js/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jembi/openhim-core-js/git/refs{/sha}","trees_url":"https://api.github.com/repos/jembi/openhim-core-js/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jembi/openhim-core-js/statuses/{sha}","languages_url":"https://api.github.com/repos/jembi/openhim-core-js/languages","stargazers_url":"https://api.github.com/repos/jembi/openhim-core-js/stargazers","contributors_url":"https://api.github.com/repos/jembi/openhim-core-js/contributors","subscribers_url":"https://api.github.com/repos/jembi/openhim-core-js/subscribers","subscription_url":"https://api.github.com/repos/jembi/openhim-core-js/subscription","commits_url":"https://api.github.com/repos/jembi/openhim-core-js/commits{/sha}","git_commits_url":"https://api.github.com/repos/jembi/openhim-core-js/git/commits{/sha}","comments_url":"https://api.github.com/repos/jembi/openhim-core-js/comments{/number}","issue_comment_url":"https://api.github.com/repos/jembi/openhim-core-js/issues/comments{/number}","contents_url":"https://api.github.com/repos/jembi/openhim-core-js/contents/{+path}","compare_url":"https://api.github.com/repos/jembi/openhim-core-js/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jembi/openhim-core-js/merges","archive_url":"https://api.github.com/repos/jembi/openhim-core-js/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jembi/openhim-core-js/downloads","issues_url":"https://api.github.com/repos/jembi/openhim-core-js/issues{/number}","pulls_url":"https://api.github.com/repos/jembi/openhim-core-js/pulls{/number}","milestones_url":"https://api.github.com/repos/jembi/openhim-core-js/milestones{/number}","notifications_url":"https://api.github.com/repos/jembi/openhim-core-js/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jembi/openhim-core-js/labels{/name}","releases_url":"https://api.github.com/repos/jembi/openhim-core-js/releases{/id}","created_at":"2014-02-10T06:41:52Z","updated_at":"2015-03-18T11:16:35Z","pushed_at":"2015-03-18T11:16:35Z","git_url":"git://github.com/jembi/openhim-core-js.git","ssh_url":"git@github.com:jembi/openhim-core-js.git","clone_url":"https://github.com/jembi/openhim-core-js.git","svn_url":"https://github.com/jembi/openhim-core-js","homepage":"","size":7653,"stargazers_count":3,"watchers_count":3,"language":"CoffeeScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":28,"forks":3,"open_issues":28,"watchers":3,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/jembi/openhim-core-js/pulls/442"},"html":{"href":"https://github.com/jembi/openhim-core-js/pull/442"},"issue":{"href":"https://api.github.com/repos/jembi/openhim-core-js/issues/442"},"comments":{"href":"https://api.github.com/repos/jembi/openhim-core-js/issues/442/comments"},"review_comments":{"href":"https://api.github.com/repos/jembi/openhim-core-js/pulls/442/comments"},"review_comment":{"href":"https://api.github.com/repos/jembi/openhim-core-js/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/jembi/openhim-core-js/pulls/442/commits"},"statuses":{"href":"https://api.github.com/repos/jembi/openhim-core-js/statuses/555d8affe6885c67c3241afc3e260c2f3f6378d4"}}}}