mirror of
https://github.com/semaphoreui/semaphore.git
synced 2025-01-19 23:09:21 +01:00
feat(be): remove task field from output table. rename log function.
This commit is contained in:
parent
4e11016543
commit
24cc613f1c
@ -36,7 +36,7 @@ func AddTask(w http.ResponseWriter, r *http.Request) {
|
||||
helpers.WriteErrorStatus(w, "No active subscription available.", http.StatusForbidden)
|
||||
return
|
||||
} 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)
|
||||
return
|
||||
}
|
||||
@ -63,7 +63,7 @@ func GetTasksList(w http.ResponseWriter, r *http.Request, limit int) {
|
||||
}
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
@ -99,14 +99,14 @@ func GetTaskMiddleware(next http.Handler) http.Handler {
|
||||
taskID, err := helpers.GetIntParam("task_id", w, r)
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
task, err := helpers.Store(r).GetTask(project.ID, taskID)
|
||||
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)
|
||||
return
|
||||
}
|
||||
@ -125,7 +125,7 @@ func GetTaskStages(w http.ResponseWriter, r *http.Request) {
|
||||
output, err := helpers.Store(r).GetTaskOutputs(project.ID, task.ID)
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
@ -142,7 +142,7 @@ func GetTaskOutput(w http.ResponseWriter, r *http.Request) {
|
||||
output, err := helpers.Store(r).GetTaskOutputs(project.ID, task.ID)
|
||||
|
||||
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)
|
||||
return
|
||||
}
|
||||
@ -235,7 +235,7 @@ func RemoveTask(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
err := helpers.Store(r).DeleteTaskWithOutputs(project.ID, targetTask.ID)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
@ -6,9 +6,9 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/semaphoreui/semaphore/util"
|
||||
"github.com/gorilla/context"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/semaphoreui/semaphore/util"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@ -44,15 +44,18 @@ type connection struct {
|
||||
func (c *connection) readPump() {
|
||||
defer func() {
|
||||
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)
|
||||
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"})
|
||||
return nil
|
||||
})
|
||||
|
||||
//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"})
|
||||
// return nil
|
||||
//})
|
||||
|
||||
for {
|
||||
_, message, err := c.ws.ReadMessage()
|
||||
@ -69,7 +72,13 @@ func (c *connection) readPump() {
|
||||
|
||||
// write writes a message with the given message type and payload.
|
||||
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)
|
||||
}
|
||||
|
||||
@ -79,23 +88,30 @@ func (c *connection) writePump() {
|
||||
|
||||
defer func() {
|
||||
ticker.Stop()
|
||||
util.LogError(c.ws.Close())
|
||||
_ = c.ws.Close()
|
||||
//util.LogError(c.ws.Close())
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case message, ok := <-c.send:
|
||||
if !ok {
|
||||
util.LogError(c.write(websocket.CloseMessage, []byte{}))
|
||||
util.LogErrorF(c.write(websocket.CloseMessage, []byte{}), log.Fields{
|
||||
"error": "Cannot send close message",
|
||||
})
|
||||
return
|
||||
}
|
||||
if err := c.write(websocket.TextMessage, message); err != nil {
|
||||
util.LogError(err)
|
||||
util.LogErrorF(err, log.Fields{
|
||||
"error": "Cannot send text message",
|
||||
})
|
||||
return
|
||||
}
|
||||
case <-ticker.C:
|
||||
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
|
||||
util.LogError(err)
|
||||
util.LogErrorF(err, log.Fields{
|
||||
"error": "Cannot send ping message",
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -191,7 +191,6 @@ type TaskWithTpl struct {
|
||||
// TaskOutput is the ansible log output from the task
|
||||
type TaskOutput struct {
|
||||
TaskID int `db:"task_id" json:"task_id"`
|
||||
Task string `db:"task" json:"task"`
|
||||
Time time.Time `db:"time" json:"time"`
|
||||
Output string `db:"output" json:"output"`
|
||||
}
|
||||
|
@ -13,15 +13,14 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
autoIncrementRE = regexp.MustCompile(`(?i)\bautoincrement\b`)
|
||||
serialRE = regexp.MustCompile(`(?i)\binteger primary key autoincrement\b`)
|
||||
dateTimeTypeRE = regexp.MustCompile(`(?i)\bdatetime\b`)
|
||||
tinyintRE = regexp.MustCompile(`(?i)\btinyint\b`)
|
||||
longtextRE = regexp.MustCompile(`(?i)\blongtext\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)?$`)
|
||||
//dropForeignKeyRE = regexp.MustCompile(`^alter table \x60(\w+)\x60 drop foreign key \x60(\w+)\x60 /\* postgres:\x60(\w*)\x60 mysql:\x60(\w*)\x60 \*/$`)
|
||||
dropForeignKey2RE = regexp.MustCompile(`(?i)\bdrop foreign key\b`)
|
||||
autoIncrementRE = regexp.MustCompile(`(?i)\bautoincrement\b`)
|
||||
serialRE = regexp.MustCompile(`(?i)\binteger primary key autoincrement\b`)
|
||||
dateTimeTypeRE = regexp.MustCompile(`(?i)\bdatetime\b`)
|
||||
tinyintRE = regexp.MustCompile(`(?i)\btinyint\b`)
|
||||
longtextRE = regexp.MustCompile(`(?i)\blongtext\b`)
|
||||
ifExistsRE = regexp.MustCompile(`(?i)\bif exists\b`)
|
||||
changeRE = regexp.MustCompile(`^alter table \x60(\w+)\x60 change \x60(\w+)\x60 \x60(\w+)\x60 ([\w\(\)]+)( autoincrement)?( not null)?$`)
|
||||
dropForeignKeyRE = regexp.MustCompile(`(?i)\bdrop foreign key\b`)
|
||||
)
|
||||
|
||||
// getVersionPath is the humanoid version with the file format appended
|
||||
@ -62,7 +61,8 @@ func (d *SqlDb) prepareMigration(query string) string {
|
||||
oldColumnName := m[2]
|
||||
newColumnName := m[3]
|
||||
columnType := m[4]
|
||||
columnNotNull := m[5] != ""
|
||||
//autoincrement := m[5] != ""
|
||||
columnNotNull := m[6] != ""
|
||||
|
||||
var queries []string
|
||||
queries = append(queries,
|
||||
@ -88,7 +88,7 @@ func (d *SqlDb) prepareMigration(query string) string {
|
||||
query = tinyintRE.ReplaceAllString(query, "smallint")
|
||||
query = longtextRE.ReplaceAllString(query, "text")
|
||||
query = serialRE.ReplaceAllString(query, "serial primary key")
|
||||
query = dropForeignKey2RE.ReplaceAllString(query, "drop constraint")
|
||||
query = dropForeignKeyRE.ReplaceAllString(query, "drop constraint")
|
||||
query = identifierQuoteRE.ReplaceAllString(query, "\"")
|
||||
}
|
||||
return query
|
||||
|
@ -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
|
@ -25,7 +25,7 @@ type JobLogger struct {
|
||||
}
|
||||
|
||||
func (e *JobLogger) ActionError(err error, action string, message string) {
|
||||
util.LogErrorWithFields(err, log.Fields{
|
||||
util.LogErrorF(err, log.Fields{
|
||||
"type": "action",
|
||||
"context": e.Context,
|
||||
"action": action,
|
||||
|
@ -92,6 +92,6 @@ func (p *runningJob) logPipe(reader *bufio.Reader) {
|
||||
|
||||
if err != nil && err.Error() != "EOF" {
|
||||
//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"})
|
||||
}
|
||||
}
|
||||
|
@ -127,7 +127,7 @@ func (t *TaskRunner) SetStatus(status task_logger.TaskStatus) {
|
||||
func (t *TaskRunner) panicOnError(err error, msg string) {
|
||||
if err != nil {
|
||||
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" {
|
||||
//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"})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,12 +5,12 @@ import (
|
||||
)
|
||||
|
||||
// LogWarning logs a warning with arbitrary field if error
|
||||
func LogWarning(err error){
|
||||
LogWarningWithFields(err, log.Fields{"level": "Warn"})
|
||||
func LogWarning(err error) {
|
||||
LogWarningF(err, log.Fields{"level": "Warn"})
|
||||
}
|
||||
|
||||
// LogWarningWithFields logs a warning with added field context if error
|
||||
func LogWarningWithFields(err error, fields log.Fields){
|
||||
// LogWarningF logs a warning with added field context if error
|
||||
func LogWarningF(err error, fields log.Fields) {
|
||||
if err != nil {
|
||||
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
|
||||
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
|
||||
func LogErrorWithFields(err error, fields log.Fields) {
|
||||
// LogErrorF logs a error with added field context if error
|
||||
func LogErrorF(err error, fields log.Fields) {
|
||||
if err != nil {
|
||||
log.WithFields(fields).Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// LogPanic logs and panics with arbitrary field if error
|
||||
func LogPanic(err error){
|
||||
LogPanicWithFields(err, log.Fields{"level": "Panic"})
|
||||
func LogPanic(err error) {
|
||||
LogPanicF(err, log.Fields{"level": "Panic"})
|
||||
}
|
||||
|
||||
// LogPanicWithFields logs and panics with added field context if error
|
||||
func LogPanicWithFields(err error, fields log.Fields){
|
||||
// LogPanicF logs and panics with added field context if error
|
||||
func LogPanicF(err error, fields log.Fields) {
|
||||
if err != nil {
|
||||
log.WithFields(fields).Panic(err.Error())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user