Browse Source

Add daily RotateFeedsJob

twitter_support
James Mills 10 months ago
parent
commit
48a5c1d5b0
Signed by: prologic GPG Key ID: AC4C014F1440EBD6
  1. 1
      .gitignore
  2. 1
      config.go
  3. 3
      config.yaml.sample
  4. 41
      jobs.go
  5. 5
      utils.go

1
.gitignore

@ -1,6 +1,7 @@
*~
*.bak
*.txt
.DS_Store
/feeds
/rss2twtxt

1
config.go

@ -9,6 +9,7 @@ import (
type Config struct {
Root string
BaseURL string
MaxSize int64 // maximum feed size before rotating
Feeds map[string]string // name -> url
path string // path to config file that was loaded used by .Save()

3
config.yaml.sample

@ -1,5 +1,6 @@
---
root: ./feeds
baseURL: /
baseurl: /
maxsize: 1048576
feeds:
unexplained_mysteries: http://www.unexplained-mysteries.com/news/umnews.xml

41
jobs.go

@ -7,6 +7,7 @@ import (
"time"
"github.com/divan/num2words"
"github.com/dustin/go-humanize"
"github.com/robfig/cron"
log "github.com/sirupsen/logrus"
)
@ -15,6 +16,7 @@ var Jobs map[string]JobFactory
func init() {
Jobs = map[string]JobFactory{
"@daily": NewRotateFeedsJob,
"@every 5m": NewUpdateFeedsJob,
"0 0,30 * * * *": NewTikTokJob,
}
@ -22,6 +24,45 @@ func init() {
type JobFactory func(conf *Config) cron.Job
type RotateFeedsJob struct {
conf *Config
}
func NewRotateFeedsJob(conf *Config) cron.Job {
return &RotateFeedsJob{conf: conf}
}
func (job *RotateFeedsJob) Run() {
conf := job.conf
files, err := WalkMatch(conf.Root, "*.txt")
if err != nil {
log.WithError(err).Error("error reading feeds directory")
return
}
for _, file := range files {
stat, err := os.Stat(file)
if err != nil {
log.WithError(err).Error("error getting feed size: %s", err)
continue
}
if stat.Size() > conf.MaxSize {
log.Infof(
"rotating %s with size %s > %s",
BaseWithoutExt(file),
humanize.Bytes(uint64(stat.Size())),
humanize.Bytes(uint64(conf.MaxSize)),
)
if err := RotateFile(file); err != nil {
log.WithError(err).Errorf("error rotating feed: %s", err)
}
}
}
}
type UpdateFeedsJob struct {
conf *Config
}

5
utils.go

@ -31,6 +31,11 @@ var (
ErrInvalidImage = errors.New("error: invalid image")
)
func RotateFile(fn string) error {
now := time.Now().Unix()
return os.Rename(fn, fmt.Sprintf("%s.%d", fn, now))
}
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {

Loading…
Cancel
Save