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
61
main.go
61
main.go
|
@ -100,7 +100,7 @@ func (web *Web) initCronJobs() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Println("Started CronJob for WatchID", cronFilter.WatchID, "with schedule:", cronFilter.Var1)
|
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()
|
web.cron.Start()
|
||||||
}
|
}
|
||||||
|
@ -120,6 +120,21 @@ func (web *Web) index(c *gin.Context) {
|
||||||
watches := []Watch{}
|
watches := []Watch{}
|
||||||
web.db.Find(&watches)
|
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++ {
|
for i := 0; i < len(watches); i++ {
|
||||||
entry := web.cronWatch[watches[i].ID]
|
entry := web.cronWatch[watches[i].ID]
|
||||||
watches[i].CronEntry = &entry
|
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(&FilterConnection{}, "watch_id = ?", id)
|
||||||
web.db.Delete(&FilterOutput{}, "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(&Filter{}, "watch_id = ?", id)
|
||||||
|
|
||||||
web.db.Delete(&Watch{}, id)
|
web.db.Delete(&Watch{}, id)
|
||||||
|
@ -164,13 +189,17 @@ func (web *Web) watchView(c *gin.Context) {
|
||||||
|
|
||||||
var watch Watch
|
var watch Watch
|
||||||
web.db.Model(&Watch{}).First(&watch, id)
|
web.db.Model(&Watch{}).First(&watch, id)
|
||||||
entry, exists := web.cronWatch[watch.ID]
|
|
||||||
|
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 {
|
if !exists {
|
||||||
log.Println("Could not find entry for Watch", watch.ID)
|
log.Println("Could not find entry for filter", filter.ID, filter.Name)
|
||||||
c.HTML(http.StatusNotFound, "watchView", gin.H{"error": "Entry not found"})
|
continue
|
||||||
return
|
|
||||||
}
|
}
|
||||||
watch.CronEntry = &entry
|
watch.CronEntry = &entry
|
||||||
|
}
|
||||||
|
|
||||||
var values []FilterOutput
|
var values []FilterOutput
|
||||||
web.db.Model(&FilterOutput{}).Order("time asc").Where("watch_id = ?", watch.ID).Find(&values)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var oldFilters []Filter
|
// stop/delete cronjobs running for this watch
|
||||||
web.db.Model(&Filter{}).Where("watch_id = ?", watch.ID).Find(&oldFilters)
|
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)
|
filterMap := make(map[uint]*Filter)
|
||||||
for i := range newFilters {
|
for i := range newFilters {
|
||||||
filter := &newFilters[i]
|
filter := &newFilters[i]
|
||||||
filterMap[filter.ID] = filter
|
filterMap[filter.ID] = filter
|
||||||
filter.ID = 0
|
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)
|
web.db.Delete(&Filter{}, "watch_id = ?", watch.ID)
|
||||||
|
|
||||||
|
|
|
@ -498,14 +498,26 @@ function onTypeChange(node) {
|
||||||
var1Label.innerHTML = "CRON";
|
var1Label.innerHTML = "CRON";
|
||||||
var1Input.placeholder = "30 3-6,20-23 * * *";
|
var1Input.placeholder = "30 3-6,20-23 * * *";
|
||||||
var1Div.appendChild(var1Input);
|
var1Div.appendChild(var1Input);
|
||||||
var var2Input = document.createElement("input");
|
var enabledSelect = document.createElement("select");
|
||||||
var2Input.name = "var2";
|
enabledSelect.name = "var2";
|
||||||
var2Input.id = "var2Input";
|
enabledSelect.id = "var2Input";
|
||||||
var2Input.value = var2Value;
|
enabledSelect.classList.add("form-control");
|
||||||
var2Input.classList.add("form-control");
|
var enabledOption = document.createElement("option");
|
||||||
var2Input.disabled = true;
|
enabledOption.value = "yes";
|
||||||
var2Label.innerHTML = "-";
|
enabledOption.innerHTML = "Enabled";
|
||||||
var2Div.appendChild(var2Input);
|
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");
|
var var3Input = document.createElement("input");
|
||||||
var3Input.name = "var3";
|
var3Input.name = "var3";
|
||||||
var3Input.id = "var3Input";
|
var3Input.id = "var3Input";
|
||||||
|
|
|
@ -495,14 +495,25 @@ function onTypeChange(node: DiagramNode | null = null){
|
||||||
var1Input.placeholder = "30 3-6,20-23 * * *";
|
var1Input.placeholder = "30 3-6,20-23 * * *";
|
||||||
var1Div.appendChild(var1Input);
|
var1Div.appendChild(var1Input);
|
||||||
|
|
||||||
let var2Input = document.createElement("input");
|
let enabledSelect = document.createElement("select");
|
||||||
var2Input.name = "var2";
|
enabledSelect.name = "var2";
|
||||||
var2Input.id = "var2Input";
|
enabledSelect.id = "var2Input";
|
||||||
var2Input.value = var2Value;
|
enabledSelect.classList.add("form-control");
|
||||||
var2Input.classList.add("form-control")
|
let enabledOption = document.createElement("option");
|
||||||
var2Input.disabled = true;
|
enabledOption.value = "yes"
|
||||||
var2Label.innerHTML = "-";
|
enabledOption.innerHTML = "Enabled";
|
||||||
var2Div.appendChild(var2Input);
|
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");
|
let var3Input = document.createElement("input");
|
||||||
var3Input.name = "var3";
|
var3Input.name = "var3";
|
||||||
|
|
|
@ -8,6 +8,9 @@
|
||||||
<div class="card d-flex justify-content-around">
|
<div class="card d-flex justify-content-around">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h4 class="card-title text-center">{{ .Watch.Name }}</h4>
|
<h4 class="card-title text-center">{{ .Watch.Name }}</h4>
|
||||||
|
{{ if not .Watch.CronEntry }}
|
||||||
|
<h5>No Schedule</h5>
|
||||||
|
{{ else }}
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-4">Previous</div>
|
<div class="col-4">Previous</div>
|
||||||
<div class="col-8">{{ .Watch.CronEntry.Prev.Format "2006-01-02 15:04:05" }}</div>
|
<div class="col-8">{{ .Watch.CronEntry.Prev.Format "2006-01-02 15:04:05" }}</div>
|
||||||
|
@ -16,6 +19,7 @@
|
||||||
<div class="col-4">Next</div>
|
<div class="col-4">Next</div>
|
||||||
<div class="col-8">{{ .Watch.CronEntry.Next.Format "2006-01-02 15:04:05" }}</div>
|
<div class="col-8">{{ .Watch.CronEntry.Next.Format "2006-01-02 15:04:05" }}</div>
|
||||||
</div>
|
</div>
|
||||||
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
2
todo.md
2
todo.md
|
@ -1,5 +1,3 @@
|
||||||
# Todo
|
# Todo
|
||||||
- add last/next run of watch to front page
|
|
||||||
- ability to pause watches?
|
|
||||||
- refactor getFilterResult so it's just part of web ?
|
- refactor getFilterResult so it's just part of web ?
|
||||||
- add timer to run watch ?
|
- add timer to run watch ?
|
||||||
|
|
Loading…
Add table
Reference in a new issue