Skip to content

Commit

Permalink
#6 issue (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiSaw authored and sashabaranov committed Dec 11, 2017
1 parent 4fb02c9 commit e5c6428
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 36 deletions.
2 changes: 1 addition & 1 deletion disneyland/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestGRPCJobCRUD(t *testing.T) {
t.Fail()
}

allJobs, err = c.ListJobs(ctx, &ListJobsRequest{Kind: "docker", HowMany:2})
allJobs, err = c.ListJobs(ctx, &ListJobsRequest{Kind: "docker", HowMany: 2})
checkTestErr(err, t)

if len(allJobs.Jobs) != 2 {
Expand Down
88 changes: 53 additions & 35 deletions disneyland/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
_ "github.com/lib/pq"
"strconv"
"time"
)

const PULLINGSTRQ_1 = `WITH updatedPts AS (
Expand All @@ -13,7 +14,7 @@ const PULLINGSTRQ_1 = `WITH updatedPts AS (
WHERE status=$1`
const PULLINGSTRQ_2 = ` FOR UPDATE SKIP LOCKED)
UPDATE jobs pts
SET status=$2
SET status=$2, last_modified=$3
FROM pulledPts
WHERE pulledPts.id=pts.id AND pulledPts.project=pts.project AND pulledPts.kind=pts.kind
RETURNING pts.id, pts.project, pts.status, pts.metadata, pts.input, pts.output, pts.kind)
Expand Down Expand Up @@ -49,13 +50,43 @@ func (storage *DisneylandStorage) Connect() error {
return err
}

func queryJobs(rows *sql.Rows) (*ListOfJobs, error) {
ret := &ListOfJobs{Jobs: []*Job{}}
var err error

for rows.Next() {
job := &Job{}

err := rows.Scan(
&job.Id,
&job.Project,
&job.Status,
&job.Metadata,
&job.Input,
&job.Output,
&job.Kind,
)

if err != nil {
return nil, err
}
ret.Jobs = append(ret.Jobs, job)
}
return ret, err
}

func getTime() (time.Time) {
return time.Now().UTC()
}

func (storage *DisneylandStorage) CreateJob(job *Job, creator User) (*Job, error) {
tx, err := storage.db.Begin()
if err != nil {
return nil, err
}

createdJob := &Job{}

err = tx.QueryRow(`
INSERT INTO jobs (project, status, metadata, creator, input, output, kind)
VALUES ($1, $2, $3, $4, $5, $6, $7)
Expand Down Expand Up @@ -86,6 +117,7 @@ func (storage *DisneylandStorage) GetJob(id uint64) (*Job, error) {
if err != nil {
return nil, err
}

job := &Job{}

strQuery := `SELECT id, project, status, metadata, input, output, kind
Expand All @@ -112,43 +144,20 @@ func (storage *DisneylandStorage) GetJob(id uint64) (*Job, error) {
return job, err
}

func queryJobs(rows *sql.Rows) (*ListOfJobs, error) {
ret := &ListOfJobs{Jobs: []*Job{}}
var err error

for rows.Next() {
job := &Job{}

err := rows.Scan(
&job.Id,
&job.Project,
&job.Status,
&job.Metadata,
&job.Input,
&job.Output,
&job.Kind,
)

if err != nil {
return nil, err
}
ret.Jobs = append(ret.Jobs, job)
}
return ret, err
}

func (storage *DisneylandStorage) ListJobs(howmany uint32, project string, kind string) (*ListOfJobs, error) {
tx, err := storage.db.Begin()
if err != nil {
return nil, err
}

var rows *sql.Rows
projectFlag := false
kindFlag := false
limitFlag := false
inc := 1

strQuery := LISTSTRQ_1
inc := 1

if project != "" {
strQuery += " project=$"
strQuery += strconv.Itoa(inc)
Expand All @@ -165,6 +174,7 @@ func (storage *DisneylandStorage) ListJobs(howmany uint32, project string, kind
inc++
kindFlag = true
}

if howmany != 0 {
strQuery += " LIMIT $"
strQuery += strconv.Itoa(inc)
Expand Down Expand Up @@ -208,20 +218,24 @@ func (storage *DisneylandStorage) UpdateJob(job *Job) (*Job, error) {
return nil, err
}

curTime := getTime()
resultJob := &Job{}

err = tx.QueryRow(`
UPDATE jobs
SET
status=$1,
metadata=$2,
output=$3,
kind=$4
WHERE id=$5
kind=$4,
last_modified=$5
WHERE id=$6
RETURNING id, project, status, metadata, input, output, kind;`,
job.Status,
job.Metadata,
job.Output,
job.Kind,
curTime,
job.Id,
).Scan(
&resultJob.Id,
Expand Down Expand Up @@ -249,13 +263,15 @@ func (storage *DisneylandStorage) PullJobs(howmany uint32, project string, kind
if err != nil {
return nil, err
}

var rows *sql.Rows
projectFlag := false
kindFlag := false
limitFlag := false
curTime := getTime()
inc := 4

strQuery := PULLINGSTRQ_1
inc := 3
if project != "" {
strQuery += " AND project=$"
strQuery += strconv.Itoa(inc)
Expand All @@ -279,17 +295,17 @@ func (storage *DisneylandStorage) PullJobs(howmany uint32, project string, kind
if projectFlag {
if kindFlag {
if limitFlag {
rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, project, kind, howmany)
rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, curTime, project, kind, howmany)
} else {
rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, project, kind)
rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, curTime, project, kind)
}
} else if limitFlag {
rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, project, howmany)
rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, curTime, project, howmany)
} else {
rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, project)
rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, curTime, project)
}
} else if limitFlag {
rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, kind, howmany)
rows, err = tx.Query(strQuery, Job_PENDING, Job_PULLED, curTime, kind, howmany)
}

if err != nil {
Expand All @@ -315,7 +331,9 @@ func (storage *DisneylandStorage) DeleteJob(id uint64, userProject string) (*Job
if err != nil {
return nil, err
}

resultJob := &Job{}

err = tx.QueryRow(`
DELETE FROM jobs
WHERE id=$1 AND project=$2
Expand Down
11 changes: 11 additions & 0 deletions migrations/20171123160700_create_last_modified.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DO $$
BEGIN
IF EXISTS(SELECT column_name
FROM information_schema.columns
WHERE table_name = 'jobs' AND column_name = 'last_modified')
THEN
ALTER TABLE jobs
DROP last_modified;
END IF;
END
$$
1 change: 1 addition & 0 deletions migrations/20171123160700_create_last_modified.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE jobs ADD last_modified TIMESTAMP WITHOUT TIME ZONE DEFAULT (now() AT TIME ZONE 'utc'); ALTER TABLE jobs ALTER COLUMN status SET DEFAULT 0;

0 comments on commit e5c6428

Please sign in to comment.