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

View File

@ -8,6 +8,7 @@ import (
"net" "net"
"net/http" "net/http"
"os" "os"
"sort"
"time" "time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -40,13 +41,34 @@ func handler(w http.ResponseWriter, r *http.Request) {
globalMutex.RLock() globalMutex.RLock()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
jobEntries := c.Entries() jobEntries := c.Entries()
var jobs []*Job
jobs := make(map[string][]*Job)
for _, jobEntry := range jobEntries { for _, jobEntry := range jobEntries {
job := jobEntry.Job.(*Job) job := jobEntry.Job.(*Job)
job.NextLaunch = jobEntry.Next.Format(config.TimeFormat) 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() globalMutex.RUnlock()
_, _ = buf.WriteTo(w) _, _ = buf.WriteTo(w)

2
job.go
View File

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

View File

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