repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
c9de2c5
parent
6d580be
author
Eric Bower
date
2024-01-20 02:00:44 +0000 UTC
fix(feeds): move cron to web app

Our `Dockerfile` doesn't move template files into their respective
images when it's an SSH app.  feeds' cron requires those template files
in order to send emails.
3 files changed,  +19, -14
M feeds/api.go
+4, -0
 1@@ -56,6 +56,10 @@ func StartApiServer() {
 2 		logger.Fatal(err)
 3 	}
 4 
 5+	// cron daily digest
 6+	fetcher := NewFetcher(db, cfg)
 7+	go fetcher.Loop()
 8+
 9 	staticRoutes := createStaticRoutes()
10 
11 	if cfg.Debug {
M feeds/cron.go
+15, -10
 1@@ -137,10 +137,10 @@ func (f *Fetcher) RunPost(user *db.User, post *db.Post) error {
 2 
 3 	parsed := shared.ListParseText(post.Text, shared.NewNullLinkify())
 4 
 5-	f.cfg.Logger.Infof("Last digest at (%s)", post.Data.LastDigest)
 6+	f.cfg.Logger.Infof("(%s) Last digest at (%s)", user.Name, post.Data.LastDigest)
 7 	err := f.Validate(post.Data.LastDigest, parsed)
 8 	if err != nil {
 9-		f.cfg.Logger.Info(err.Error())
10+		f.cfg.Logger.Infof("(%s) %s", user.Name, err.Error())
11 		return nil
12 	}
13 
14@@ -160,7 +160,7 @@ func (f *Fetcher) RunPost(user *db.User, post *db.Post) error {
15 		urls = append(urls, url)
16 	}
17 
18-	msgBody, err := f.FetchAll(urls, parsed.InlineContent, post.ID)
19+	msgBody, err := f.FetchAll(urls, parsed.InlineContent, post.ID, user.Name)
20 	if err != nil {
21 		return err
22 	}
23@@ -190,7 +190,7 @@ func (f *Fetcher) RunUser(user *db.User) error {
24 	for _, post := range posts.Data {
25 		err = f.RunPost(user, post)
26 		if err != nil {
27-			f.cfg.Logger.Infof(err.Error())
28+			f.cfg.Logger.Infof("(%s) %s", user.Name, err.Error())
29 		}
30 	}
31 
32@@ -231,8 +231,8 @@ func (f *Fetcher) ParseURL(fp *gofeed.Parser, url string) (*gofeed.Feed, error)
33 	return feed, nil
34 }
35 
36-func (f *Fetcher) Fetch(fp *gofeed.Parser, url string, feedItems []*db.FeedItem) (*Feed, error) {
37-	f.cfg.Logger.Infof("(%s) fetching feed", url)
38+func (f *Fetcher) Fetch(fp *gofeed.Parser, url string, username string, feedItems []*db.FeedItem) (*Feed, error) {
39+	f.cfg.Logger.Infof("(%s) %s fetching feed", username, url)
40 
41 	feed, err := f.ParseURL(fp, url)
42 	if err != nil {
43@@ -262,7 +262,12 @@ func (f *Fetcher) Fetch(fp *gofeed.Parser, url string, feedItems []*db.FeedItem)
44 	}
45 
46 	if len(items) == 0 {
47-		return nil, fmt.Errorf("(%s) %w, skipping", url, ErrNoRecentArticles)
48+		return nil, fmt.Errorf(
49+			"(%s) %s %w, skipping",
50+			username,
51+			url,
52+			ErrNoRecentArticles,
53+		)
54 	}
55 
56 	feedTmpl.FeedItems = gofeedItems
57@@ -311,7 +316,7 @@ type MsgBody struct {
58 	Text string
59 }
60 
61-func (f *Fetcher) FetchAll(urls []string, inlineContent bool, postID string) (*MsgBody, error) {
62+func (f *Fetcher) FetchAll(urls []string, inlineContent bool, postID string, username string) (*MsgBody, error) {
63 	fp := gofeed.NewParser()
64 	feeds := &DigestFeed{Options: DigestOptions{InlineContent: inlineContent}}
65 	feedItems, err := f.db.FindFeedItemsByPostID(postID)
66@@ -320,7 +325,7 @@ func (f *Fetcher) FetchAll(urls []string, inlineContent bool, postID string) (*M
67 	}
68 
69 	for _, url := range urls {
70-		feedTmpl, err := f.Fetch(fp, url, feedItems)
71+		feedTmpl, err := f.Fetch(fp, url, username, feedItems)
72 		if err != nil {
73 			if errors.Is(err, ErrNoRecentArticles) {
74 				f.cfg.Logger.Info(err)
75@@ -333,7 +338,7 @@ func (f *Fetcher) FetchAll(urls []string, inlineContent bool, postID string) (*M
76 	}
77 
78 	if len(feeds.Feeds) == 0 {
79-		return nil, fmt.Errorf("%w, skipping", ErrNoRecentArticles)
80+		return nil, fmt.Errorf("(%s) %w, skipping email", username, ErrNoRecentArticles)
81 	}
82 
83 	fdi := []*db.FeedItem{}
M feeds/ssh.go
+0, -4
 1@@ -84,10 +84,6 @@ func StartSshServer() {
 2 		logger.Fatal(err)
 3 	}
 4 
 5-	// cron daily digest
 6-	fetcher := NewFetcher(dbh, cfg)
 7-	go fetcher.Loop()
 8-
 9 	handler := filehandlers.NewScpPostHandler(dbh, cfg, hooks, st)
10 
11 	sshServer := &SSHServer{}