Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

AuthorJakob Wakeling <[email protected]>
Date2023-12-15 11:03:03
Commit2d1243ed20cccba9b3cc7d6daf1c0fd17813ef64
Parent570144e80c9e9b0f73733fce92c0af0253f4a88e

Support mirror repositories

Diffstat

M src/cron/schedule.go | 12 +++++++++++-
M src/goit/goit.go | 24 +++++++++++++++++++++++-
M src/main.go | 8 --------
M src/repo/import.go | 15 +++++++++------

4 files changed, 43 insertions, 16 deletions

diff --git a/src/cron/schedule.go b/src/cron/schedule.go
index 7690775..4a83656 100644
--- a/src/cron/schedule.go
+++ b/src/cron/schedule.go
@@ -9,9 +9,19 @@ import (
 
 type Schedule struct{ Month, Day, Weekday, Hour, Minute, Second int64 }
 
-var Immediate = Schedule{-1, -1, -1, -1, -1, -1}
+var (
+	Immediate = Schedule{-1, -1, -1, -1, -1, -1}
+	Yearly    = Schedule{1, 1, -1, 0, 0, 0}
+	Monthly   = Schedule{-1, 1, -1, 0, 0, 0}
+	Weekly    = Schedule{-1, -1, 1, 0, 0, 0}
+	Daily     = Schedule{-1, -1, -1, 0, 0, 0}
+	Hourly    = Schedule{-1, -1, -1, -1, 0, 0}
+	Minutely  = Schedule{-1, -1, -1, -1, -1, 0}
+)
 
 func (s Schedule) Next(t time.Time) time.Time {
+	t = t.Add(1 * time.Second)
+
 	added := false
 
 wrap:
diff --git a/src/goit/goit.go b/src/goit/goit.go
index df0480a..f2d405c 100644
--- a/src/goit/goit.go
+++ b/src/goit/goit.go
@@ -118,12 +118,34 @@ func Goit(conf string) (err error) {
 	Cron = cron.New()
 	Cron.Start()
 
+	/* Periodically clean up expired sessions */
+	Cron.Add(cron.Hourly, func() { CleanupSessions() })
+
+	/* Add cron jobs for mirror repositories */
+	repos, err := GetRepos()
+	if err != nil {
+		return err
+	}
+
+	for _, r := range repos {
+		if r.IsMirror {
+			util.Debugln("Adding cron job for", r.Name)
+			Cron.Add(cron.Daily, func() {
+				if err := Pull(r.Id); err != nil {
+					log.Println("[cron:mirror]", err.Error())
+				}
+			})
+		}
+	}
+
+	Cron.Update()
+
 	return nil
 }
 
 func ConfPath() string {
 	if p, err := xdg.SearchConfigFile(filepath.Join("goit", "goit.json")); err != nil {
-		log.Println("[Config]", err.Error())
+		log.Println("[config]", err.Error())
 		return ""
 	} else {
 		return p
diff --git a/src/main.go b/src/main.go
index 9ebbbe8..1a53f50 100644
--- a/src/main.go
+++ b/src/main.go
@@ -150,14 +150,6 @@ func main() {
 	// h.Get("/{repo}/git-receive-pack", goit.HandleReceivePack)
 	// h.Post("/{repo}/git-receive-pack", goit.HandleReceivePack)
 
-	/* Create a ticker to periodically cleanup expired sessions */
-	tick := time.NewTicker(1 * time.Hour)
-	go func() {
-		for range tick.C {
-			goit.CleanupSessions()
-		}
-	}()
-
 	/* Listen for IPC */
 	ipc, err := net.Listen("unix", filepath.Join(xdg.RuntimeDir, "goit-"+goit.Conf.HttpPort+".sock"))
 	if err != nil {
diff --git a/src/repo/import.go b/src/repo/import.go
index e70b4d6..a3279e2 100644
--- a/src/repo/import.go
+++ b/src/repo/import.go
@@ -12,6 +12,7 @@ import (
 
 	"github.com/Jamozed/Goit/src/cron"
 	"github.com/Jamozed/Goit/src/goit"
+	"github.com/Jamozed/Goit/src/util"
 	"github.com/gorilla/csrf"
 )
 
@@ -66,13 +67,15 @@ func HandleImport(w http.ResponseWriter, r *http.Request) {
 			goit.HttpError(w, http.StatusInternalServerError)
 			return
 		} else {
-			goit.Cron.Add(cron.Immediate, func() {
-				if err := goit.Pull(rid); err != nil {
-					log.Println("[/repo/import:cron]", err.Error())
-				}
-			})
+			if data.Url != "" {
+				goit.Cron.Add(util.If(data.IsMirror, cron.Daily, cron.Immediate), func() {
+					if err := goit.Pull(rid); err != nil {
+						log.Println("[cron:import]", err.Error())
+					}
+				})
 
-			goit.Cron.Update()
+				goit.Cron.Update()
+			}
 
 			http.Redirect(w, r, "/"+data.Name, http.StatusFound)
 			return