{"action":"created","comment":{"url":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls/comments/36961626","id":36961626,"diff_hunk":"@@ -0,0 +1,167 @@\n+/**\n+ * PluginManager.js\n+ *\n+ * A bare bones plugin manager.  It is meant to look very much like the linux\n+ * process management.  It creates hooks for the startup of plugins, restarts,\n+ * and halts.\n+ *\n+*/\n+\n+var path =  require('path'),\n+    thread = require('child_process');\n+\n+\n+var MAX_RESTARTS = 3;\n+\n+/*\n+ * An instance of a plugin.\n+ * @constructor\n+ * @param {string} script - the script to execute a node process on.\n+ *\n+*/\n+function Plugin(script) {\n+\n+  // the location of the program\n+  this.script = script;\n+\n+  // number of attempted restarts\n+  this.restarts = 0;\n+\n+  // the number of times to attempt a restart\n+  // in case of unknown termination signal.\n+  this.maxRestarts = MAX_RESTARTS;\n+\n+\n+  // perform the initial startup\n+  this.startup();\n+}\n+\n+\n+/** performs the initial fork, and sets up handlers */\n+Plugin.prototype.startup = function () {\n+\n+  // fork the process and assign it to this.process\n+  this.process = thread.fork(__dirname + this.script);\n+\n+  // set up the exit handler\n+  this.process.on('exit', this.exitHandler);\n+\n+  // the child fork is running\n+  this.running = true;\n+};\n+\n+\n+/** exit handler for the underlying process */\n+Plugin.prototype.exitHandler = function (code, signal) {\n+\n+  // 0 is the successful exit code\n+  // SIGTERM is emitted on process.kill()\n+  if (code !== 0 && signal !== 'SIGTERM') {\n+\n+    // if we have received more than the maxRestarts, something may be amiss\n+    // and we should quit trying to restart\n+    if (this.restarts < this.maxRestarts) {\n+      this.startup();\n+      this.restarts++;\n+    }\n+  }\n+};\n+\n+\n+/** nice event emitter for the underlying process */\n+Plugin.prototype.emit = function (event, data) {\n+  this.process.send({ event : event, data : data });\n+};\n+\n+\n+/** wraps process.kill() */\n+Plugin.prototype.kill = function (code) {\n+  this.process.kill(code);\n+};\n+\n+\n+/** wraps process.on() */\n+Plugin.prototype.register = function (event, callback) {","path":"plugins/pluginManager.js","position":84,"original_position":84,"commit_id":"9e0e3daac82ca56a1771287dbce5a7390d4ad5e0","original_commit_id":"9e0e3daac82ca56a1771287dbce5a7390d4ad5e0","user":{"login":"DedrickEnc","id":5418919,"avatar_url":"https://avatars.githubusercontent.com/u/5418919?v=3","gravatar_id":"","url":"https://api.github.com/users/DedrickEnc","html_url":"https://github.com/DedrickEnc","followers_url":"https://api.github.com/users/DedrickEnc/followers","following_url":"https://api.github.com/users/DedrickEnc/following{/other_user}","gists_url":"https://api.github.com/users/DedrickEnc/gists{/gist_id}","starred_url":"https://api.github.com/users/DedrickEnc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DedrickEnc/subscriptions","organizations_url":"https://api.github.com/users/DedrickEnc/orgs","repos_url":"https://api.github.com/users/DedrickEnc/repos","events_url":"https://api.github.com/users/DedrickEnc/events{/privacy}","received_events_url":"https://api.github.com/users/DedrickEnc/received_events","type":"User","site_admin":false},"body":"This function seems important but unused","created_at":"2015-08-13T10:46:38Z","updated_at":"2015-08-13T10:46:38Z","html_url":"https://github.com/IMA-WorldHealth/bhima/pull/554#discussion_r36961626","pull_request_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls/554","_links":{"self":{"href":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls/comments/36961626"},"html":{"href":"https://github.com/IMA-WorldHealth/bhima/pull/554#discussion_r36961626"},"pull_request":{"href":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls/554"}}},"pull_request":{"url":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls/554","id":37752871,"html_url":"https://github.com/IMA-WorldHealth/bhima/pull/554","diff_url":"https://github.com/IMA-WorldHealth/bhima/pull/554.diff","patch_url":"https://github.com/IMA-WorldHealth/bhima/pull/554.patch","issue_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/issues/554","number":554,"state":"closed","locked":false,"title":"New plugin manager; complete mail plugin rewrite","user":{"login":"jniles","id":896472,"avatar_url":"https://avatars.githubusercontent.com/u/896472?v=3","gravatar_id":"","url":"https://api.github.com/users/jniles","html_url":"https://github.com/jniles","followers_url":"https://api.github.com/users/jniles/followers","following_url":"https://api.github.com/users/jniles/following{/other_user}","gists_url":"https://api.github.com/users/jniles/gists{/gist_id}","starred_url":"https://api.github.com/users/jniles/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jniles/subscriptions","organizations_url":"https://api.github.com/users/jniles/orgs","repos_url":"https://api.github.com/users/jniles/repos","events_url":"https://api.github.com/users/jniles/events{/privacy}","received_events_url":"https://api.github.com/users/jniles/received_events","type":"User","site_admin":false},"body":"This PR implements a new plugin manager (plugins/pluginManager.js).  It is fairly bare-bones at this point, but the structure is fully functional.  This PR is mostly for review by the team, with additions to be made in the future.\r\n\r\nEach plugin is loaded in a separate thread using `child_process.fork([plugin])`.  Configuration parameters are defined in `config/environment/server.js`.  Each plugin must define a schedule using a crontab syntax.   The schedule is parsed and interpreted using [laterjs](http://bunkat.github.io/later/).\r\n\r\nThe mail plugin has been completely rewritten as an example plugin.  Currently, the daily report has been implemented, but it should be very easy to create new reports by putting a folder in `mail/reports/{report_name}/` with `queries.json`, `{report_name}.tmpl.html`, and `{report_name}/lang/en.json` or `{report_name}/lang/fr.json` with appropriate translations.  Note: the French daily report is not yet translated.  For more information on how this works, see the README (`plugins/mail/README.md`).\r\n\r\nA nice bonus with the mail plugin is that templates are now defined using handlebarsjs-like syntax.  You can now write:\r\n```html\r\n<h1> {{ this.data.patientCount }} </h1>\r\n```\r\nand use a JSON object `{ data : { patientCount : 12 } }` to render the template with the correct values.  For database queries, specifying the locality (\"en\" or \"fr\") of the currency in the query will lead to automatic currency formatting in either Congolese Francs or USD.  The code for this is located inside `plugins/mail/lib/locales.js`.\r\n\r\nAny feedback would be appreciated.  There are still some bugs to be patched, but this is my proposal for the future of bhima plugins.  In the future, I'd like to move plugins into separate repositories, making them easier to maintain.  Any comments would be welcome.","created_at":"2015-06-16T01:52:30Z","updated_at":"2015-08-13T10:46:38Z","closed_at":"2015-08-10T09:47:25Z","merged_at":"2015-08-10T09:47:25Z","merge_commit_sha":"7e7005a790f6299cb02d09488417d3868f0ac6ea","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls/554/commits","review_comments_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls/554/comments","review_comment_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls/comments{/number}","comments_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/issues/554/comments","statuses_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/statuses/9e0e3daac82ca56a1771287dbce5a7390d4ad5e0","head":{"label":"jniles:features/plugin-manager","ref":"features/plugin-manager","sha":"9e0e3daac82ca56a1771287dbce5a7390d4ad5e0","user":{"login":"jniles","id":896472,"avatar_url":"https://avatars.githubusercontent.com/u/896472?v=3","gravatar_id":"","url":"https://api.github.com/users/jniles","html_url":"https://github.com/jniles","followers_url":"https://api.github.com/users/jniles/followers","following_url":"https://api.github.com/users/jniles/following{/other_user}","gists_url":"https://api.github.com/users/jniles/gists{/gist_id}","starred_url":"https://api.github.com/users/jniles/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jniles/subscriptions","organizations_url":"https://api.github.com/users/jniles/orgs","repos_url":"https://api.github.com/users/jniles/repos","events_url":"https://api.github.com/users/jniles/events{/privacy}","received_events_url":"https://api.github.com/users/jniles/received_events","type":"User","site_admin":false},"repo":{"id":21632374,"name":"bhima","full_name":"jniles/bhima","owner":{"login":"jniles","id":896472,"avatar_url":"https://avatars.githubusercontent.com/u/896472?v=3","gravatar_id":"","url":"https://api.github.com/users/jniles","html_url":"https://github.com/jniles","followers_url":"https://api.github.com/users/jniles/followers","following_url":"https://api.github.com/users/jniles/following{/other_user}","gists_url":"https://api.github.com/users/jniles/gists{/gist_id}","starred_url":"https://api.github.com/users/jniles/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jniles/subscriptions","organizations_url":"https://api.github.com/users/jniles/orgs","repos_url":"https://api.github.com/users/jniles/repos","events_url":"https://api.github.com/users/jniles/events{/privacy}","received_events_url":"https://api.github.com/users/jniles/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jniles/bhima","description":"Continuation of the Bika HIS project with an Angular core.","fork":true,"url":"https://api.github.com/repos/jniles/bhima","forks_url":"https://api.github.com/repos/jniles/bhima/forks","keys_url":"https://api.github.com/repos/jniles/bhima/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jniles/bhima/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jniles/bhima/teams","hooks_url":"https://api.github.com/repos/jniles/bhima/hooks","issue_events_url":"https://api.github.com/repos/jniles/bhima/issues/events{/number}","events_url":"https://api.github.com/repos/jniles/bhima/events","assignees_url":"https://api.github.com/repos/jniles/bhima/assignees{/user}","branches_url":"https://api.github.com/repos/jniles/bhima/branches{/branch}","tags_url":"https://api.github.com/repos/jniles/bhima/tags","blobs_url":"https://api.github.com/repos/jniles/bhima/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jniles/bhima/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jniles/bhima/git/refs{/sha}","trees_url":"https://api.github.com/repos/jniles/bhima/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jniles/bhima/statuses/{sha}","languages_url":"https://api.github.com/repos/jniles/bhima/languages","stargazers_url":"https://api.github.com/repos/jniles/bhima/stargazers","contributors_url":"https://api.github.com/repos/jniles/bhima/contributors","subscribers_url":"https://api.github.com/repos/jniles/bhima/subscribers","subscription_url":"https://api.github.com/repos/jniles/bhima/subscription","commits_url":"https://api.github.com/repos/jniles/bhima/commits{/sha}","git_commits_url":"https://api.github.com/repos/jniles/bhima/git/commits{/sha}","comments_url":"https://api.github.com/repos/jniles/bhima/comments{/number}","issue_comment_url":"https://api.github.com/repos/jniles/bhima/issues/comments{/number}","contents_url":"https://api.github.com/repos/jniles/bhima/contents/{+path}","compare_url":"https://api.github.com/repos/jniles/bhima/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jniles/bhima/merges","archive_url":"https://api.github.com/repos/jniles/bhima/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jniles/bhima/downloads","issues_url":"https://api.github.com/repos/jniles/bhima/issues{/number}","pulls_url":"https://api.github.com/repos/jniles/bhima/pulls{/number}","milestones_url":"https://api.github.com/repos/jniles/bhima/milestones{/number}","notifications_url":"https://api.github.com/repos/jniles/bhima/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jniles/bhima/labels{/name}","releases_url":"https://api.github.com/repos/jniles/bhima/releases{/id}","created_at":"2014-07-08T23:14:42Z","updated_at":"2014-07-09T01:36:06Z","pushed_at":"2015-08-12T10:41:22Z","git_url":"git://github.com/jniles/bhima.git","ssh_url":"git@github.com:jniles/bhima.git","clone_url":"https://github.com/jniles/bhima.git","svn_url":"https://github.com/jniles/bhima","homepage":null,"size":97400,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"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":"IMA-WorldHealth:development","ref":"development","sha":"de45a3460087764b84b54c43bb8d609e04d3f6f3","user":{"login":"IMA-WorldHealth","id":5330583,"avatar_url":"https://avatars.githubusercontent.com/u/5330583?v=3","gravatar_id":"","url":"https://api.github.com/users/IMA-WorldHealth","html_url":"https://github.com/IMA-WorldHealth","followers_url":"https://api.github.com/users/IMA-WorldHealth/followers","following_url":"https://api.github.com/users/IMA-WorldHealth/following{/other_user}","gists_url":"https://api.github.com/users/IMA-WorldHealth/gists{/gist_id}","starred_url":"https://api.github.com/users/IMA-WorldHealth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/IMA-WorldHealth/subscriptions","organizations_url":"https://api.github.com/users/IMA-WorldHealth/orgs","repos_url":"https://api.github.com/users/IMA-WorldHealth/repos","events_url":"https://api.github.com/users/IMA-WorldHealth/events{/privacy}","received_events_url":"https://api.github.com/users/IMA-WorldHealth/received_events","type":"Organization","site_admin":false},"repo":{"id":15043926,"name":"bhima","full_name":"IMA-WorldHealth/bhima","owner":{"login":"IMA-WorldHealth","id":5330583,"avatar_url":"https://avatars.githubusercontent.com/u/5330583?v=3","gravatar_id":"","url":"https://api.github.com/users/IMA-WorldHealth","html_url":"https://github.com/IMA-WorldHealth","followers_url":"https://api.github.com/users/IMA-WorldHealth/followers","following_url":"https://api.github.com/users/IMA-WorldHealth/following{/other_user}","gists_url":"https://api.github.com/users/IMA-WorldHealth/gists{/gist_id}","starred_url":"https://api.github.com/users/IMA-WorldHealth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/IMA-WorldHealth/subscriptions","organizations_url":"https://api.github.com/users/IMA-WorldHealth/orgs","repos_url":"https://api.github.com/users/IMA-WorldHealth/repos","events_url":"https://api.github.com/users/IMA-WorldHealth/events{/privacy}","received_events_url":"https://api.github.com/users/IMA-WorldHealth/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/IMA-WorldHealth/bhima","description":"A full-featured hospital information system for developing countries.","fork":false,"url":"https://api.github.com/repos/IMA-WorldHealth/bhima","forks_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/forks","keys_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/keys{/key_id}","collaborators_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/teams","hooks_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/hooks","issue_events_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/issues/events{/number}","events_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/events","assignees_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/assignees{/user}","branches_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/branches{/branch}","tags_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/tags","blobs_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/git/refs{/sha}","trees_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/git/trees{/sha}","statuses_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/statuses/{sha}","languages_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/languages","stargazers_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/stargazers","contributors_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/contributors","subscribers_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/subscribers","subscription_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/subscription","commits_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/commits{/sha}","git_commits_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/git/commits{/sha}","comments_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/comments{/number}","issue_comment_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/issues/comments{/number}","contents_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/contents/{+path}","compare_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/compare/{base}...{head}","merges_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/merges","archive_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/downloads","issues_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/issues{/number}","pulls_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls{/number}","milestones_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/milestones{/number}","notifications_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/labels{/name}","releases_url":"https://api.github.com/repos/IMA-WorldHealth/bhima/releases{/id}","created_at":"2013-12-09T10:07:16Z","updated_at":"2015-07-20T14:21:57Z","pushed_at":"2015-08-12T14:45:43Z","git_url":"git://github.com/IMA-WorldHealth/bhima.git","ssh_url":"git@github.com:IMA-WorldHealth/bhima.git","clone_url":"https://github.com/IMA-WorldHealth/bhima.git","svn_url":"https://github.com/IMA-WorldHealth/bhima","homepage":"","size":99779,"stargazers_count":6,"watchers_count":6,"language":"Groff","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":9,"mirror_url":null,"open_issues_count":34,"forks":9,"open_issues":34,"watchers":6,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls/554"},"html":{"href":"https://github.com/IMA-WorldHealth/bhima/pull/554"},"issue":{"href":"https://api.github.com/repos/IMA-WorldHealth/bhima/issues/554"},"comments":{"href":"https://api.github.com/repos/IMA-WorldHealth/bhima/issues/554/comments"},"review_comments":{"href":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls/554/comments"},"review_comment":{"href":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/IMA-WorldHealth/bhima/pulls/554/commits"},"statuses":{"href":"https://api.github.com/repos/IMA-WorldHealth/bhima/statuses/9e0e3daac82ca56a1771287dbce5a7390d4ad5e0"}}}}