2024-04-12 12:32:54 +02:00
|
|
|
package task_logger
|
2022-01-30 12:22:18 +01:00
|
|
|
|
2023-08-29 00:51:04 +02:00
|
|
|
import (
|
|
|
|
"os/exec"
|
2024-03-23 20:33:48 +01:00
|
|
|
"strings"
|
2023-08-29 00:51:04 +02:00
|
|
|
"time"
|
|
|
|
)
|
2022-01-30 12:22:18 +01:00
|
|
|
|
2023-09-23 17:12:35 +02:00
|
|
|
type TaskStatus string
|
|
|
|
|
|
|
|
const (
|
2024-01-30 13:47:03 +01:00
|
|
|
TaskWaitingStatus TaskStatus = "waiting"
|
|
|
|
TaskStartingStatus TaskStatus = "starting"
|
|
|
|
TaskWaitingConfirmation TaskStatus = "waiting_confirmation"
|
|
|
|
TaskConfirmed TaskStatus = "confirmed"
|
|
|
|
TaskRunningStatus TaskStatus = "running"
|
|
|
|
TaskStoppingStatus TaskStatus = "stopping"
|
|
|
|
TaskStoppedStatus TaskStatus = "stopped"
|
|
|
|
TaskSuccessStatus TaskStatus = "success"
|
|
|
|
TaskFailStatus TaskStatus = "error"
|
2023-09-23 17:12:35 +02:00
|
|
|
)
|
|
|
|
|
2024-03-23 20:36:43 +01:00
|
|
|
func (s TaskStatus) IsNotifiable() bool {
|
|
|
|
return s == TaskSuccessStatus || s == TaskFailStatus || s == TaskWaitingConfirmation
|
|
|
|
}
|
|
|
|
|
2024-03-23 20:33:48 +01:00
|
|
|
func (s TaskStatus) Format() (res string) {
|
|
|
|
|
|
|
|
switch s {
|
|
|
|
case TaskFailStatus:
|
|
|
|
res += "❌"
|
|
|
|
case TaskSuccessStatus:
|
|
|
|
res += "✅"
|
|
|
|
case TaskStoppedStatus:
|
|
|
|
res += "⏹️"
|
|
|
|
case TaskWaitingConfirmation:
|
|
|
|
res += "⚠️"
|
|
|
|
}
|
|
|
|
res += strings.ToUpper(string(s))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-23 17:12:35 +02:00
|
|
|
func (s TaskStatus) IsFinished() bool {
|
|
|
|
return s == TaskStoppedStatus || s == TaskSuccessStatus || s == TaskFailStatus
|
|
|
|
}
|
|
|
|
|
2024-06-17 20:37:45 +02:00
|
|
|
type StatusListener func(status TaskStatus)
|
|
|
|
type LogListener func(new time.Time, msg string)
|
|
|
|
|
2022-01-30 12:22:18 +01:00
|
|
|
type Logger interface {
|
|
|
|
Log(msg string)
|
2024-04-12 12:21:05 +02:00
|
|
|
Logf(format string, a ...any)
|
|
|
|
LogWithTime(now time.Time, msg string)
|
|
|
|
LogfWithTime(now time.Time, format string, a ...any)
|
2022-01-30 12:22:18 +01:00
|
|
|
LogCmd(cmd *exec.Cmd)
|
2023-09-23 17:12:35 +02:00
|
|
|
SetStatus(status TaskStatus)
|
2024-06-17 20:37:45 +02:00
|
|
|
AddStatusListener(l StatusListener)
|
|
|
|
AddLogListener(l LogListener)
|
2022-01-30 12:22:18 +01:00
|
|
|
}
|