Skip to content

Commit c4618f8

Browse files
committed
feat: server supports to cancel jobs
chore(deps): update ffmpeg build script to latest
1 parent 9c33d80 commit c4618f8

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

Dockerfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
ARG BASE_IMAGE=ubuntu:22.04@sha256:77906da86b60585ce12215807090eb327e7386c8fafb5402369e421f44eff17e
1+
ARG BASE_IMAGE=ubuntu:24.04
22
FROM ${BASE_IMAGE} AS builder-ffmpeg
33

4-
ARG FFMPEG_BUILD_SCRIPT_VERSION=1.48
5-
64
ENV DEBIAN_FRONTEND=noninteractive
75

86
RUN apt-get update \
@@ -20,9 +18,10 @@ RUN apt-get update \
2018

2119
WORKDIR /app
2220

21+
ARG FFMPEG_BUILD_SCRIPT_VERSION=7ea5427c1f91d5eb3400f47dfe5e425844e4af35
2322
# ADD doesn't cache when used from URL
2423
RUN curl -sLO \
25-
https://raw.githubusercontent.com/markus-perl/ffmpeg-build-script/v${FFMPEG_BUILD_SCRIPT_VERSION}/build-ffmpeg && \
24+
https://raw.githubusercontent.com/markus-perl/ffmpeg-build-script/${FFMPEG_BUILD_SCRIPT_VERSION}/build-ffmpeg && \
2625
chmod 755 ./build-ffmpeg && \
2726
SKIPINSTALL=yes ./build-ffmpeg \
2827
--build \

server/scheduler/scheduler.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Scheduler interface {
3131
GetChecksum(ctx context.Context, uuid string) (string, error)
3232
RequestJob(ctx context.Context, workerName string) (*model.TaskEncode, error)
3333
HandleWorkerEvent(ctx context.Context, taskEvent *model.TaskEvent) error
34+
CancelJob(ctx context.Context, id string) error
3435
}
3536

3637
type SchedulerConfig struct {
@@ -97,6 +98,30 @@ func (R *RuntimeScheduler) HandleWorkerEvent(ctx context.Context, jobEvent *mode
9798
return nil
9899
}
99100

101+
func (R *RuntimeScheduler) CancelJob(ctx context.Context, id string) error {
102+
job, err := R.repo.GetJob(ctx, id)
103+
if err != nil {
104+
return err
105+
}
106+
107+
status := job.Events.GetStatus()
108+
switch {
109+
case status == model.CompletedNotificationStatus:
110+
return fmt.Errorf("job already completed")
111+
case status == model.FailedNotificationStatus:
112+
return fmt.Errorf("job is failed")
113+
case status == model.CanceledNotificationStatus:
114+
return fmt.Errorf("job already canceled")
115+
case status == model.AssignedNotificationStatus, status == model.StartedNotificationStatus:
116+
newEvent := job.AddEventComplete(model.NotificationEvent, model.JobNotification, model.CanceledNotificationStatus, "Job canceled by user")
117+
err = R.repo.AddNewTaskEvent(ctx, newEvent)
118+
if err != nil {
119+
return err
120+
}
121+
}
122+
return fmt.Errorf("job %s is in unknown state", id)
123+
}
124+
100125
func (R *RuntimeScheduler) processEvent(ctx context.Context, taskEvent *model.TaskEvent) error {
101126
var err error
102127
switch taskEvent.EventType {

server/web/web.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ func (W *WebServer) addJobs(writer http.ResponseWriter, request *http.Request) {
9898
writer.Write(b)
9999
}
100100

101+
func (W *WebServer) cancelJobs(writer http.ResponseWriter, request *http.Request) {
102+
vars := mux.Vars(request)
103+
jobId := vars["jobid"]
104+
if jobId == "" {
105+
webError(writer, fmt.Errorf("jobId is mandatory"), 400)
106+
return
107+
}
108+
err := W.scheduler.CancelJob(W.ctx, jobId)
109+
if webError(writer, err, 500) {
110+
return
111+
}
112+
}
113+
101114
func (W *WebServer) upload(writer http.ResponseWriter, request *http.Request) {
102115
workerName := request.Header.Get("workerName")
103116
if workerName == "" {
@@ -251,6 +264,7 @@ func NewWebServer(config WebServerConfig, scheduler scheduler.Scheduler, updater
251264
},
252265
}
253266
rtr.Handle("/api/v1/job/", webServer.AuthFunc(webServer.addJobs)).Methods("POST")
267+
rtr.Handle("/api/v1/job/{jobid}", webServer.AuthFunc(webServer.cancelJobs)).Methods("DELETE")
254268
rtr.Handle("/api/v1/job/request", webServer.AuthFunc(webServer.requestJob)).Methods("GET")
255269
rtr.Handle("/api/v1/event", webServer.AuthFunc(webServer.handleWorkerEvent)).Methods("POST")
256270
rtr.HandleFunc("/api/v1/download", webServer.download).Methods("GET")

0 commit comments

Comments
 (0)