-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
222 lines (177 loc) · 5.45 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"context"
"fmt"
wfutils "github.com/bcc-code/bcc-media-flows/utils/workflows"
miscworkflows "github.com/bcc-code/bcc-media-flows/workflows/misc"
"log"
"os"
"os/exec"
"reflect"
"runtime"
"strconv"
"time"
"github.com/bcc-code/bcc-media-flows/services/rclone"
"github.com/bcc-code/bcc-media-flows/workflows"
batonactivities "github.com/bcc-code/bcc-media-flows/activities/baton"
"github.com/bcc-code/bcc-media-flows/activities/cantemo"
"github.com/bcc-code/bcc-media-flows/environment"
"github.com/teamwork/reload"
"go.temporal.io/sdk/activity"
"github.com/bcc-code/bcc-media-flows/activities"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
selfupdate "github.com/creativeprojects/go-selfupdate"
)
var utilActivities = []any{
batonactivities.QC,
cantemo.AddRelation,
cantemo.RenameFile,
cantemo.MoveFileWait,
cantemo.GetTaskInfo,
}
// registerActivitiesInStruct registers all methods in a struct as activities
func registerActivitiesInStruct(w worker.Worker, activityStruct any) {
v := reflect.ValueOf(activityStruct)
t := reflect.TypeOf(activityStruct)
for i := 0; i < t.NumMethod(); i++ {
method := t.Method(i)
f := v.MethodByName(method.Name)
opts := activity.RegisterOptions{
Name: method.Name,
}
w.RegisterActivityWithOptions(f.Interface(), opts)
}
}
var Version = "development"
func main() {
err := update(Version)
if err != nil {
panic(err)
}
go func() {
for {
time.Sleep(5 * time.Minute)
err := update(Version)
if err != nil {
log.Printf("Error updating worker: %v", err)
}
}
}()
c, err := client.Dial(client.Options{
HostPort: os.Getenv("TEMPORAL_HOST_PORT"),
Namespace: os.Getenv("TEMPORAL_NAMESPACE"),
})
if err != nil {
panic(err)
}
defer c.Close()
identity := os.Getenv("IDENTITY")
if identity == "" {
identity = "worker"
}
activityCountString := os.Getenv("ACTIVITY_COUNT")
if activityCountString == "" {
activityCountString = "5"
}
activityCount, err := strconv.Atoi(activityCountString)
if err != nil {
panic(err)
}
if environment.GetQueue() == environment.QueueAudio {
// Test if the libfdk_aac encoder is available
cmd := exec.Command("ffmpeg", "-xerror",
"-f", "lavfi", "-xerror",
"-i", "sine=frequency=1000:duration=0.1",
"-c:a", "libfdk_aac",
"-f", "null", "-")
err := cmd.Run()
if err != nil {
panic(err)
}
if cmd.ProcessState.ExitCode() != 0 {
panic("audio worker must support ffmpeg with libfdk_aac")
}
}
ctx := context.Background()
workerOptions := worker.Options{
DeadlockDetectionTimeout: time.Hour * 3,
DisableRegistrationAliasing: true, // Recommended according to readme, default false for backwards compatibility
EnableSessionWorker: true,
Identity: identity,
LocalActivityWorkerOnly: false,
MaxConcurrentActivityExecutionSize: activityCount, // Doesn't make sense to have more than one activity running at a time
BackgroundActivityContext: context.WithValue(ctx, miscworkflows.ClientContextKey, c),
}
if os.Getenv("RCLONE_PASSWORD") != "" {
go rclone.StartFileTransferQueue()
}
registerWorker(c, environment.GetQueue(), workerOptions)
}
func registerWorker(c client.Client, queue string, options worker.Options) {
w := worker.New(c, queue, options)
switch queue {
case environment.QueueDebug:
registerActivitiesInStruct(w, activities.Util)
for _, a := range utilActivities {
w.RegisterActivity(a)
}
registerActivitiesInStruct(w, activities.Vidispine)
registerActivitiesInStruct(w, activities.Platform)
registerActivitiesInStruct(w, activities.Video)
registerActivitiesInStruct(w, activities.Audio)
for _, wf := range workflows.WorkerWorkflows {
w.RegisterWorkflow(wf)
}
case environment.QueueLowPriority:
fallthrough
case environment.QueueWorker:
registerActivitiesInStruct(w, activities.Util)
for _, a := range utilActivities {
w.RegisterActivity(a)
}
registerActivitiesInStruct(w, activities.Platform)
registerActivitiesInStruct(w, activities.Vidispine)
for _, wf := range workflows.WorkerWorkflows {
w.RegisterWorkflow(wf)
}
case environment.QueueTranscode:
registerActivitiesInStruct(w, activities.Video)
case environment.QueueAudio:
registerActivitiesInStruct(w, activities.Audio)
case environment.QueueLiveIngest:
registerActivitiesInStruct(w, activities.Live)
}
fmt.Println("STARTING")
err := w.Run(worker.InterruptCh())
log.Printf("Worker finished: %v", err)
}
func update(version string) error {
if version == "development" {
return nil
}
// Prevent worker from restarting if there are activities executing
wfutils.ActivityWG.Wait()
ctx := context.Background()
latest, found, err := selfupdate.DetectLatest(ctx, selfupdate.ParseSlug("bcc-code/bcc-media-flows"))
if err != nil {
return fmt.Errorf("error occurred while detecting version: %w", err)
}
if !found {
return fmt.Errorf("latest version for %s/%s could not be found from github repository", runtime.GOOS, runtime.GOARCH)
}
if latest.LessOrEqual(version) {
log.Printf("Current version (%s) is the latest", version)
return nil
}
exe, err := os.Executable()
if err != nil {
return fmt.Errorf("could not locate executable path")
}
if err := selfupdate.UpdateTo(ctx, latest.AssetURL, latest.AssetName, exe); err != nil {
return fmt.Errorf("error occurred while updating binary: %w", err)
}
log.Printf("Successfully updated to version %s", latest.Version())
reload.Exec()
return nil
}