-
Notifications
You must be signed in to change notification settings - Fork 1
/
radcast.go
158 lines (135 loc) · 2.81 KB
/
radcast.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
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"sync"
"github.com/robfig/cron/v3"
)
type Radcast struct {
reloadChan chan struct{}
saveChan chan *Radiko
configPath string
cron *cron.Cron
m sync.Mutex
wg sync.WaitGroup
ctx context.Context
cancel context.CancelFunc
host string
port string
title string
output string
buffer int64
converter string
}
func NewRadcast(path string, host string, port string, title string, output string, buffer int64, converter string) *Radcast {
ctx, cancel := context.WithCancel(context.Background())
r := &Radcast{
reloadChan: make(chan struct{}),
saveChan: make(chan *Radiko),
configPath: path,
ctx: ctx,
cancel: cancel,
host: host,
port: port,
title: title,
output: output,
buffer: buffer,
converter: converter,
}
return r
}
func (r *Radcast) Run() error {
if err := r.ReloadConfig(); err != nil {
return err
}
if _, err := os.Stat(r.output); err != nil {
if err := os.MkdirAll(r.output, 0777); err != nil {
return err
}
}
go func() {
s := &Server{
Output: r.output,
Title: r.title,
Addr: net.JoinHostPort(r.host, r.port),
}
if err := s.Run(); err != nil {
r.Log(err)
r.Stop()
}
}()
for {
select {
case <-r.ctx.Done():
r.wg.Wait()
return r.ctx.Err()
case <-r.reloadChan:
if err := r.ReloadConfig(); err != nil {
r.Log(err)
}
// if same program is recorded, write files as parallely and may occure error. so write file as serially by channel.
case radiko := <-r.saveChan:
func() {
defer os.RemoveAll(radiko.TempDir)
if err := radiko.Result.Save(r.output); err != nil {
r.Log(err)
}
}()
}
}
}
func (r *Radcast) Stop() {
r.cancel()
}
func (r *Radcast) ReloadConfig() error {
r.m.Lock()
defer r.m.Unlock()
if r.cron != nil {
r.cron.Stop()
r.Log("stop previous cron")
}
config, err := LoadConfig(*configPath)
if err != nil {
return err
}
c := cron.New()
for station, specs := range config {
for _, spec := range specs {
func(station string, spec string) {
r.Log("station:", station, " spec:", spec)
c.AddFunc(spec, func() {
r.wg.Add(1)
defer r.wg.Done()
dir, err := ioutil.TempDir("", "radiko")
if err != nil {
r.Log(err)
return
}
radiko := &Radiko{
Station: station,
Buffer: r.buffer,
Converter: r.converter,
TempDir: dir,
}
if err := radiko.Run(r.ctx); err != nil {
os.RemoveAll(radiko.TempDir)
r.Log(err)
return
}
r.saveChan <- radiko
})
}(station, spec)
}
}
c.Start()
r.cron = c
r.Log("start new cron")
return nil
}
func (r *Radcast) Log(v ...interface{}) {
log.Println("[Radcast]", fmt.Sprint(v...))
}