feat(be): remove task field from output table. rename log function.

This commit is contained in:
Denis Gukov 2025-01-05 14:09:56 +05:00
parent 4e11016543
commit 24cc613f1c
No known key found for this signature in database
GPG Key ID: 044381366A5D4731
9 changed files with 65 additions and 48 deletions

View File

@ -36,7 +36,7 @@ func AddTask(w http.ResponseWriter, r *http.Request) {
helpers.WriteErrorStatus(w, "No active subscription available.", http.StatusForbidden) helpers.WriteErrorStatus(w, "No active subscription available.", http.StatusForbidden)
return return
} else if err != nil { } else if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Cannot write new event to database"}) util.LogErrorF(err, log.Fields{"error": "Cannot write new event to database"})
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -63,7 +63,7 @@ func GetTasksList(w http.ResponseWriter, r *http.Request, limit int) {
} }
if err != nil { if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot get tasks list from database"}) util.LogErrorF(err, log.Fields{"error": "Bad request. Cannot get tasks list from database"})
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
@ -99,14 +99,14 @@ func GetTaskMiddleware(next http.Handler) http.Handler {
taskID, err := helpers.GetIntParam("task_id", w, r) taskID, err := helpers.GetIntParam("task_id", w, r)
if err != nil { if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot get task_id from request"}) util.LogErrorF(err, log.Fields{"error": "Bad request. Cannot get task_id from request"})
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
task, err := helpers.Store(r).GetTask(project.ID, taskID) task, err := helpers.Store(r).GetTask(project.ID, taskID)
if err != nil { if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot get task from database"}) util.LogErrorF(err, log.Fields{"error": "Bad request. Cannot get task from database"})
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
@ -125,7 +125,7 @@ func GetTaskStages(w http.ResponseWriter, r *http.Request) {
output, err := helpers.Store(r).GetTaskOutputs(project.ID, task.ID) output, err := helpers.Store(r).GetTaskOutputs(project.ID, task.ID)
if err != nil { if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot get task output from database"}) util.LogErrorF(err, log.Fields{"error": "Bad request. Cannot get task output from database"})
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
@ -142,7 +142,7 @@ func GetTaskOutput(w http.ResponseWriter, r *http.Request) {
output, err := helpers.Store(r).GetTaskOutputs(project.ID, task.ID) output, err := helpers.Store(r).GetTaskOutputs(project.ID, task.ID)
if err != nil { if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot get task output from database"}) util.LogErrorF(err, log.Fields{"error": "Bad request. Cannot get task output from database"})
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
@ -235,7 +235,7 @@ func RemoveTask(w http.ResponseWriter, r *http.Request) {
err := helpers.Store(r).DeleteTaskWithOutputs(project.ID, targetTask.ID) err := helpers.Store(r).DeleteTaskWithOutputs(project.ID, targetTask.ID)
if err != nil { if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot delete task from database"}) util.LogErrorF(err, log.Fields{"error": "Bad request. Cannot delete task from database"})
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }
@ -283,7 +283,7 @@ func GetTaskStats(w http.ResponseWriter, r *http.Request) {
stats, err := helpers.Store(r).GetTaskStats(project.ID, tplID, db.TaskStatUnitDay, filter) stats, err := helpers.Store(r).GetTaskStats(project.ID, tplID, db.TaskStatUnitDay, filter)
if err != nil { if err != nil {
util.LogErrorWithFields(err, log.Fields{"error": "Bad request. Cannot get task stats from database"}) util.LogErrorF(err, log.Fields{"error": "Bad request. Cannot get task stats from database"})
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }

View File

@ -6,9 +6,9 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/semaphoreui/semaphore/util"
"github.com/gorilla/context" "github.com/gorilla/context"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/semaphoreui/semaphore/util"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -44,15 +44,18 @@ type connection struct {
func (c *connection) readPump() { func (c *connection) readPump() {
defer func() { defer func() {
h.unregister <- c h.unregister <- c
util.LogErrorWithFields(c.ws.Close(), log.Fields{"error": "Error closing websocket"}) _ = c.ws.Close()
//util.LogErrorWithFields(c.ws.Close(), log.Fields{"error": "Error closing websocket"})
}() }()
c.ws.SetReadLimit(maxMessageSize) c.ws.SetReadLimit(maxMessageSize)
util.LogErrorWithFields(c.ws.SetReadDeadline(time.Now().Add(pongWait)), log.Fields{"error": "Socket state corrupt"})
c.ws.SetPongHandler(func(string) error { //util.LogErrorWithFields(c.ws.SetReadDeadline(time.Now().Add(pongWait)), log.Fields{"error": "Socket state corrupt"})
util.LogErrorWithFields(c.ws.SetReadDeadline(time.Now().Add(pongWait)), log.Fields{"error": "Socket state corrupt"}) //
return nil //c.ws.SetPongHandler(func(string) error {
}) // util.LogErrorWithFields(c.ws.SetReadDeadline(time.Now().Add(pongWait)), log.Fields{"error": "Socket state corrupt"})
// return nil
//})
for { for {
_, message, err := c.ws.ReadMessage() _, message, err := c.ws.ReadMessage()
@ -69,7 +72,13 @@ func (c *connection) readPump() {
// write writes a message with the given message type and payload. // write writes a message with the given message type and payload.
func (c *connection) write(mt int, payload []byte) error { func (c *connection) write(mt int, payload []byte) error {
util.LogErrorWithFields(c.ws.SetWriteDeadline(time.Now().Add(writeWait)), log.Fields{"error": "Socket state corrupt"})
err := c.ws.SetWriteDeadline(time.Now().Add(writeWait))
util.LogErrorF(err, log.Fields{
"error": "Cannot set write deadline",
})
return c.ws.WriteMessage(mt, payload) return c.ws.WriteMessage(mt, payload)
} }
@ -79,23 +88,30 @@ func (c *connection) writePump() {
defer func() { defer func() {
ticker.Stop() ticker.Stop()
util.LogError(c.ws.Close()) _ = c.ws.Close()
//util.LogError(c.ws.Close())
}() }()
for { for {
select { select {
case message, ok := <-c.send: case message, ok := <-c.send:
if !ok { if !ok {
util.LogError(c.write(websocket.CloseMessage, []byte{})) util.LogErrorF(c.write(websocket.CloseMessage, []byte{}), log.Fields{
"error": "Cannot send close message",
})
return return
} }
if err := c.write(websocket.TextMessage, message); err != nil { if err := c.write(websocket.TextMessage, message); err != nil {
util.LogError(err) util.LogErrorF(err, log.Fields{
"error": "Cannot send text message",
})
return return
} }
case <-ticker.C: case <-ticker.C:
if err := c.write(websocket.PingMessage, []byte{}); err != nil { if err := c.write(websocket.PingMessage, []byte{}); err != nil {
util.LogError(err) util.LogErrorF(err, log.Fields{
"error": "Cannot send ping message",
})
return return
} }
} }

View File

@ -191,7 +191,6 @@ type TaskWithTpl struct {
// TaskOutput is the ansible log output from the task // TaskOutput is the ansible log output from the task
type TaskOutput struct { type TaskOutput struct {
TaskID int `db:"task_id" json:"task_id"` TaskID int `db:"task_id" json:"task_id"`
Task string `db:"task" json:"task"`
Time time.Time `db:"time" json:"time"` Time time.Time `db:"time" json:"time"`
Output string `db:"output" json:"output"` Output string `db:"output" json:"output"`
} }

View File

@ -13,15 +13,14 @@ import (
) )
var ( var (
autoIncrementRE = regexp.MustCompile(`(?i)\bautoincrement\b`) autoIncrementRE = regexp.MustCompile(`(?i)\bautoincrement\b`)
serialRE = regexp.MustCompile(`(?i)\binteger primary key autoincrement\b`) serialRE = regexp.MustCompile(`(?i)\binteger primary key autoincrement\b`)
dateTimeTypeRE = regexp.MustCompile(`(?i)\bdatetime\b`) dateTimeTypeRE = regexp.MustCompile(`(?i)\bdatetime\b`)
tinyintRE = regexp.MustCompile(`(?i)\btinyint\b`) tinyintRE = regexp.MustCompile(`(?i)\btinyint\b`)
longtextRE = regexp.MustCompile(`(?i)\blongtext\b`) longtextRE = regexp.MustCompile(`(?i)\blongtext\b`)
ifExistsRE = regexp.MustCompile(`(?i)\bif exists\b`) ifExistsRE = regexp.MustCompile(`(?i)\bif exists\b`)
changeRE = regexp.MustCompile(`^alter table \x60(\w+)\x60 change \x60(\w+)\x60 \x60(\w+)\x60 ([\w\(\)]+)( not null)?$`) changeRE = regexp.MustCompile(`^alter table \x60(\w+)\x60 change \x60(\w+)\x60 \x60(\w+)\x60 ([\w\(\)]+)( autoincrement)?( not null)?$`)
//dropForeignKeyRE = regexp.MustCompile(`^alter table \x60(\w+)\x60 drop foreign key \x60(\w+)\x60 /\* postgres:\x60(\w*)\x60 mysql:\x60(\w*)\x60 \*/$`) dropForeignKeyRE = regexp.MustCompile(`(?i)\bdrop foreign key\b`)
dropForeignKey2RE = regexp.MustCompile(`(?i)\bdrop foreign key\b`)
) )
// getVersionPath is the humanoid version with the file format appended // getVersionPath is the humanoid version with the file format appended
@ -62,7 +61,8 @@ func (d *SqlDb) prepareMigration(query string) string {
oldColumnName := m[2] oldColumnName := m[2]
newColumnName := m[3] newColumnName := m[3]
columnType := m[4] columnType := m[4]
columnNotNull := m[5] != "" //autoincrement := m[5] != ""
columnNotNull := m[6] != ""
var queries []string var queries []string
queries = append(queries, queries = append(queries,
@ -88,7 +88,7 @@ func (d *SqlDb) prepareMigration(query string) string {
query = tinyintRE.ReplaceAllString(query, "smallint") query = tinyintRE.ReplaceAllString(query, "smallint")
query = longtextRE.ReplaceAllString(query, "text") query = longtextRE.ReplaceAllString(query, "text")
query = serialRE.ReplaceAllString(query, "serial primary key") query = serialRE.ReplaceAllString(query, "serial primary key")
query = dropForeignKey2RE.ReplaceAllString(query, "drop constraint") query = dropForeignKeyRE.ReplaceAllString(query, "drop constraint")
query = identifierQuoteRE.ReplaceAllString(query, "\"") query = identifierQuoteRE.ReplaceAllString(query, "\"")
} }
return query return query

View File

@ -1 +1,3 @@
alter table `task__output` change `id` `id` bigint not null alter table `task__output` drop `task`;
alter table `task__output` change `id` `id` bigint autoincrement not null

View File

@ -25,7 +25,7 @@ type JobLogger struct {
} }
func (e *JobLogger) ActionError(err error, action string, message string) { func (e *JobLogger) ActionError(err error, action string, message string) {
util.LogErrorWithFields(err, log.Fields{ util.LogErrorF(err, log.Fields{
"type": "action", "type": "action",
"context": e.Context, "context": e.Context,
"action": action, "action": action,

View File

@ -92,6 +92,6 @@ func (p *runningJob) logPipe(reader *bufio.Reader) {
if err != nil && err.Error() != "EOF" { if err != nil && err.Error() != "EOF" {
//don't panic on these errors, sometimes it throws not dangerous "read |0: file already closed" error //don't panic on these errors, sometimes it throws not dangerous "read |0: file already closed" error
util.LogWarningWithFields(err, log.Fields{"error": "Failed to read TaskRunner output"}) util.LogWarningF(err, log.Fields{"error": "Failed to read TaskRunner output"})
} }
} }

View File

@ -127,7 +127,7 @@ func (t *TaskRunner) SetStatus(status task_logger.TaskStatus) {
func (t *TaskRunner) panicOnError(err error, msg string) { func (t *TaskRunner) panicOnError(err error, msg string) {
if err != nil { if err != nil {
t.Log(msg) t.Log(msg)
util.LogPanicWithFields(err, log.Fields{"error": msg}) util.LogPanicF(err, log.Fields{"error": msg})
} }
} }
@ -141,7 +141,7 @@ func (t *TaskRunner) logPipe(reader *bufio.Reader) {
if err.Error() != "EOF" { if err.Error() != "EOF" {
//don't panic on these errors, sometimes it throws not dangerous "read |0: file already closed" error //don't panic on these errors, sometimes it throws not dangerous "read |0: file already closed" error
util.LogWarningWithFields(err, log.Fields{"error": "Failed to read TaskRunner output"}) util.LogWarningF(err, log.Fields{"error": "Failed to read TaskRunner output"})
} }
} }

View File

@ -5,12 +5,12 @@ import (
) )
// LogWarning logs a warning with arbitrary field if error // LogWarning logs a warning with arbitrary field if error
func LogWarning(err error){ func LogWarning(err error) {
LogWarningWithFields(err, log.Fields{"level": "Warn"}) LogWarningF(err, log.Fields{"level": "Warn"})
} }
// LogWarningWithFields logs a warning with added field context if error // LogWarningF logs a warning with added field context if error
func LogWarningWithFields(err error, fields log.Fields){ func LogWarningF(err error, fields log.Fields) {
if err != nil { if err != nil {
log.WithFields(fields).Warn(err.Error()) log.WithFields(fields).Warn(err.Error())
} }
@ -18,23 +18,23 @@ func LogWarningWithFields(err error, fields log.Fields){
// LogError logs an error with arbitrary field if error // LogError logs an error with arbitrary field if error
func LogError(err error) { func LogError(err error) {
LogErrorWithFields(err, log.Fields{"level": "Error"}) LogErrorF(err, log.Fields{"level": "Error"})
} }
// LogErrorWithFields logs a error with added field context if error // LogErrorF logs a error with added field context if error
func LogErrorWithFields(err error, fields log.Fields) { func LogErrorF(err error, fields log.Fields) {
if err != nil { if err != nil {
log.WithFields(fields).Error(err.Error()) log.WithFields(fields).Error(err.Error())
} }
} }
// LogPanic logs and panics with arbitrary field if error // LogPanic logs and panics with arbitrary field if error
func LogPanic(err error){ func LogPanic(err error) {
LogPanicWithFields(err, log.Fields{"level": "Panic"}) LogPanicF(err, log.Fields{"level": "Panic"})
} }
// LogPanicWithFields logs and panics with added field context if error // LogPanicF logs and panics with added field context if error
func LogPanicWithFields(err error, fields log.Fields){ func LogPanicF(err error, fields log.Fields) {
if err != nil { if err != nil {
log.WithFields(fields).Panic(err.Error()) log.WithFields(fields).Panic(err.Error())
} }