Task chaining + some refactoring

This commit is contained in:
simon987
2019-02-15 22:10:02 -05:00
parent 07c0eca5aa
commit 6ca92bc0a7
31 changed files with 306 additions and 166 deletions

View File

@@ -245,7 +245,7 @@ func TestUpdateProjectConstraintFail(t *testing.T) {
func createProject(req api.CreateProjectRequest) *api.CreateProjectResponse {
r := Post("/project/create", req, nil)
r := Post("/projectChange/create", req, nil)
var resp api.CreateProjectResponse
data, _ := ioutil.ReadAll(r.Body)
@@ -257,7 +257,7 @@ func createProject(req api.CreateProjectRequest) *api.CreateProjectResponse {
func getProject(id int64) (*api.GetProjectResponse, *http.Response) {
r := Get(fmt.Sprintf("/project/get/%d", id), nil)
r := Get(fmt.Sprintf("/projectChange/get/%d", id), nil)
var getResp api.GetProjectResponse
data, _ := ioutil.ReadAll(r.Body)
@@ -269,7 +269,7 @@ func getProject(id int64) (*api.GetProjectResponse, *http.Response) {
func updateProject(request api.UpdateProjectRequest, pid int64) *api.UpdateProjectResponse {
r := Post(fmt.Sprintf("/project/update/%d", pid), request, nil)
r := Post(fmt.Sprintf("/projectChange/update/%d", pid), request, nil)
var resp api.UpdateProjectResponse
data, _ := ioutil.ReadAll(r.Body)

View File

@@ -11,7 +11,7 @@ import (
func TestCreateTaskValid(t *testing.T) {
//Make sure there is always a project for id:1
//Make sure there is always a projectChange for id:1
createProject(api.CreateProjectRequest{
Name: "Some Test name",
Version: "Test Version",
@@ -132,9 +132,9 @@ func TestCreateTaskInvalidRecipe(t *testing.T) {
func TestCreateGetTask(t *testing.T) {
//Make sure there is always a project for id:1
//Make sure there is always a projectChange for id:1
resp := createProject(api.CreateProjectRequest{
Name: "My project",
Name: "My projectChange",
Version: "1.0",
CloneUrl: "http://github.com/test/test",
GitRepo: "myrepo",
@@ -639,6 +639,57 @@ func TestReleaseTaskFail(t *testing.T) {
}
func TestTaskChain(t *testing.T) {
w := genWid()
p1 := createProject(api.CreateProjectRequest{
Name: "testtaskchain1",
Public: true,
GitRepo: "testtaskchain1",
CloneUrl: "testtaskchain1",
}).Id
p2 := createProject(api.CreateProjectRequest{
Name: "testtaskchain2",
Public: true,
GitRepo: "testtaskchain2",
CloneUrl: "testtaskchain2",
Chain: p1,
}).Id
createTask(api.CreateTaskRequest{
Project: p2,
Recipe: "###",
VerificationCount: 0,
}, w)
t1 := getTaskFromProject(p2, w).Task
releaseTask(api.ReleaseTaskRequest{
TaskId: t1.Id,
Result: storage.TR_OK,
}, w)
chained := getTaskFromProject(p1, w).Task
if chained.VerificationCount != t1.VerificationCount {
t.Error()
}
if chained.Recipe != t1.Recipe {
t.Error()
}
if chained.MaxRetries != t1.MaxRetries {
t.Error()
}
if chained.Priority != t1.Priority {
t.Error()
}
if chained.Status != storage.NEW {
t.Error()
}
}
func createTask(request api.CreateTaskRequest, worker *storage.Worker) *api.CreateTaskResponse {
r := Post("/task/create", request, worker)

View File

@@ -6,7 +6,6 @@ import (
"crypto/hmac"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/simon987/task_tracker/config"
"github.com/simon987/task_tracker/storage"
"io"
@@ -51,12 +50,10 @@ func Get(path string, worker *storage.Worker) *http.Response {
if worker != nil {
fmt.Println(worker.Secret)
mac := hmac.New(crypto.SHA256.New, worker.Secret)
mac.Write([]byte(path))
sig := hex.EncodeToString(mac.Sum(nil))
fmt.Println(strconv.FormatInt(worker.Id, 10))
req.Header.Add("X-Worker-Id", strconv.FormatInt(worker.Id, 10))
req.Header.Add("X-Signature", sig)
}

View File

@@ -18,6 +18,7 @@ CREATE TABLE project
id SERIAL PRIMARY KEY NOT NULL,
priority INTEGER DEFAULT 0 NOT NULL,
closed_task_count INT DEFAULT 0 NOT NULL,
chain INT DEFAULT NULL REFERENCES project (id),
public boolean NOT NULL,
hidden boolean NOT NULL,
name TEXT UNIQUE NOT NULL,
@@ -101,9 +102,18 @@ CREATE TABLE worker_requests_access_to_project
CREATE OR REPLACE FUNCTION on_task_delete_proc() RETURNS TRIGGER AS
$$
DECLARE
chain INTEGER;
BEGIN
UPDATE project SET closed_task_count=closed_task_count + 1 WHERE id = OLD.project;
UPDATE project SET closed_task_count=closed_task_count + 1 WHERE id = OLD.project returning project.chain into chain;
UPDATE worker SET closed_task_count=closed_task_count + 1 WHERE id = OLD.assignee;
IF chain != 0 THEN
INSERT into task (hash64, project, assignee, max_assign_time, assign_time, verification_count,
priority, retries, max_retries, status, recipe)
VALUES (old.hash64, chain, NULL, old.max_assign_time, NULL,
old.verification_count, old.priority, 0, old.max_retries, 1,
old.recipe);
end if;
RETURN OLD;
END;
$$ LANGUAGE 'plpgsql';