Skip to content

Commit 35aea3e

Browse files
committed
Add framework for scheduled jobs; convert auto-refresh
This PR adds a framework for "scheduled jobs" and passes "TickMsg" messages to the Update() function based on an interval set in the root.go file (currently 1s per "tick"). This also converts the auto-refresh to a scheduled job, and sets a slice of initial `[]*scheduledJob`. Each scheduled job is made up of: ``` type scheduledJob struct { jobMsg tea.Cmd lastRun time.Time frequency time.Duration } ``` And the `jobMsg` `tea.Cmd` is run if the `lastRun` is further than `frequency` in the past. Each scheduled job is evalusted when a `TickMsg` is received. The auto refresh feature is implemented as: ``` { jobMsg: func() tea.Msg { return PollIncidentsMsg{} }, frequency: time.Second * 15, }, ``` This job is added to the list of initial secheduled jobs on startup. Currently, the job is run every 15 seconds, and if "auto-refresh" is enabled, then incidents are updated. In the future this will be converted to adding or removing the scheduled job from the job list, rather than parsing if "auto-refresh" is true or not, and `autoRefresh bool` removed from the model. Signed-off-by: Chris Collins <collins.christopher@gmail.com>
1 parent 0e7eccd commit 35aea3e

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

cmd/root.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
"github.com/spf13/viper"
4040
)
4141

42-
const pollInterval = 15
42+
const tickInterval = 1
4343

4444
// rootCmd represents the base command when called without any subcommands
4545
var rootCmd = &cobra.Command{
@@ -86,10 +86,8 @@ but rather a simple tool to make on-call tasks easier.`,
8686

8787
go func() {
8888
for {
89-
time.Sleep(pollInterval * time.Second)
90-
p.Send(tui.PollIncidentsMsg{
91-
PollInterval: pollInterval * time.Second,
92-
})
89+
time.Sleep(tickInterval * time.Second)
90+
p.Send(tui.TickMsg{})
9391
}
9492
}()
9593

pkg/tui/commands.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,10 @@ type waitForSelectedIncidentThenDoMsg struct {
174174
}
175175

176176
type TickMsg struct {
177-
Tick int
178-
}
179-
type PollIncidentsMsg struct {
180-
PollInterval time.Duration
181177
}
182178

179+
type PollIncidentsMsg struct{}
180+
183181
type renderIncidentMsg string
184182

185183
type renderedIncidentMsg struct {
@@ -640,3 +638,15 @@ func doIfIncidentSelected(m *model, cmd tea.Cmd) tea.Cmd {
640638
cmd,
641639
)
642640
}
641+
642+
func runScheduledJobs(m *model) []tea.Cmd {
643+
var cmds []tea.Cmd
644+
for _, job := range m.scheduledJobs {
645+
if time.Since(job.lastRun) > job.frequency && job.jobMsg != nil {
646+
log.Debug("Update: TicketMsg", "scheduledJob", job.jobMsg, "frequency", job.frequency, "lastRun", job.lastRun, "running", true)
647+
cmds = append(cmds, job.jobMsg)
648+
job.lastRun = time.Now()
649+
}
650+
}
651+
return cmds
652+
}

pkg/tui/model.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package tui
22

33
import (
4+
"time"
5+
46
"github.com/PagerDuty/go-pagerduty"
57
"github.com/charmbracelet/bubbles/help"
68
"github.com/charmbracelet/bubbles/table"
@@ -12,6 +14,13 @@ import (
1214
"github.com/clcollins/srepd/pkg/pd"
1315
)
1416

17+
var initialScheduledJobs = []*scheduledJob{
18+
{
19+
jobMsg: func() tea.Msg { return PollIncidentsMsg{} },
20+
frequency: time.Second * 15,
21+
},
22+
}
23+
1524
type model struct {
1625
err error
1726

@@ -33,6 +42,8 @@ type model struct {
3342
selectedIncidentNotes []pagerduty.IncidentNote
3443
selectedIncidentAlerts []pagerduty.IncidentAlert
3544

45+
scheduledJobs []*scheduledJob
46+
3647
autoAcknowledge bool
3748
autoRefresh bool
3849
teamMode bool
@@ -60,6 +71,7 @@ func InitialModel(
6071
input: newTextInput(),
6172
incidentViewer: newIncidentViewer(),
6273
status: "",
74+
scheduledJobs: append([]*scheduledJob{}, initialScheduledJobs...),
6375
}
6476

6577
// This is an ugly way to handle this error

pkg/tui/tui.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ type filteredMsg struct {
4545
truncated bool
4646
}
4747

48+
type scheduledJob struct {
49+
jobMsg tea.Cmd
50+
lastRun time.Time
51+
frequency time.Duration
52+
}
53+
4854
func filterMsgContent(msg tea.Msg) tea.Msg {
4955
var truncatedMsg string
5056
switch msg := msg.(type) {
@@ -94,8 +100,8 @@ func filterMsgContent(msg tea.Msg) tea.Msg {
94100
// return m, func() tea.Msg { getIncident(m.config, msg.incident.ID) }
95101
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
96102
msgType := reflect.TypeOf(msg)
97-
// PollIncidentsMsgs are not helpful for logging
98-
if msgType != reflect.TypeOf(PollIncidentsMsg{}) {
103+
// TickMsg are not helpful for logging
104+
if msgType != reflect.TypeOf(TickMsg{}) {
99105
log.Debug("Update", msgType, filterMsgContent(msg))
100106
}
101107

@@ -107,7 +113,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
107113
return m.errMsgHandler(msg)
108114

109115
case TickMsg:
110-
// Pass
116+
return m, tea.Batch(runScheduledJobs(&m)...)
111117

112118
case tea.WindowSizeMsg:
113119
return m.windowSizeMsgHandler(msg)

0 commit comments

Comments
 (0)