-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctx.go
More file actions
87 lines (73 loc) · 1.94 KB
/
Copy pathctx.go
File metadata and controls
87 lines (73 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package schedo
import (
"context"
schedo "github.com/useschedo/golang-sdk"
)
// LogLevel represents the severity level of a log entry
type LogLevel string
const (
LogLevelInfo LogLevel = "INFO"
LogLevelWarn LogLevel = "WARN"
LogLevelError LogLevel = "ERROR"
LogLevelDebug LogLevel = "DEBUG"
)
// LogEntry represents a single log entry with its level
type LogEntry struct {
Level LogLevel
Line string
}
type JobExecutionContext struct {
ExecutionID int64
JobCode string
client *schedo.Client
metadata map[string]any
logs []LogEntry
context.Context
}
func NewJobExecutionContext(ctx context.Context, client *schedo.Client, executionID int64, jobCode string, metadata map[string]any) *JobExecutionContext {
return &JobExecutionContext{
ExecutionID: executionID,
JobCode: jobCode,
client: client,
metadata: metadata,
logs: make([]LogEntry, 0),
Context: ctx,
}
}
func (j *JobExecutionContext) WriteLog(line string) error {
return j.WriteLogWithLevel(line, LogLevelInfo)
}
// WriteLogWithLevel writes a log line with the specified log level
func (j *JobExecutionContext) WriteLogWithLevel(line string, level LogLevel) error {
j.logs = append(j.logs, LogEntry{
Level: level,
Line: line,
})
return nil
}
// GetLogs returns all collected logs as a single string with each entry on a new line
// Format: [LEVEL] message
func (j *JobExecutionContext) GetLogs() string {
if len(j.logs) == 0 {
return ""
}
var result string
for i, entry := range j.logs {
if i > 0 {
result += "\n"
}
result += "[" + string(entry.Level) + "] " + entry.Line
}
return result
}
// ClearLogs removes all collected logs
func (j *JobExecutionContext) ClearLogs() {
j.logs = make([]LogEntry, 0)
}
func (j *JobExecutionContext) GetMetadataField(key string) (any, bool) {
value, ok := j.metadata[key]
return value, ok
}
func (j *JobExecutionContext) GetMetadata(key string) map[string]any {
return j.metadata
}