1
0
mirror of https://github.com/nxshock/gron.git synced 2024-11-27 03:41:00 +05:00

Add job categories

This commit is contained in:
nxshock 2022-10-21 20:12:41 +05:00
parent 96587792cc
commit 6dd6112040
4 changed files with 32 additions and 6 deletions

View File

@ -8,6 +8,7 @@
2. Create job config in `gron.d/job1.conf` ([TOML](https://en.wikipedia.org/wiki/TOML) format):
```toml
Type = "cmd" # command execution
Category = "Test jobs" # jobs category name
Cron = "* * * * *" # cron instructions
Command = "echo Hello" # command to execute

View File

@ -8,6 +8,7 @@ import (
"net"
"net/http"
"os"
"sort"
"time"
log "github.com/sirupsen/logrus"
@ -40,13 +41,34 @@ func handler(w http.ResponseWriter, r *http.Request) {
globalMutex.RLock()
buf := new(bytes.Buffer)
jobEntries := c.Entries()
var jobs []*Job
jobs := make(map[string][]*Job)
for _, jobEntry := range jobEntries {
job := jobEntry.Job.(*Job)
job.NextLaunch = jobEntry.Next.Format(config.TimeFormat)
jobs = append(jobs, job)
jobs[job.JobConfig.Category] = append(jobs[job.JobConfig.Category], job)
}
_ = templates.ExecuteTemplate(buf, "index.htm", jobs)
var keys []string
for k, v := range jobs {
keys = append(keys, k)
sort.Slice(v, func(i, j int) bool {
return v[i].Name < v[j].Name
})
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
_ = templates.ExecuteTemplate(buf, "index.htm", struct {
Categories []string
Jobs map[string][]*Job
}{
Categories: keys,
Jobs: jobs,
})
globalMutex.RUnlock()
_, _ = buf.WriteTo(w)

2
job.go
View File

@ -21,6 +21,8 @@ import (
type JobConfig struct {
Type JobType
Category string
// JobType = Cmd
Command string // command for execution

View File

@ -20,7 +20,8 @@
<a href="/shutdown">&#x23fb; Shutdown</a>
</div>
</div>
<h1>Job list</h1>
{{range .Categories}}
<h2>{{if eq . ""}}Other jobs{{else}}{{.}}{{end}}</h2>
<table>
<tr>
<th>Name</th>
@ -33,7 +34,7 @@
<th>Next launch</th>
<th>Details</th>
</tr>
{{range .}}
{{range (index $.Jobs .)}}
<tr>
<td class="no-padding">
<form action="/start" method="get" id="form-{{.Name}}"></form>
@ -50,7 +51,7 @@
<td>{{.NextLaunch}}</td>
<td class="centered"><a href="/details?jobName={{.Name}}">open</a></td>
</tr>{{end}}
</table>
</table>{{end}}
</main>
</body>