11package model
22
33import (
4+ "encoding/json"
45 "fmt"
56 "github.com/google/uuid"
6- log "github.com/sirupsen/logrus"
7- "os"
87 "time"
98 "transcoder/helper/max"
109)
@@ -13,28 +12,26 @@ type EventType string
1312type NotificationType string
1413type NotificationStatus string
1514type JobAction string
16- type TaskEvents []* TaskEvent
15+ type TaskEvents []* TaskEventType
1716
1817const (
1918 PingEvent EventType = "Ping"
2019 NotificationEvent EventType = "Notification"
21-
22- JobNotification NotificationType = "Job"
23- DownloadNotification NotificationType = "Download "
24- UploadNotification NotificationType = "Upload "
25- MKVExtractNotification NotificationType = "MKVExtract "
26- FFProbeNotification NotificationType = "FFProbe "
27- PGSNotification NotificationType = "PGS"
28- FFMPEGSNotification NotificationType = "FFMPEG"
29-
20+ ProgressEvent EventType = "Progress"
21+
22+ JobNotification NotificationType = "Job "
23+ DownloadNotification NotificationType = "Download "
24+ UploadNotification NotificationType = "Upload "
25+ MKVExtractNotification NotificationType = "MKVExtract "
26+ PGSNotification NotificationType = "PGS"
27+ FFMPEGSNotification NotificationType = "FFMPEG"
28+ JobVerify NotificationType = "JobVerify"
3029 QueuedNotificationStatus NotificationStatus = "queued"
3130 AssignedNotificationStatus NotificationStatus = "assigned"
3231 StartedNotificationStatus NotificationStatus = "started"
3332 CompletedNotificationStatus NotificationStatus = "completed"
3433 CanceledNotificationStatus NotificationStatus = "canceled"
3534 FailedNotificationStatus NotificationStatus = "failed"
36-
37- CancelJob JobAction = "cancel"
3835)
3936
4037type Identity interface {
@@ -64,155 +61,87 @@ type Worker struct {
6461 LastSeen time.Time
6562}
6663
67- type ControlEvent struct {
68- Event * TaskEncode
69- ControlChan chan interface {}
70- }
71-
7264type JobEvent struct {
7365 Id uuid.UUID `json:"id"`
7466 Action JobAction `json:"action"`
7567}
7668
7769type JobType string
7870
79- type TaskEncode struct {
71+ type RequestJobResponse struct {
8072 Id uuid.UUID `json:"id"`
8173 EventID int `json:"eventID"`
8274}
8375
84- type WorkTaskEncode struct {
85- TaskEncode * TaskEncode
86- WorkDir string
87- SourceFilePath string
88- TargetFilePath string
89- }
90-
9176type TaskPGS struct {
9277 PGSID int
9378 PGSSourcePath string
9479 PGSLanguage string
9580 PGSTargetPath string
9681}
9782
98- type TaskPGSResponse struct {
99- Id uuid.UUID `json:"id"`
100- PGSID int `json:"pgsid"`
101- Srt []byte `json:"srt"`
102- Err string `json:"error"`
103- Queue string `json:"queue"`
83+ type EnvelopEvent struct {
84+ EventType EventType `json:"eventType"`
85+ EventData json.RawMessage `json:"eventData"`
10486}
10587
106- func (t TaskEncode ) getUUID () uuid.UUID {
107- return t .Id
88+ type Event struct {
89+ EventTime time.Time `json:"eventTime"`
90+ WorkerName string `json:"workerName"`
10891}
109-
110- type TaskEvent struct {
111- Id uuid.UUID `json:"id "`
92+ type TaskEventType struct {
93+ Event
94+ JobId uuid.UUID `json:"Id "`
11295 EventID int `json:"eventID"`
113- EventType EventType `json:"eventType"`
114- WorkerName string `json:"workerName"`
115- EventTime time.Time `json:"eventTime"`
116- IP string `json:"ip"`
11796 NotificationType NotificationType `json:"notificationType"`
11897 Status NotificationStatus `json:"status"`
11998 Message string `json:"message"`
12099}
121100
122- type TaskStatus struct {
123- LastState * TaskEvent
124- Task * WorkTaskEncode
125- }
126-
127- func (e TaskEvent ) IsAssigned () bool {
128- if e .EventType != NotificationEvent {
129- return false
130- }
131- if e .NotificationType == JobNotification && (e .Status == AssignedNotificationStatus || e .Status == StartedNotificationStatus ) {
132- return true
133- }
134- return false
101+ type PingEventType struct {
102+ Event
103+ IP string `json:"ip"`
135104}
136105
137- func (e TaskEvent ) IsCompleted () bool {
138- if e .EventType != NotificationEvent {
139- return false
140- }
141- if e .NotificationType == JobNotification && e .Status == CompletedNotificationStatus {
142- return true
143- }
144- return false
145- }
106+ type TaskProgressStatus string
146107
147- func (e TaskEvent ) IsDownloading () bool {
148- if e .EventType != NotificationEvent {
149- return false
150- }
151- if e .NotificationType == DownloadNotification && e .Status == StartedNotificationStatus {
152- return true
153- }
108+ const (
109+ ProgressingTaskProgressTypeStatus TaskProgressStatus = "progressing"
110+ DoneTaskProgressTypeStatus TaskProgressStatus = "done"
111+ FailureTaskProgressTypeStatus TaskProgressStatus = "failure"
112+ )
154113
155- if e .NotificationType == JobNotification && (e .Status == StartedNotificationStatus ) {
156- return true
157- }
158- return false
114+ type TaskProgressType struct {
115+ Event
116+ JobId uuid.UUID `json:"jobId"`
117+ ProgressID string `json:"progressID"`
118+ Percent float64 `json:"percent"`
119+ ETA time.Duration `json:"eta"`
120+ NotificationType NotificationType `json:"notificationType"`
121+ Status TaskProgressStatus `json:"status"`
159122}
160123
161- func (e TaskEvent ) IsEncoding () bool {
162- if e .EventType != NotificationEvent {
163- return false
164- }
165- if e .NotificationType == DownloadNotification && e .Status == CompletedNotificationStatus {
166- return true
167- }
168-
169- if e .NotificationType == MKVExtractNotification && (e .Status == StartedNotificationStatus || e .Status == CompletedNotificationStatus ) {
170- return true
171- }
172- if e .NotificationType == FFProbeNotification && (e .Status == StartedNotificationStatus || e .Status == CompletedNotificationStatus ) {
173- return true
174- }
175- if e .NotificationType == PGSNotification && (e .Status == StartedNotificationStatus || e .Status == CompletedNotificationStatus ) {
176- return true
177- }
178- if e .NotificationType == FFMPEGSNotification && e .Status == StartedNotificationStatus {
124+ func (e TaskEventType ) IsAssigned () bool {
125+ if e .NotificationType == JobNotification && (e .Status == AssignedNotificationStatus || e .Status == StartedNotificationStatus ) {
179126 return true
180127 }
181-
182128 return false
183129}
184130
185- func (e TaskEvent ) IsUploading () bool {
186- if e .EventType != NotificationEvent {
187- return false
188- }
189- if e .NotificationType == FFMPEGSNotification && e .Status == CompletedNotificationStatus {
190- return true
191- }
192-
193- if e .NotificationType == UploadNotification && e .Status == StartedNotificationStatus {
131+ func (e TaskEventType ) IsCompleted () bool {
132+ if e .NotificationType == JobNotification && e .Status == CompletedNotificationStatus {
194133 return true
195134 }
196-
197135 return false
198136}
199137
200- func (w * WorkTaskEncode ) Clean () error {
201- log .Debugf ("[%s] Cleaning up Task Workspace" , w .TaskEncode .Id .String ())
202- err := os .RemoveAll (w .WorkDir )
203- if err != nil {
204- return err
205- }
206- return nil
207- }
208-
209- func (t TaskEvents ) GetLatest () * TaskEvent {
138+ func (t TaskEvents ) GetLatest () * TaskEventType {
210139 if len (t ) == 0 {
211140 return nil
212141 }
213- return max .Max (t ).(* TaskEvent )
142+ return max .Max (t ).(* TaskEventType )
214143}
215- func (t TaskEvents ) GetLatestPerNotificationType (notificationType NotificationType ) (returnEvent * TaskEvent ) {
144+ func (t TaskEvents ) GetLatestPerNotificationType (notificationType NotificationType ) (returnEvent * TaskEventType ) {
216145 eventID := - 1
217146 for _ , event := range t {
218147 if event .NotificationType == notificationType && event .EventID > eventID {
@@ -249,7 +178,7 @@ func (t TaskEvents) Less(i, j int) bool {
249178func (t TaskEvents ) Swap (i , j int ) {
250179 t [i ], t [j ] = t [j ], t [i ]
251180}
252- func (t TaskEvents ) GetByEventId (i int ) (* TaskEvent , error ) {
181+ func (t TaskEvents ) GetByEventId (i int ) (* TaskEventType , error ) {
253182 for _ , event := range t {
254183 if event .EventID == i {
255184 return event , nil
@@ -260,22 +189,23 @@ func (t TaskEvents) GetByEventId(i int) (*TaskEvent, error) {
260189func (t TaskEvents ) GetLastElement (i int ) interface {} {
261190 return t [i ]
262191}
263- func (v * Job ) AddEvent (eventType EventType , notificationType NotificationType , notificationStatus NotificationStatus ) (newEvent * TaskEvent ) {
264- return v .AddEventComplete (eventType , notificationType , notificationStatus , "" )
192+ func (v * Job ) AddEvent (notificationType NotificationType , notificationStatus NotificationStatus ) (newEvent * TaskEventType ) {
193+ return v .AddEventComplete (notificationType , notificationStatus , "" )
265194}
266195
267- func (v * Job ) AddEventComplete (eventType EventType , notificationType NotificationType , notificationStatus NotificationStatus , message string ) (newEvent * TaskEvent ) {
196+ func (v * Job ) AddEventComplete (notificationType NotificationType , notificationStatus NotificationStatus , message string ) (newEvent * TaskEventType ) {
268197 latestEvent := v .Events .GetLatest ()
269198 newEventID := 0
270199 if latestEvent != nil {
271200 newEventID = latestEvent .EventID + 1
272201 }
273202
274- newEvent = & TaskEvent {
275- Id : v .Id ,
203+ newEvent = & TaskEventType {
204+ Event : Event {
205+ EventTime : time .Now (),
206+ },
207+ JobId : v .Id ,
276208 EventID : newEventID ,
277- EventType : eventType ,
278- EventTime : time .Now (),
279209 NotificationType : notificationType ,
280210 Status : notificationStatus ,
281211 Message : message ,
0 commit comments