Skip to content

Commit f3447da

Browse files
authored
feat: masive refactor for fun (#22)
refactor: simplier code and more readable feat: progress events are not send to job_events now new table job_progress feat: add romanian tesseract lang chore: update pgstosrt to latest version fix: some minor fixes
1 parent 7c23c9b commit f3447da

File tree

31 files changed

+2256
-1556
lines changed

31 files changed

+2256
-1556
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ ENTRYPOINT ["/app/transcoderd-server"]
4141
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS builder-pgs
4242
WORKDIR /src
4343
ARG tessdata_version=ced78752cc61322fb554c280d13360b35b8684e4
44-
ARG pgstosrt_version=26a4ab214fbc18520d2999eb3d8baf8d5c84a724
44+
ARG pgstosrt_version=3123a9004cf1e163b6b7171a72deff2a899ed361
4545

4646
RUN apt-get -y update && \
4747
apt-get -y upgrade && \

Makefile

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,16 @@ help: ## show this help menu.
2121
@echo ""
2222

2323
.PHONY: fmt
24-
fmt:
24+
fmt: ## Code Format
2525
go fmt ./...
2626

27+
.PHONY: lint
28+
lint: ## Linters
29+
@golangci-lint run
2730

28-
# Install GolangCI-Lint
29-
install-lint:
30-
@echo "Installing GolangCI-Lint $(GOLANGCI_LINT_VERSION)..."
31-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s $(GOLANGCI_LINT_VERSION)
32-
33-
# Run GolangCI-Lint
34-
lint:
35-
@echo "Running GolangCI-Lint..."
36-
$(GOLANGCI_LINT_BIN) run --config=$(GOLANGCI_LINT_CONFIG)
37-
38-
# Run GolangCI-Lint and fix issues automatically
39-
lint-fix:
40-
@echo "Running GolangCI-Lint with --fix..."
41-
$(GOLANGCI_LINT_BIN) run --fix --config=$(GOLANGCI_LINT_CONFIG)
31+
.PHONY: lint-fix
32+
lint-fix: ## Lint fix if possible
33+
@golangci-lint run --fix
4234

4335

4436
.PHONY: build

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module transcoder
22

3-
go 1.22.0
3+
go 1.23.0
44

55
toolchain go1.23.3
66

@@ -15,9 +15,8 @@ require (
1515
github.com/lib/pq v1.10.9
1616
github.com/minio/selfupdate v0.6.0
1717
github.com/sirupsen/logrus v1.9.3
18-
github.com/spf13/pflag v1.0.5
18+
github.com/spf13/pflag v1.0.6
1919
github.com/spf13/viper v1.19.0
20-
gopkg.in/errgo.v2 v2.1.0
2120
gopkg.in/vansante/go-ffprobe.v2 v2.2.1
2221
)
2322

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
8181
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
8282
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
8383
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
84+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
85+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
8486
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
8587
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
8688
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

helper/command/command.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Command struct {
2424
WorkDir string
2525
stdoutFunc ReaderFunc
2626
stderrFunc ReaderFunc
27+
buffSize int
2728
}
2829

2930
func NewAllowedCodesOption(codes ...int) Option {
@@ -36,10 +37,11 @@ func NewCommandByString(command string, params string) *Command {
3637
}
3738
func NewCommand(command string, params ...string) *Command {
3839
cmd := &Command{
39-
Command: command,
40-
Params: params,
41-
Env: os.Environ(),
42-
WorkDir: GetWD(),
40+
Command: command,
41+
Params: params,
42+
Env: os.Environ(),
43+
WorkDir: GetWD(),
44+
buffSize: 4096,
4345
}
4446
return cmd
4547
}
@@ -63,6 +65,11 @@ func (c *Command) AddEnv(env string) *Command {
6365
return c
6466
}
6567

68+
func (c *Command) BuffSize(size int) *Command {
69+
c.buffSize = size
70+
return c
71+
}
72+
6673
func (c *Command) SetStdoutFunc(stdoutFunc ReaderFunc) *Command {
6774
c.stdoutFunc = stdoutFunc
6875
return c
@@ -144,7 +151,7 @@ func allowedCodes(opts []Option, exitCode int) bool {
144151

145152
func (c *Command) readerStreamProcessor(ctx context.Context, wg *sync.WaitGroup, reader io.ReadCloser, callbackFunc ReaderFunc) {
146153
defer wg.Done()
147-
buffer := make([]byte, 4096)
154+
buffer := make([]byte, c.buffSize)
148155
loop:
149156
for {
150157
select {

model/model.go

Lines changed: 54 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package model
22

33
import (
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
1312
type NotificationType string
1413
type NotificationStatus string
1514
type JobAction string
16-
type TaskEvents []*TaskEvent
15+
type TaskEvents []*TaskEventType
1716

1817
const (
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

4037
type 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-
7264
type JobEvent struct {
7365
Id uuid.UUID `json:"id"`
7466
Action JobAction `json:"action"`
7567
}
7668

7769
type 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-
9176
type 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 {
249178
func (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) {
260189
func (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

Comments
 (0)