-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
manager.go
104 lines (89 loc) · 2.28 KB
/
manager.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
package core
import (
"fmt"
"os"
"os/exec"
"sync"
"time"
"github.com/sirupsen/logrus"
)
// IManager is the interface for the manager object that handles the start
// of the transcoding process
type IManager interface {
Start(cmd *exec.Cmd, physicalPath string) chan bool
WaitForStream(path string) chan bool
}
// Manager is describes a new object that has the start function
type Manager struct {
timeout time.Duration
}
// Type check
var _ IManager = (*Manager)(nil)
// NewManager returns a new instance of a manager
func NewManager(timeout time.Duration) *Manager {
return &Manager{timeout}
}
// WaitForStream is for waiting for the index file of a given stream
// after it has been restarted
func (m Manager) WaitForStream(path string) chan bool {
var once sync.Once
streamResolved := make(chan bool, 1)
// Start scanning for the given file
go func() {
for {
_, err := os.Open(path)
if err != nil {
<-time.After(25 * time.Millisecond)
continue
}
once.Do(func() { streamResolved <- true })
return
}
}()
// Start the timeout phase for the restarted stream
go func() {
<-time.After(m.timeout)
once.Do(func() {
logrus.Error(fmt.Errorf("%s timed out while waiting for file creation in manager start", path))
streamResolved <- false
})
}()
return streamResolved
}
// Start is to manage the start of the transcoding
func (m Manager) Start(cmd *exec.Cmd, physicalPath string) chan bool {
// Init synchronization components
var once sync.Once
streamResolved := make(chan bool, 1)
// Try scanning for the file, resolve if we found index.m3u8
go func() {
for {
_, err := os.Stat(physicalPath)
if err != nil {
<-time.After(25 * time.Millisecond)
continue
}
once.Do(func() { streamResolved <- true })
return
}
}()
// Run the transcoding, resolve stream if it errors out
go func() {
if err := cmd.Run(); err != nil {
once.Do(func() {
logrus.Error(err)
streamResolved <- false
})
}
}()
// After a certain time if nothing happens, just error it out
go func() {
<-time.After(m.timeout)
once.Do(func() {
logrus.Error(fmt.Errorf("%s timed out while waiting for file creation in manager start", physicalPath))
streamResolved <- false
})
}()
// Return channel for synchronization
return streamResolved
}