From 39b2aa2e10ce5e16016e80ce1bd06bbdd05e9014 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Mon, 19 Dec 2022 17:18:12 +0000 Subject: [PATCH] only things connected to the schedule are processed (unless editing) --- main.go | 8 ++++---- scraping.go | 22 +++++++++++++++++----- todo.md | 1 - 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index ba6e0df..dcf75c8 100644 --- a/main.go +++ b/main.go @@ -135,7 +135,7 @@ func (web *Web) initCronJobs() { web.cronWatch = make(map[uint]cron.Entry, len(cronFilters)) web.cron = cron.New() for _, cronFilter := range cronFilters { - entryID, err := web.cron.AddFunc(cronFilter.Var1, func() { triggerSchedule(cronFilter.WatchID, web) }) + entryID, err := web.cron.AddFunc(cronFilter.Var1, func() { triggerSchedule(cronFilter.WatchID, web, &cronFilter.ID) }) if err != nil { log.Println("Could not start job for Watch: ", cronFilter.WatchID) continue @@ -305,7 +305,7 @@ func (web *Web) watchEdit(c *gin.Context) { } buildFilterTree(filters, connections) - processFilters(filters, web, &watch, true) + processFilters(filters, web, &watch, true, nil) c.HTML(http.StatusOK, "watchEdit", gin.H{ "Watch": watch, @@ -367,7 +367,7 @@ func (web *Web) watchUpdate(c *gin.Context) { if filter.Type != "cron" { continue } - entryID, err := web.cron.AddFunc(filter.Var1, func() { triggerSchedule(filter.WatchID, web) }) + entryID, err := web.cron.AddFunc(filter.Var1, func() { triggerSchedule(filter.WatchID, web, &filter.ID) }) if err != nil { log.Println("Could not start job for Watch: ", filter.WatchID, err) continue @@ -470,7 +470,7 @@ func (web *Web) importWatch(c *gin.Context) { } for _, filter := range export.Filters { if filter.Type == "cron" { - entryID, err := web.cron.AddFunc(filter.Var1, func() { triggerSchedule(filter.WatchID, web) }) + entryID, err := web.cron.AddFunc(filter.Var1, func() { triggerSchedule(filter.WatchID, web, &filter.ID) }) if err != nil { log.Println("Could not start job for Watch: ", filter.WatchID) continue diff --git a/scraping.go b/scraping.go index 50cfc8a..5cf733d 100644 --- a/scraping.go +++ b/scraping.go @@ -22,13 +22,18 @@ import ( "gorm.io/gorm" ) -func processFilters(filters []Filter, web *Web, watch *Watch, debug bool) { +func processFilters(filters []Filter, web *Web, watch *Watch, debug bool, scheduleID *uint) { allFilters := filters processedMap := make(map[uint]bool, len(filters)) + if scheduleID != nil { + processedMap[*scheduleID] = true + } // collect 'store' filters so we can process them separately at the end storeFilters := make([]Filter, 5) - for len(filters) > 0 { + processedAFilter := true + for len(filters) > 0 && processedAFilter { + processedAFilter = false filter := &filters[0] filters = filters[1:] if filter.Type == "store" { @@ -36,9 +41,13 @@ func processFilters(filters []Filter, web *Web, watch *Watch, debug bool) { processedMap[filter.ID] = true continue } + + if len(filter.Parents) == 0 && !debug { + continue + } var allParentsProcessed = true for _, parent := range filter.Parents { - if _, contains := processedMap[parent.ID]; !contains && parent.Type != "cron" { + if _, contains := processedMap[parent.ID]; !contains { allParentsProcessed = false break } @@ -49,6 +58,7 @@ func processFilters(filters []Filter, web *Web, watch *Watch, debug bool) { } getFilterResult(allFilters, filter, watch, web, debug) processedMap[filter.ID] = true + processedAFilter = true } // process the store filters last @@ -788,7 +798,7 @@ func notifyFilter(filters []Filter, filter *Filter, watch *Watch, web *Web, debu } -func triggerSchedule(watchID uint, web *Web) { +func triggerSchedule(watchID uint, web *Web, scheduleID *uint) { var watch *Watch web.db.Model(&Watch{}).First(&watch, watchID) @@ -798,8 +808,10 @@ func triggerSchedule(watchID uint, web *Web) { var connections []FilterConnection web.db.Model(&FilterConnection{}).Where("watch_id = ?", watch.ID).Find(&connections) + log.Println("Trigger schedule for", watch.ID, ":", *scheduleID) + buildFilterTree(filters, connections) - processFilters(filters, web, watch, false) + processFilters(filters, web, watch, false, scheduleID) } func getFilterResultLua(filter *Filter) { diff --git a/todo.md b/todo.md index 68b95d0..58a6cfc 100644 --- a/todo.md +++ b/todo.md @@ -8,5 +8,4 @@ - sms? - etch? - trusted proxies in conf? -- only run part of filter tree connected to schedule filter - cron jobs not starting on startup \ No newline at end of file