Skip to content

Commit

Permalink
Add framework for scheduled jobs; convert auto-refresh
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
clcollins committed Oct 1, 2024
1 parent 0e7eccd commit 35aea3e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
8 changes: 3 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"github.com/spf13/viper"
)

const pollInterval = 15
const tickInterval = 1

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

go func() {
for {
time.Sleep(pollInterval * time.Second)
p.Send(tui.PollIncidentsMsg{
PollInterval: pollInterval * time.Second,
})
time.Sleep(tickInterval * time.Second)
p.Send(tui.TickMsg{})
}
}()

Expand Down
18 changes: 14 additions & 4 deletions pkg/tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,10 @@ type waitForSelectedIncidentThenDoMsg struct {
}

type TickMsg struct {
Tick int
}
type PollIncidentsMsg struct {
PollInterval time.Duration
}

type PollIncidentsMsg struct{}

type renderIncidentMsg string

type renderedIncidentMsg struct {
Expand Down Expand Up @@ -640,3 +638,15 @@ func doIfIncidentSelected(m *model, cmd tea.Cmd) tea.Cmd {
cmd,
)
}

func runScheduledJobs(m *model) []tea.Cmd {
var cmds []tea.Cmd
for _, job := range m.scheduledJobs {
if time.Since(job.lastRun) > job.frequency && job.jobMsg != nil {
log.Debug("Update: TicketMsg", "scheduledJob", job.jobMsg, "frequency", job.frequency, "lastRun", job.lastRun, "running", true)
cmds = append(cmds, job.jobMsg)
job.lastRun = time.Now()
}
}
return cmds
}
12 changes: 12 additions & 0 deletions pkg/tui/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tui

import (
"time"

"github.com/PagerDuty/go-pagerduty"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/table"
Expand All @@ -12,6 +14,13 @@ import (
"github.com/clcollins/srepd/pkg/pd"
)

var initialScheduledJobs = []*scheduledJob{
{
jobMsg: func() tea.Msg { return PollIncidentsMsg{} },
frequency: time.Second * 15,
},
}

type model struct {
err error

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

scheduledJobs []*scheduledJob

autoAcknowledge bool
autoRefresh bool
teamMode bool
Expand Down Expand Up @@ -60,6 +71,7 @@ func InitialModel(
input: newTextInput(),
incidentViewer: newIncidentViewer(),
status: "",
scheduledJobs: append([]*scheduledJob{}, initialScheduledJobs...),
}

// This is an ugly way to handle this error
Expand Down
12 changes: 9 additions & 3 deletions pkg/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ type filteredMsg struct {
truncated bool
}

type scheduledJob struct {
jobMsg tea.Cmd
lastRun time.Time
frequency time.Duration
}

func filterMsgContent(msg tea.Msg) tea.Msg {
var truncatedMsg string
switch msg := msg.(type) {
Expand Down Expand Up @@ -94,8 +100,8 @@ func filterMsgContent(msg tea.Msg) tea.Msg {
// return m, func() tea.Msg { getIncident(m.config, msg.incident.ID) }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
msgType := reflect.TypeOf(msg)
// PollIncidentsMsgs are not helpful for logging
if msgType != reflect.TypeOf(PollIncidentsMsg{}) {
// TickMsg are not helpful for logging
if msgType != reflect.TypeOf(TickMsg{}) {
log.Debug("Update", msgType, filterMsgContent(msg))
}

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

case TickMsg:
// Pass
return m, tea.Batch(runScheduledJobs(&m)...)

case tea.WindowSizeMsg:
return m.windowSizeMsgHandler(msg)
Expand Down

0 comments on commit 35aea3e

Please sign in to comment.