From 36e41939772d0441dc023f7b02e96567441b02ec Mon Sep 17 00:00:00 2001 From: Julio Capote Date: Sun, 1 Jan 2023 20:21:10 -0500 Subject: execute tick on boot, add real world handler example --- autosport-handler.sh | 4 ++++ cgi/servers.go | 27 +++++++++++++++++++++------ sample-config.toml | 16 +++++++++++----- 3 files changed, 36 insertions(+), 11 deletions(-) create mode 100755 autosport-handler.sh diff --git a/autosport-handler.sh b/autosport-handler.sh new file mode 100755 index 0000000..d9e3352 --- /dev/null +++ b/autosport-handler.sh @@ -0,0 +1,4 @@ +#!/bin/bash +echo "Content-type: application/xml" +echo "" +curl -s https://www.autosport.com/rss/f1/news/ diff --git a/cgi/servers.go b/cgi/servers.go index 58d72c1..a3c7e47 100644 --- a/cgi/servers.go +++ b/cgi/servers.go @@ -32,15 +32,28 @@ func (s *Servers) Start(cfg config.Config) { var wg sync.WaitGroup for _, handler := range cfg.Handlers { - wg.Add(2) + wg.Add(3) + // Internal CGI server go func(aHandler config.Handler) { defer wg.Done() startCGIServer(aHandler) }(handler) + // Ticker go func(aHandler config.Handler) { defer wg.Done() startTicker(aHandler, s.persister, s.log) }(handler) + // Execute a handler tick on start since Go's ticker waits until $interval to trigger first tick + go func(aHandler config.Handler) { + defer wg.Done() + time.Sleep(1 * time.Second) + output := tick(aHandler) + err := processTick(aHandler, output, s.persister, s.log) + if err != nil { + s.log.Error(err) + } + }(handler) + } wg.Wait() } @@ -89,11 +102,13 @@ func processTick(h config.Handler, output []byte, persister *models.Persister, l } for _, v := range feed.Items { - log.Debugf("found content '%s'", v.Content) - outboxItem := models.CreateOutboxItem(h, []byte(v.Content)) - err = persister.Store(outboxItem) - if err != nil { - return err + if len(v.Description) != 0 { + log.Debugf("found description '%s'", v.Description) + outboxItem := models.CreateOutboxItem(h, []byte(v.Description)) + err = persister.Store(outboxItem) + if err != nil { + return err + } } } return nil diff --git a/sample-config.toml b/sample-config.toml index a77678c..7cd02f7 100644 --- a/sample-config.toml +++ b/sample-config.toml @@ -1,13 +1,19 @@ domain = "activitybub.xyz" dbPath = "bub.db" +# [[handlers]] +# name = "sample" +# rpc = "cgi" # rename to protocol? +# exec = "sample-cgi-handler.sh" +# interval = "5s" +# dedupWindow = "1m" + [[handlers]] -name = "sample" +name = "autosport" rpc = "cgi" # rename to protocol? -# add response type? like Note -exec = "sample-cgi-handler.sh" -interval = "5s" -dedupWindow = "1m" +exec = "autosport-handler.sh" +interval = "10m" +dedupWindow = "900h" # [[handlers]] # name = "another" -- cgit v1.2.3