mirror of
https://github.com/simon987/task_tracker.git
synced 2025-12-11 14:08:52 +00:00
change worker id to serial
This commit is contained in:
@@ -4,20 +4,19 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
Id int64 `json:"id"`
|
||||
Priority int64 `json:"priority"`
|
||||
Project *Project `json:"project"`
|
||||
Assignee uuid.UUID `json:"assignee"`
|
||||
Retries int64 `json:"retries"`
|
||||
MaxRetries int64 `json:"max_retries"`
|
||||
Status string `json:"status"`
|
||||
Recipe string `json:"recipe"`
|
||||
MaxAssignTime int64 `json:"max_assign_time"`
|
||||
AssignTime int64 `json:"assign_time"`
|
||||
Id int64 `json:"id"`
|
||||
Priority int64 `json:"priority"`
|
||||
Project *Project `json:"project"`
|
||||
Assignee int64 `json:"assignee"`
|
||||
Retries int64 `json:"retries"`
|
||||
MaxRetries int64 `json:"max_retries"`
|
||||
Status string `json:"status"`
|
||||
Recipe string `json:"recipe"`
|
||||
MaxAssignTime int64 `json:"max_assign_time"`
|
||||
AssignTime int64 `json:"assign_time"`
|
||||
}
|
||||
|
||||
func (database *Database) SaveTask(task *Task, project int64, hash64 int64) error {
|
||||
@@ -53,7 +52,7 @@ func (database *Database) GetTask(worker *Worker) *Task {
|
||||
|
||||
row := db.QueryRow(`
|
||||
UPDATE task
|
||||
SET assignee=$1
|
||||
SET assignee=$1, assign_time=extract(epoch from now() at time zone 'utc')
|
||||
WHERE id IN
|
||||
(
|
||||
SELECT task.id
|
||||
@@ -71,7 +70,7 @@ func (database *Database) GetTask(worker *Worker) *Task {
|
||||
|
||||
err := row.Scan(&id)
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
logrus.WithError(err).WithFields(logrus.Fields{
|
||||
"worker": worker,
|
||||
}).Trace("No task available")
|
||||
return nil
|
||||
@@ -104,7 +103,7 @@ func getTaskById(id int64, db *sql.DB) *Task {
|
||||
return task
|
||||
}
|
||||
|
||||
func (database Database) ReleaseTask(id int64, workerId *uuid.UUID, success bool) bool {
|
||||
func (database Database) ReleaseTask(id int64, workerId int64, success bool) bool {
|
||||
|
||||
db := database.getDB()
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Identity struct {
|
||||
@@ -13,7 +12,7 @@ type Identity struct {
|
||||
}
|
||||
|
||||
type Worker struct {
|
||||
Id uuid.UUID `json:"id"`
|
||||
Id int64 `json:"id"`
|
||||
Created int64 `json:"created"`
|
||||
Identity *Identity `json:"identity"`
|
||||
Alias string `json:"alias,omitempty"`
|
||||
@@ -26,17 +25,18 @@ func (database *Database) SaveWorker(worker *Worker) {
|
||||
|
||||
identityId := getOrCreateIdentity(worker.Identity, db)
|
||||
|
||||
res, err := db.Exec("INSERT INTO worker (id, created, identity, secret, alias) VALUES ($1,$2,$3,$4,$5)",
|
||||
worker.Id, worker.Created, identityId, worker.Secret, worker.Alias)
|
||||
row := db.QueryRow("INSERT INTO worker (created, identity, secret, alias) VALUES ($1,$2,$3,$4) RETURNING id",
|
||||
worker.Created, identityId, worker.Secret, worker.Alias)
|
||||
|
||||
err := row.Scan(&worker.Id)
|
||||
handleErr(err)
|
||||
|
||||
var rowsAffected, _ = res.RowsAffected()
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"rowsAffected": rowsAffected,
|
||||
"newId": worker.Id,
|
||||
}).Trace("Database.saveWorker INSERT worker")
|
||||
}
|
||||
|
||||
func (database *Database) GetWorker(id uuid.UUID) *Worker {
|
||||
func (database *Database) GetWorker(id int64) *Worker {
|
||||
|
||||
db := database.getDB()
|
||||
|
||||
@@ -104,7 +104,7 @@ func getOrCreateIdentity(identity *Identity, db *sql.DB) int64 {
|
||||
return rowId
|
||||
}
|
||||
|
||||
func (database *Database) GrantAccess(workerId *uuid.UUID, projectId int64) bool {
|
||||
func (database *Database) GrantAccess(workerId int64, projectId int64) bool {
|
||||
|
||||
db := database.getDB()
|
||||
res, err := db.Exec(`INSERT INTO worker_has_access_to_project (worker, project) VALUES ($1,$2)
|
||||
@@ -129,7 +129,7 @@ func (database *Database) GrantAccess(workerId *uuid.UUID, projectId int64) bool
|
||||
return rowsAffected == 1
|
||||
}
|
||||
|
||||
func (database *Database) RemoveAccess(workerId *uuid.UUID, projectId int64) bool {
|
||||
func (database *Database) RemoveAccess(workerId int64, projectId int64) bool {
|
||||
|
||||
db := database.getDB()
|
||||
res, err := db.Exec(`DELETE FROM worker_has_access_to_project WHERE worker=$1 AND project=$2`,
|
||||
|
||||
Reference in New Issue
Block a user