-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsched.go
More file actions
139 lines (129 loc) · 4.41 KB
/
Copy pathsched.go
File metadata and controls
139 lines (129 loc) · 4.41 KB
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
// Copyright 2026 HAProxy Technologies LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"log"
"time"
"github.com/haproxytech/gopherd/internal/cron"
"github.com/haproxytech/gopherd/service"
)
// schedRunner drives one startup=scheduled service: it sleeps until the next
// cron tick and fires start(), skipping the tick when the previous run is
// still going (isRunning). It deliberately knows nothing about services or
// the daemon — the callbacks keep it testable in a synctest bubble.
type schedRunner struct {
stopCh chan struct{}
doneCh chan struct{}
start func()
isRunning func() bool
sched *cron.Schedule
name string
}
func newSchedRunner(name string, sched *cron.Schedule, start func(), isRunning func() bool) *schedRunner {
return &schedRunner{
name: name,
sched: sched,
start: start,
isRunning: isRunning,
stopCh: make(chan struct{}),
doneCh: make(chan struct{}),
}
}
// loop runs until stop() or until the schedule has no future match. Run it in
// its own goroutine.
func (r *schedRunner) loop() {
defer close(r.doneCh)
for {
now := time.Now()
next := r.sched.Next(now)
if next.IsZero() {
log.Printf("scheduled %s: no future run matches schedule; scheduling stopped", r.name)
return
}
timer := time.NewTimer(next.Sub(now))
select {
case <-timer.C:
if r.isRunning() {
log.Printf("scheduled %s: skipping run: previous run still in progress", r.name)
continue
}
r.start()
case <-r.stopCh:
timer.Stop()
return
}
}
}
// stop signals the loop to exit without joining it: stop is called under
// d.mu (reload) while the loop may be blocked on d.mu inside start(), so
// joining could deadlock — the same rationale as check.Stop. A tick that
// slipped through is harmless: startService re-validates shutdown/replacement
// under d.mu. Called once per runner; reload replaces runners, never reuses.
func (r *schedRunner) stop() {
close(r.stopCh)
}
// startSchedulers creates and starts one schedRunner per scheduled service.
// Called without d.mu during single-threaded startup and under d.mu from
// reload; appending to d.schedulers is safe in both.
func (d *daemon) startSchedulers() {
for _, svc := range d.services {
if !svc.Scheduled {
continue
}
r := newSchedRunner(svc.Name, svc.Schedule, func() { d.startScheduledRun(svc) }, svc.IsRunning)
d.schedulers = append(d.schedulers, r)
go r.loop()
if next := svc.Schedule.Next(time.Now()); !next.IsZero() {
log.Printf("scheduled %s: next run at %s", svc.Name, next.Format(time.DateTime))
}
}
}
// stopSchedulers signals every runner to exit. Non-joining (see
// schedRunner.stop), so it is safe under d.mu.
func (d *daemon) stopSchedulers() {
for _, r := range d.schedulers {
r.stop()
}
d.schedulers = nil
}
// startScheduledRun fires one scheduled run and, when startup-timeout is set,
// arms a stop for a run that overstays it. The pid comparison keeps the
// timeout from killing a later run of the same service.
func (d *daemon) startScheduledRun(svc *service.Service) {
pid, err := d.startService(svc)
if err != nil {
// Benign races (shutdown began, reload replaced the service, manual
// start won the tick) and condition skips are not failures worth
// logging as errors.
if err != errShuttingDown && err != errServiceReplaced && err != errAlreadyRunning && err != errConditionUnmet {
log.Printf("scheduled %s: start failed: %v", svc.Name, err)
}
return
}
if svc.Proc.StartupTimeout == "" {
return
}
// Pre-validated at config load; a parse failure here means a code bug,
// and running unbounded is safer than crashing PID 1.
dur, err := time.ParseDuration(svc.Proc.StartupTimeout)
if err != nil || dur <= 0 {
return
}
time.AfterFunc(dur, func() {
if svc.IsRunning() && svc.Pid.Load() == int64(pid) {
log.Printf("scheduled %s: run exceeded startup-timeout %s; stopping", svc.Name, svc.Proc.StartupTimeout)
svc.Stop()
}
})
}