-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae9cc3e
commit 6cd8ca6
Showing
8 changed files
with
175 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cron | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/charmbracelet/log" | ||
"github.com/robfig/cron/v3" | ||
) | ||
|
||
// CronScheduler is a cron-like job scheduler. | ||
type CronScheduler struct { | ||
*cron.Cron | ||
logger cron.Logger | ||
} | ||
|
||
// Entry is a cron job. | ||
type Entry struct { | ||
ID cron.EntryID | ||
Desc string | ||
Spec string | ||
} | ||
|
||
// cronLogger is a wrapper around the logger to make it compatible with the | ||
// cron logger. | ||
type cronLogger struct { | ||
logger *log.Logger | ||
} | ||
|
||
// Info logs routine messages about cron's operation. | ||
func (l cronLogger) Info(msg string, keysAndValues ...interface{}) { | ||
l.logger.Info(msg, keysAndValues...) | ||
} | ||
|
||
// Error logs an error condition. | ||
func (l cronLogger) Error(err error, msg string, keysAndValues ...interface{}) { | ||
l.logger.Error(msg, append(keysAndValues, "err", err)...) | ||
} | ||
|
||
// NewCronScheduler returns a new Cron. | ||
func NewCronScheduler() *CronScheduler { | ||
logger := cronLogger{log.WithPrefix("server.cron")} | ||
return &CronScheduler{ | ||
Cron: cron.New(cron.WithLogger(logger)), | ||
} | ||
} | ||
|
||
// Shutdonw gracefully shuts down the CronServer. | ||
func (s *CronScheduler) Shutdown() { | ||
ctx, cancel := context.WithTimeout(s.Cron.Stop(), 30*time.Second) | ||
defer func() { cancel() }() | ||
<-ctx.Done() | ||
} | ||
|
||
// Start starts the CronServer. | ||
func (s *CronScheduler) Start() { | ||
s.Cron.Start() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package server | ||
|
||
import ( | ||
"github.com/charmbracelet/soft-serve/git" | ||
"github.com/charmbracelet/soft-serve/server/backend" | ||
) | ||
|
||
var ( | ||
jobSpecs = map[string]string{ | ||
"mirror": "@every 10m", | ||
} | ||
) | ||
|
||
// mirrorJob runs the (pull) mirror job task. | ||
func mirrorJob(b backend.Backend) func() { | ||
logger := logger.WithPrefix("server.mirrorJob") | ||
return func() { | ||
repos, err := b.Repositories() | ||
if err != nil { | ||
logger.Error("error getting repositories", "err", err) | ||
return | ||
} | ||
|
||
for _, repo := range repos { | ||
if repo.IsMirror() { | ||
logger.Debug("updating mirror", "repo", repo.Name()) | ||
r, err := repo.Open() | ||
if err != nil { | ||
logger.Error("error opening repository", "repo", repo.Name(), "err", err) | ||
continue | ||
} | ||
|
||
cmd := git.NewCommand("remote", "update", "--prune") | ||
if _, err := cmd.RunInDir(r.Path); err != nil { | ||
logger.Error("error running git remote update", "repo", repo.Name(), "err", err) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters