refactored schedules behind filter.ID, also have enabled/disabled option now
This commit is contained in:
parent
29ef3c7f15
commit
69e1f8dd34
5 changed files with 107 additions and 35 deletions
65
main.go
65
main.go
|
@ -100,7 +100,7 @@ func (web *Web) initCronJobs() {
|
|||
continue
|
||||
}
|
||||
log.Println("Started CronJob for WatchID", cronFilter.WatchID, "with schedule:", cronFilter.Var1)
|
||||
web.cronWatch[cronFilter.WatchID] = web.cron.Entry(entryID)
|
||||
web.cronWatch[cronFilter.ID] = web.cron.Entry(entryID)
|
||||
}
|
||||
web.cron.Start()
|
||||
}
|
||||
|
@ -120,6 +120,21 @@ func (web *Web) index(c *gin.Context) {
|
|||
watches := []Watch{}
|
||||
web.db.Find(&watches)
|
||||
|
||||
watchMap := make(map[uint]*Watch, len(watches))
|
||||
for i := 0; i < len(watches); i++ {
|
||||
watchMap[watches[i].ID] = &watches[i]
|
||||
}
|
||||
// this doesn't work with multiple schedule filters per watch, but meh
|
||||
var filters []Filter
|
||||
web.db.Model(&Filter{}).Find(&filters, "type = 'cron'")
|
||||
for _, filter := range filters {
|
||||
entry, exists := web.cronWatch[filter.ID]
|
||||
if !exists {
|
||||
log.Println("No cron entry for filter", filter.ID, filter.Name)
|
||||
continue
|
||||
}
|
||||
watchMap[filter.WatchID].CronEntry = &entry
|
||||
}
|
||||
for i := 0; i < len(watches); i++ {
|
||||
entry := web.cronWatch[watches[i].ID]
|
||||
watches[i].CronEntry = &entry
|
||||
|
@ -153,6 +168,16 @@ func (web *Web) deleteWatch(c *gin.Context) {
|
|||
|
||||
web.db.Delete(&FilterConnection{}, "watch_id = ?", id)
|
||||
web.db.Delete(&FilterOutput{}, "watch_id = ?", id)
|
||||
|
||||
var cronFilters []Filter
|
||||
web.db.Model(&Filter{}).Find(&cronFilters, "watch_id = ? AND type = 'cron'", id)
|
||||
for _, filter := range cronFilters {
|
||||
entry, exist := web.cronWatch[filter.ID]
|
||||
if exist {
|
||||
web.cron.Remove(entry.ID)
|
||||
delete(web.cronWatch, filter.ID)
|
||||
}
|
||||
}
|
||||
web.db.Delete(&Filter{}, "watch_id = ?", id)
|
||||
|
||||
web.db.Delete(&Watch{}, id)
|
||||
|
@ -164,13 +189,17 @@ func (web *Web) watchView(c *gin.Context) {
|
|||
|
||||
var watch Watch
|
||||
web.db.Model(&Watch{}).First(&watch, id)
|
||||
entry, exists := web.cronWatch[watch.ID]
|
||||
if !exists {
|
||||
log.Println("Could not find entry for Watch", watch.ID)
|
||||
c.HTML(http.StatusNotFound, "watchView", gin.H{"error": "Entry not found"})
|
||||
return
|
||||
|
||||
var cronFilters []Filter
|
||||
web.db.Model(&Filter{}).Find(&cronFilters, "watch_id = ? AND type = 'cron'", id)
|
||||
for _, filter := range cronFilters {
|
||||
entry, exists := web.cronWatch[filter.ID]
|
||||
if !exists {
|
||||
log.Println("Could not find entry for filter", filter.ID, filter.Name)
|
||||
continue
|
||||
}
|
||||
watch.CronEntry = &entry
|
||||
}
|
||||
watch.CronEntry = &entry
|
||||
|
||||
var values []FilterOutput
|
||||
web.db.Model(&FilterOutput{}).Order("time asc").Where("watch_id = ?", watch.ID).Find(&values)
|
||||
|
@ -244,13 +273,31 @@ func (web *Web) watchUpdate(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
var oldFilters []Filter
|
||||
web.db.Model(&Filter{}).Where("watch_id = ?", watch.ID).Find(&oldFilters)
|
||||
// stop/delete cronjobs running for this watch
|
||||
var cronFilters []Filter
|
||||
web.db.Model(&Filter{}).Where("watch_id = ? AND type = 'cron'", watch.ID).Find(&cronFilters)
|
||||
for _, filter := range cronFilters {
|
||||
entry, exist := web.cronWatch[filter.ID]
|
||||
if exist {
|
||||
web.cron.Remove(entry.ID)
|
||||
delete(web.cronWatch, filter.ID)
|
||||
}
|
||||
}
|
||||
|
||||
filterMap := make(map[uint]*Filter)
|
||||
for i := range newFilters {
|
||||
filter := &newFilters[i]
|
||||
filterMap[filter.ID] = filter
|
||||
filter.ID = 0
|
||||
if filter.Type == "cron" {
|
||||
entryID, err := web.cron.AddFunc(filter.Var1, func() { triggerSchedule(filter.WatchID, web) })
|
||||
if err != nil {
|
||||
log.Println("Could not start job for Watch: ", filter.WatchID)
|
||||
continue
|
||||
}
|
||||
log.Println("Started CronJob for WatchID", filter.WatchID, "with schedule:", filter.Var1)
|
||||
web.cronWatch[filter.ID] = web.cron.Entry(entryID)
|
||||
}
|
||||
}
|
||||
web.db.Delete(&Filter{}, "watch_id = ?", watch.ID)
|
||||
|
||||
|
|
|
@ -498,14 +498,26 @@ function onTypeChange(node) {
|
|||
var1Label.innerHTML = "CRON";
|
||||
var1Input.placeholder = "30 3-6,20-23 * * *";
|
||||
var1Div.appendChild(var1Input);
|
||||
var var2Input = document.createElement("input");
|
||||
var2Input.name = "var2";
|
||||
var2Input.id = "var2Input";
|
||||
var2Input.value = var2Value;
|
||||
var2Input.classList.add("form-control");
|
||||
var2Input.disabled = true;
|
||||
var2Label.innerHTML = "-";
|
||||
var2Div.appendChild(var2Input);
|
||||
var enabledSelect = document.createElement("select");
|
||||
enabledSelect.name = "var2";
|
||||
enabledSelect.id = "var2Input";
|
||||
enabledSelect.classList.add("form-control");
|
||||
var enabledOption = document.createElement("option");
|
||||
enabledOption.value = "yes";
|
||||
enabledOption.innerHTML = "Enabled";
|
||||
enabledSelect.appendChild(enabledOption);
|
||||
var disabledOption = document.createElement("option");
|
||||
disabledOption.value = "no";
|
||||
disabledOption.innerHTML = "Disabled";
|
||||
enabledSelect.appendChild(disabledOption);
|
||||
if (var2Value == "") {
|
||||
enabledSelect.value = "yes";
|
||||
}
|
||||
else {
|
||||
enabledSelect.value = var2Value;
|
||||
}
|
||||
var2Div.appendChild(enabledSelect);
|
||||
var2Label.innerHTML = "Enabled";
|
||||
var var3Input = document.createElement("input");
|
||||
var3Input.name = "var3";
|
||||
var3Input.id = "var3Input";
|
||||
|
|
|
@ -495,14 +495,25 @@ function onTypeChange(node: DiagramNode | null = null){
|
|||
var1Input.placeholder = "30 3-6,20-23 * * *";
|
||||
var1Div.appendChild(var1Input);
|
||||
|
||||
let var2Input = document.createElement("input");
|
||||
var2Input.name = "var2";
|
||||
var2Input.id = "var2Input";
|
||||
var2Input.value = var2Value;
|
||||
var2Input.classList.add("form-control")
|
||||
var2Input.disabled = true;
|
||||
var2Label.innerHTML = "-";
|
||||
var2Div.appendChild(var2Input);
|
||||
let enabledSelect = document.createElement("select");
|
||||
enabledSelect.name = "var2";
|
||||
enabledSelect.id = "var2Input";
|
||||
enabledSelect.classList.add("form-control");
|
||||
let enabledOption = document.createElement("option");
|
||||
enabledOption.value = "yes"
|
||||
enabledOption.innerHTML = "Enabled";
|
||||
enabledSelect.appendChild(enabledOption);
|
||||
let disabledOption = document.createElement("option");
|
||||
disabledOption.value = "no"
|
||||
disabledOption.innerHTML = "Disabled";
|
||||
enabledSelect.appendChild(disabledOption);
|
||||
if (var2Value == ""){
|
||||
enabledSelect.value = "yes";
|
||||
} else {
|
||||
enabledSelect.value = var2Value;
|
||||
}
|
||||
var2Div.appendChild(enabledSelect);
|
||||
var2Label.innerHTML = "Enabled"
|
||||
|
||||
let var3Input = document.createElement("input");
|
||||
var3Input.name = "var3";
|
||||
|
|
|
@ -8,14 +8,18 @@
|
|||
<div class="card d-flex justify-content-around">
|
||||
<div class="card-body">
|
||||
<h4 class="card-title text-center">{{ .Watch.Name }}</h4>
|
||||
<div class="row">
|
||||
<div class="col-4">Previous</div>
|
||||
<div class="col-8">{{ .Watch.CronEntry.Prev.Format "2006-01-02 15:04:05" }}</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-4">Next</div>
|
||||
<div class="col-8">{{ .Watch.CronEntry.Next.Format "2006-01-02 15:04:05" }}</div>
|
||||
</div>
|
||||
{{ if not .Watch.CronEntry }}
|
||||
<h5>No Schedule</h5>
|
||||
{{ else }}
|
||||
<div class="row">
|
||||
<div class="col-4">Previous</div>
|
||||
<div class="col-8">{{ .Watch.CronEntry.Prev.Format "2006-01-02 15:04:05" }}</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-4">Next</div>
|
||||
<div class="col-8">{{ .Watch.CronEntry.Next.Format "2006-01-02 15:04:05" }}</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
2
todo.md
2
todo.md
|
@ -1,5 +1,3 @@
|
|||
# Todo
|
||||
- add last/next run of watch to front page
|
||||
- ability to pause watches?
|
||||
- refactor getFilterResult so it's just part of web ?
|
||||
- add timer to run watch ?
|
||||
|
|
Loading…
Add table
Reference in a new issue