-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
73 lines (65 loc) · 1.78 KB
/
task.go
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
package bark
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/robfig/cron/v3"
)
type BarkTaskLog struct {
ID string `json:"id"`
TaskID string `json:"taskId" storm:"index"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Buffer bytes.Buffer `json:"buffer"`
}
type BarkTask struct {
ID string `json:"id"`
ActiveID cron.EntryID `json:"activeId"`
Spec string `json:"spec"`
Description string `json:"description"`
Content string `json:"content"`
Title string `json:"title"`
DeviceKey string `json:"deviceKey"`
Icon string `json:"icon"`
}
func (taskLog *BarkTaskLog) Printf(format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(&taskLog.Buffer, format, a...)
}
func (taskLog *BarkTaskLog) Println(a ...interface{}) (n int, err error) {
return fmt.Fprintln(&taskLog.Buffer, a...)
}
func (taskLog *BarkTaskLog) UnmarshalJSON(b []byte) error {
var log struct {
ID string `json:"id"`
TaskID string `json:"taskId"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Buffer string `json:"buffer"`
}
err := json.Unmarshal(b, &log)
if err != nil {
return err
}
taskLog.Buffer = *bytes.NewBufferString(log.Buffer)
taskLog.ID = log.ID
taskLog.TaskID = log.TaskID
taskLog.Start = log.Start
taskLog.End = log.End
return nil
}
func (taskLog *BarkTaskLog) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
ID string `json:"id"`
TaskID string `json:"taskId"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Buffer string `json:"buffer"`
}{
ID: taskLog.ID,
TaskID: taskLog.TaskID,
Start: taskLog.Start,
End: taskLog.End,
Buffer: taskLog.Buffer.String(),
})
}