/
renderer-service.go
160 lines (121 loc) · 3.58 KB
/
renderer-service.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
package renderer
import (
"context"
"errors"
"io"
"time"
"github.com/rs/zerolog/log"
"github.com/lucas-gaitzsch/pdf-turtle/config"
"github.com/lucas-gaitzsch/pdf-turtle/models"
)
type workerSlot struct{}
type workerSlots chan workerSlot
type RendererBackgroundService struct {
localCtx context.Context
localCtxCancel context.CancelFunc
htmlToPdfRenderer HtmlToPdfRendererAbstraction
Jobs chan models.Job
workerSlots workerSlots
renderTimeout time.Duration
workerInstances int
}
func NewRendererBackgroundService(ctx context.Context) *RendererBackgroundService {
rbs := new(RendererBackgroundService)
rbs.workerInstances = config.Get(ctx).WorkerInstances
rbs.renderTimeout = time.Duration(config.Get(ctx).RenderTimeoutInSeconds) * time.Second
rbs.Init(ctx)
return rbs
}
func (rbs *RendererBackgroundService) Init(outerCtx context.Context) {
rbs.workerSlots = make(workerSlots, rbs.workerInstances)
rbs.Jobs = make(chan models.Job)
rbs.localCtx, rbs.localCtxCancel = context.WithCancel(outerCtx)
if rbs.htmlToPdfRenderer == nil {
rbs.htmlToPdfRenderer = NewAsyncHtmlRendererChromium(rbs.localCtx)
}
go rbs.handleRequests(outerCtx)
log.
Info().
Int("workerInstances", rbs.workerInstances).
Msgf("render service started with %d worker", rbs.workerInstances)
}
func (rbs *RendererBackgroundService) acquiredWorker() {
rbs.workerSlots <- workerSlot{}
workerCount := len(rbs.workerSlots)
log.Debug().
Int("workerCount", workerCount).
Msgf("renderer worker up: %d", workerCount)
}
func (rbs *RendererBackgroundService) releaseWorker() {
<-rbs.workerSlots
workerCount := len(rbs.workerSlots)
log.Debug().
Int("workerCount", workerCount).
Msgf("renderer worker down: %d", workerCount)
}
func (rbs *RendererBackgroundService) handleRequests(outerCtx context.Context) {
defer rbs.localCtxCancel()
defer func() {
if r := recover(); r != nil {
log.
Warn().
Interface("recover", r).
Msg("Recovered in renderer-service; wait 500 ms")
time.Sleep(500 * time.Millisecond)
rbs.htmlToPdfRenderer = nil
rbs.Init(outerCtx)
}
}()
for {
select {
case job := <-rbs.Jobs:
go rbs.doWork(rbs.localCtx, job)
case <-rbs.localCtx.Done():
log.Info().Msg("shutting renderer service down")
return
}
}
}
func (rbs *RendererBackgroundService) doWork(ctx context.Context, job models.Job) {
rbs.acquiredWorker()
defer rbs.releaseWorker()
done := make(chan bool, 1)
go func() {
defer func() { done <- true }()
res, err := rbs.htmlToPdfRenderer.RenderHtmlAsPdf(job.RequestCtx, job.RenderData)
if err != nil {
job.CallbackChan <- nil
log.Ctx(job.RequestCtx).Error().Err(err).Msg("render service: cant render pdf")
return
}
job.CallbackChan <- res
}()
select {
case <-done:
case <-time.After(rbs.renderTimeout):
log.Ctx(job.RequestCtx).Warn().Msg("render service: cancel pdf callback (timeout)")
case <-ctx.Done():
log.Ctx(job.RequestCtx).Info().Msg("cancel render task by global context")
case <-job.RequestCtx.Done():
log.Ctx(job.RequestCtx).Info().Msg("cancel render task by request context")
}
}
func (rbs *RendererBackgroundService) RenderAndReceive(job models.Job) (io.Reader, error) {
rbs.Jobs <- job
select {
case pdfBytes := <-job.CallbackChan:
if pdfBytes != nil {
return pdfBytes, nil
} else {
return nil, errors.New("pdf callback empty")
}
case <-time.After(rbs.renderTimeout + 5*time.Second):
return nil, errors.New("pdf callback timeout")
}
}
func (rbs *RendererBackgroundService) Close() {
rbs.htmlToPdfRenderer.Close()
rbs.localCtxCancel()
close(rbs.workerSlots)
close(rbs.Jobs)
}