{"action":"created","comment":{"url":"https://api.github.com/repos/RackHD/on-core/pulls/comments/52049563","id":52049563,"diff_hunk":"@@ -0,0 +1,493 @@\n+// Copyright 2015, EMC, Inc.\n+'use strict';\n+\n+module.exports = taskGraphFactory;\n+taskGraphFactory.$provide = 'TaskGraph.TaskGraph';\n+taskGraphFactory.$inject = [\n+    'Task.taskLibrary',\n+    'TaskGraph.Store',\n+    'Constants',\n+    'Assert',\n+    'uuid',\n+    'Promise',\n+    '_'\n+];\n+\n+function taskGraphFactory(\n+    taskLibrary,\n+    store,\n+    Constants,\n+    assert,\n+    uuid,\n+    Promise,\n+    _\n+) {\n+    function TaskGraph(definition, context, domain) {\n+        this.definition = definition;\n+\n+        if (this.definition.options && this.definition.options.instanceId) {\n+            this.instanceId = this.definition.options.instanceId;\n+        } else {\n+            this.instanceId = uuid.v4();\n+        }\n+\n+        // Bool\n+        this.serviceGraph = this.definition.serviceGraph;\n+        this.context = context || {};\n+        this.context.graphId = this.instanceId;\n+        this.domain = domain || Constants.DefaultTaskDomain;\n+        this.name = this.definition.friendlyName;\n+        this.injectableName = this.definition.injectableName;\n+\n+        this.tasks = {};\n+\n+        // TODO: find instances of 'valid' elsewhere and replace from valid to Pending\n+        // TODO: replace _status with status\n+        this._status = Constants.TaskStates.Pending;\n+\n+        this.logContext = {\n+            graphInstance: this.instanceId,\n+            graphName: this.name\n+        };\n+        if (this.context.target) {\n+            this.logContext.id = this.context.target;\n+        }\n+        // For database ref linking\n+        this.node = this.context.target || this.definition.options.nodeId;\n+\n+        return this;\n+    }\n+\n+    TaskGraph.prototype._visitGraphNode = function(\n+            taskId, parentTaskName, markers, nonTerminalOnStates) {\n+        var self = this;\n+        var task = self.tasks[taskId];\n+        var marker = markers[taskId];\n+        if (!marker) {\n+            marker = {};\n+            markers[taskId] = marker;\n+        }\n+\n+        if (marker.temporaryMark) {\n+            throw new Error('Detected a cyclic graph with tasks %s and %s'.format(\n+                                task.injectableName, parentTaskName));\n+        }\n+\n+        // Only check for task.terminalOnStates being null here, not for an empty array!\n+        var _terminalOnStates = task.terminalOnStates || Constants.FinishedTaskStates;\n+        task.terminalOnStates = _.difference(_terminalOnStates, nonTerminalOnStates);\n+\n+        if (marker.permanentMark) {\n+            return;\n+        }\n+\n+        marker.temporaryMark = true;\n+\n+        _.forEach(task.waitingOn, function(value, dep) {\n+            var nonTerminalOnStates = [];\n+\n+            // This won't actually get called because we do this same check in\n+            // _populateTaskData, but keep this here just to be defensive.\n+            if (!_.has(self.tasks, dep)) {\n+                throw new Error('Graph does not contain task with ID ' + dep);\n+            }\n+            // Expand 'finished' to all finished task states\n+            if (_.contains(value, Constants.TaskStates.Finished)) {\n+                value = Constants.FinishedTaskStates;\n+                task.waitingOn[dep] = [Constants.TaskStates.Finished];\n+            }\n+            // The dependent task is no longer terminal on state <value> since the\n+            // current task lists that condition as a dependency.\n+            // task.nonTerminalOnStates gets substracted from task.terminalOnStates\n+            // when we traverse to the dependent task.\n+            nonTerminalOnStates = Array.isArray(value) ? value : [value];\n+            return self._visitGraphNode(dep, task.injectableName, markers, nonTerminalOnStates);\n+        });\n+\n+        marker.permanentMark = true;\n+        marker.temporaryMark = false;\n+    };\n+\n+    TaskGraph.prototype.detectCyclesAndSetTerminalTasks = function() {\n+        var self = this;\n+        var markers = {};\n+        _.forEach(self.tasks, function(task, taskId) {\n+            self._visitGraphNode(taskId, null, markers, null);\n+        });\n+    };\n+\n+    /*\n+     * Take the tasks definitions in this.definition.tasks, generate instanceIds\n+     * to use for each task, and then create new Task objects that reference\n+     * the instanceIds in their dependencies instead of user-created task labels.\n+     */\n+    // TODO: Replace this with a proper DFS traversal instead of iterating\n+    TaskGraph.prototype._populateTaskData = function() {\n+        var self = this;\n+\n+        assert.arrayOfObject(self.definition.tasks);\n+        var idMap = _.transform(self.definition.tasks, function(result, v) {\n+            result[v.label] = uuid.v4();\n+        }, {});\n+        return Promise.map(self.definition.tasks, function(taskData) {\n+            assert.object(taskData);\n+            if (_.has(taskData, 'taskName')) {\n+                assert.string(taskData.taskName);\n+            } else if (_.has(taskData, 'taskDefinition')) {\n+                assert.object(taskData.taskDefinition);\n+            } else {\n+                throw new Error(\"All TaskGraph tasks should have either a taskName\" +\n+                    \" or taskDefinition property.\");\n+            }\n+\n+            _.forEach(_.keys(taskData.waitOn), function(waitOnTask) {\n+                var newWaitOnTaskKey = idMap[waitOnTask];\n+                assert.ok(newWaitOnTaskKey, 'Task to wait on does not exist: ' + waitOnTask);\n+                var waitOnTaskValue = taskData.waitOn[waitOnTask];\n+                delete taskData.waitOn[waitOnTask];\n+                taskData.waitOn[newWaitOnTaskKey] = waitOnTaskValue;\n+            });\n+            var taskOverrides = {\n+                instanceId: idMap[taskData.label],\n+                waitingOn: taskData.waitOn,\n+                ignoreFailure: taskData.ignoreFailure\n+            };\n+\n+            if (taskData.taskName) {\n+                return self.constructTaskObject(taskData.taskName, taskOverrides,\n+                        taskData.optionOverrides, taskData.label)","path":"lib/workflow/task-graph.js","position":158,"original_position":158,"commit_id":"a434d9946477fbba6190ad72ff78e853f5d61470","original_commit_id":"a434d9946477fbba6190ad72ff78e853f5d61470","user":{"login":"benbp","id":1020379,"avatar_url":"https://avatars.githubusercontent.com/u/1020379?v=3","gravatar_id":"","url":"https://api.github.com/users/benbp","html_url":"https://github.com/benbp","followers_url":"https://api.github.com/users/benbp/followers","following_url":"https://api.github.com/users/benbp/following{/other_user}","gists_url":"https://api.github.com/users/benbp/gists{/gist_id}","starred_url":"https://api.github.com/users/benbp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benbp/subscriptions","organizations_url":"https://api.github.com/users/benbp/orgs","repos_url":"https://api.github.com/users/benbp/repos","events_url":"https://api.github.com/users/benbp/events{/privacy}","received_events_url":"https://api.github.com/users/benbp/received_events","type":"User","site_admin":false},"body":"Oh, or you mean:\r\n\r\n```\r\nreturn self.constructTaskObject(taskData.taskName, taskOverrides,\r\n    taskData.optionOverrides, taskData.label)\r\n```","created_at":"2016-02-05T17:57:08Z","updated_at":"2016-02-05T17:57:08Z","html_url":"https://github.com/RackHD/on-core/pull/75#discussion_r52049563","pull_request_url":"https://api.github.com/repos/RackHD/on-core/pulls/75","_links":{"self":{"href":"https://api.github.com/repos/RackHD/on-core/pulls/comments/52049563"},"html":{"href":"https://github.com/RackHD/on-core/pull/75#discussion_r52049563"},"pull_request":{"href":"https://api.github.com/repos/RackHD/on-core/pulls/75"}}},"pull_request":{"url":"https://api.github.com/repos/RackHD/on-core/pulls/75","id":58087639,"html_url":"https://github.com/RackHD/on-core/pull/75","diff_url":"https://github.com/RackHD/on-core/pull/75.diff","patch_url":"https://github.com/RackHD/on-core/pull/75.patch","issue_url":"https://api.github.com/repos/RackHD/on-core/issues/75","number":75,"state":"open","locked":false,"title":"Workflow engine refactor","user":{"login":"benbp","id":1020379,"avatar_url":"https://avatars.githubusercontent.com/u/1020379?v=3","gravatar_id":"","url":"https://api.github.com/users/benbp","html_url":"https://github.com/benbp","followers_url":"https://api.github.com/users/benbp/followers","following_url":"https://api.github.com/users/benbp/following{/other_user}","gists_url":"https://api.github.com/users/benbp/gists{/gist_id}","starred_url":"https://api.github.com/users/benbp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benbp/subscriptions","organizations_url":"https://api.github.com/users/benbp/orgs","repos_url":"https://api.github.com/users/benbp/repos","events_url":"https://api.github.com/users/benbp/events{/privacy}","received_events_url":"https://api.github.com/users/benbp/received_events","type":"User","site_admin":false},"body":"Refactoring the workflow engine for:\r\n\r\n- High Availability\r\n- Fault Tolerance\r\n- Better performance\r\n- Better execution strategy\r\n\r\nThis PR primarily includes adding a mongo based store and an AMQP based messenger plugin layer for the workflow engine, and also moves/adds all the TaskGraph creation and validation code into this repository, so that it can be called from on-http where we do up front validation on request now.\r\n\r\nRelies on RackHD/on-taskgraph#56, https://github.com/RackHD/on-tasks/pull/116, and RackHD/on-http#112\r\n\r\nSee the description of RackHD/on-taskgraph#56 for architectural slides.","created_at":"2016-02-03T03:44:11Z","updated_at":"2016-02-05T17:57:08Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ba224639dc670f719ce08f875a7eeedb2a350aa7","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/RackHD/on-core/pulls/75/commits","review_comments_url":"https://api.github.com/repos/RackHD/on-core/pulls/75/comments","review_comment_url":"https://api.github.com/repos/RackHD/on-core/pulls/comments{/number}","comments_url":"https://api.github.com/repos/RackHD/on-core/issues/75/comments","statuses_url":"https://api.github.com/repos/RackHD/on-core/statuses/a434d9946477fbba6190ad72ff78e853f5d61470","head":{"label":"benbp:taskgraph-improvements","ref":"taskgraph-improvements","sha":"a434d9946477fbba6190ad72ff78e853f5d61470","user":{"login":"benbp","id":1020379,"avatar_url":"https://avatars.githubusercontent.com/u/1020379?v=3","gravatar_id":"","url":"https://api.github.com/users/benbp","html_url":"https://github.com/benbp","followers_url":"https://api.github.com/users/benbp/followers","following_url":"https://api.github.com/users/benbp/following{/other_user}","gists_url":"https://api.github.com/users/benbp/gists{/gist_id}","starred_url":"https://api.github.com/users/benbp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benbp/subscriptions","organizations_url":"https://api.github.com/users/benbp/orgs","repos_url":"https://api.github.com/users/benbp/repos","events_url":"https://api.github.com/users/benbp/events{/privacy}","received_events_url":"https://api.github.com/users/benbp/received_events","type":"User","site_admin":false},"repo":{"id":45488670,"name":"on-core","full_name":"benbp/on-core","owner":{"login":"benbp","id":1020379,"avatar_url":"https://avatars.githubusercontent.com/u/1020379?v=3","gravatar_id":"","url":"https://api.github.com/users/benbp","html_url":"https://github.com/benbp","followers_url":"https://api.github.com/users/benbp/followers","following_url":"https://api.github.com/users/benbp/following{/other_user}","gists_url":"https://api.github.com/users/benbp/gists{/gist_id}","starred_url":"https://api.github.com/users/benbp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benbp/subscriptions","organizations_url":"https://api.github.com/users/benbp/orgs","repos_url":"https://api.github.com/users/benbp/repos","events_url":"https://api.github.com/users/benbp/events{/privacy}","received_events_url":"https://api.github.com/users/benbp/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/benbp/on-core","description":"","fork":true,"url":"https://api.github.com/repos/benbp/on-core","forks_url":"https://api.github.com/repos/benbp/on-core/forks","keys_url":"https://api.github.com/repos/benbp/on-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/benbp/on-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/benbp/on-core/teams","hooks_url":"https://api.github.com/repos/benbp/on-core/hooks","issue_events_url":"https://api.github.com/repos/benbp/on-core/issues/events{/number}","events_url":"https://api.github.com/repos/benbp/on-core/events","assignees_url":"https://api.github.com/repos/benbp/on-core/assignees{/user}","branches_url":"https://api.github.com/repos/benbp/on-core/branches{/branch}","tags_url":"https://api.github.com/repos/benbp/on-core/tags","blobs_url":"https://api.github.com/repos/benbp/on-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/benbp/on-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/benbp/on-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/benbp/on-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/benbp/on-core/statuses/{sha}","languages_url":"https://api.github.com/repos/benbp/on-core/languages","stargazers_url":"https://api.github.com/repos/benbp/on-core/stargazers","contributors_url":"https://api.github.com/repos/benbp/on-core/contributors","subscribers_url":"https://api.github.com/repos/benbp/on-core/subscribers","subscription_url":"https://api.github.com/repos/benbp/on-core/subscription","commits_url":"https://api.github.com/repos/benbp/on-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/benbp/on-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/benbp/on-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/benbp/on-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/benbp/on-core/contents/{+path}","compare_url":"https://api.github.com/repos/benbp/on-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/benbp/on-core/merges","archive_url":"https://api.github.com/repos/benbp/on-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/benbp/on-core/downloads","issues_url":"https://api.github.com/repos/benbp/on-core/issues{/number}","pulls_url":"https://api.github.com/repos/benbp/on-core/pulls{/number}","milestones_url":"https://api.github.com/repos/benbp/on-core/milestones{/number}","notifications_url":"https://api.github.com/repos/benbp/on-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/benbp/on-core/labels{/name}","releases_url":"https://api.github.com/repos/benbp/on-core/releases{/id}","deployments_url":"https://api.github.com/repos/benbp/on-core/deployments","created_at":"2015-11-03T18:58:54Z","updated_at":"2015-11-03T18:58:55Z","pushed_at":"2016-02-03T02:35:48Z","git_url":"git://github.com/benbp/on-core.git","ssh_url":"git@github.com:benbp/on-core.git","clone_url":"https://github.com/benbp/on-core.git","svn_url":"https://github.com/benbp/on-core","homepage":null,"size":803,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"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":"RackHD:master","ref":"master","sha":"220c090b34916618c1381e9a9538b62972040593","user":{"login":"RackHD","id":13982662,"avatar_url":"https://avatars.githubusercontent.com/u/13982662?v=3","gravatar_id":"","url":"https://api.github.com/users/RackHD","html_url":"https://github.com/RackHD","followers_url":"https://api.github.com/users/RackHD/followers","following_url":"https://api.github.com/users/RackHD/following{/other_user}","gists_url":"https://api.github.com/users/RackHD/gists{/gist_id}","starred_url":"https://api.github.com/users/RackHD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RackHD/subscriptions","organizations_url":"https://api.github.com/users/RackHD/orgs","repos_url":"https://api.github.com/users/RackHD/repos","events_url":"https://api.github.com/users/RackHD/events{/privacy}","received_events_url":"https://api.github.com/users/RackHD/received_events","type":"Organization","site_admin":false},"repo":{"id":41690044,"name":"on-core","full_name":"RackHD/on-core","owner":{"login":"RackHD","id":13982662,"avatar_url":"https://avatars.githubusercontent.com/u/13982662?v=3","gravatar_id":"","url":"https://api.github.com/users/RackHD","html_url":"https://github.com/RackHD","followers_url":"https://api.github.com/users/RackHD/followers","following_url":"https://api.github.com/users/RackHD/following{/other_user}","gists_url":"https://api.github.com/users/RackHD/gists{/gist_id}","starred_url":"https://api.github.com/users/RackHD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RackHD/subscriptions","organizations_url":"https://api.github.com/users/RackHD/orgs","repos_url":"https://api.github.com/users/RackHD/repos","events_url":"https://api.github.com/users/RackHD/events{/privacy}","received_events_url":"https://api.github.com/users/RackHD/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/RackHD/on-core","description":"","fork":false,"url":"https://api.github.com/repos/RackHD/on-core","forks_url":"https://api.github.com/repos/RackHD/on-core/forks","keys_url":"https://api.github.com/repos/RackHD/on-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/RackHD/on-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/RackHD/on-core/teams","hooks_url":"https://api.github.com/repos/RackHD/on-core/hooks","issue_events_url":"https://api.github.com/repos/RackHD/on-core/issues/events{/number}","events_url":"https://api.github.com/repos/RackHD/on-core/events","assignees_url":"https://api.github.com/repos/RackHD/on-core/assignees{/user}","branches_url":"https://api.github.com/repos/RackHD/on-core/branches{/branch}","tags_url":"https://api.github.com/repos/RackHD/on-core/tags","blobs_url":"https://api.github.com/repos/RackHD/on-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/RackHD/on-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/RackHD/on-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/RackHD/on-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/RackHD/on-core/statuses/{sha}","languages_url":"https://api.github.com/repos/RackHD/on-core/languages","stargazers_url":"https://api.github.com/repos/RackHD/on-core/stargazers","contributors_url":"https://api.github.com/repos/RackHD/on-core/contributors","subscribers_url":"https://api.github.com/repos/RackHD/on-core/subscribers","subscription_url":"https://api.github.com/repos/RackHD/on-core/subscription","commits_url":"https://api.github.com/repos/RackHD/on-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/RackHD/on-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/RackHD/on-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/RackHD/on-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/RackHD/on-core/contents/{+path}","compare_url":"https://api.github.com/repos/RackHD/on-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/RackHD/on-core/merges","archive_url":"https://api.github.com/repos/RackHD/on-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/RackHD/on-core/downloads","issues_url":"https://api.github.com/repos/RackHD/on-core/issues{/number}","pulls_url":"https://api.github.com/repos/RackHD/on-core/pulls{/number}","milestones_url":"https://api.github.com/repos/RackHD/on-core/milestones{/number}","notifications_url":"https://api.github.com/repos/RackHD/on-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/RackHD/on-core/labels{/name}","releases_url":"https://api.github.com/repos/RackHD/on-core/releases{/id}","deployments_url":"https://api.github.com/repos/RackHD/on-core/deployments","created_at":"2015-08-31T17:28:32Z","updated_at":"2016-01-31T09:01:09Z","pushed_at":"2016-02-04T23:34:48Z","git_url":"git://github.com/RackHD/on-core.git","ssh_url":"git@github.com:RackHD/on-core.git","clone_url":"https://github.com/RackHD/on-core.git","svn_url":"https://github.com/RackHD/on-core","homepage":null,"size":843,"stargazers_count":9,"watchers_count":9,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":25,"mirror_url":null,"open_issues_count":2,"forks":25,"open_issues":2,"watchers":9,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/RackHD/on-core/pulls/75"},"html":{"href":"https://github.com/RackHD/on-core/pull/75"},"issue":{"href":"https://api.github.com/repos/RackHD/on-core/issues/75"},"comments":{"href":"https://api.github.com/repos/RackHD/on-core/issues/75/comments"},"review_comments":{"href":"https://api.github.com/repos/RackHD/on-core/pulls/75/comments"},"review_comment":{"href":"https://api.github.com/repos/RackHD/on-core/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/RackHD/on-core/pulls/75/commits"},"statuses":{"href":"https://api.github.com/repos/RackHD/on-core/statuses/a434d9946477fbba6190ad72ff78e853f5d61470"}}}}