Goit

Simple and lightweight Git web server
git clone https://git.omkov.net/Goit
git clone [email protected]:Goit
Log | Tree | Refs | README | Download

Goit/src/admin/cron.go (59 lines, 1.2 KiB) -rw-r--r-- blame download

012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
// Copyright (C) 2023, Jakob Wakeling
// All rights reserved.

package admin

import (
	"fmt"
	"net/http"
	"time"

	"github.com/Jamozed/Goit/src/goit"
	"github.com/Jamozed/Goit/src/util"
)

func HandleCron(w http.ResponseWriter, r *http.Request) {
	auth, user, err := goit.Auth(w, r, true)
	if err != nil {
		util.PrintError(err)
		goit.HttpError(w, http.StatusInternalServerError)
		return
	}

	if !auth || !user.IsAdmin {
		goit.HttpError(w, http.StatusNotFound)
		return
	}

	type row struct{ ID, Description, Repo, Schedule, Next, Last string }
	data := struct {
		Title string
		Jobs  []row
	}{Title: "Admin - Cron"}

	for _, job := range goit.Cron.Jobs() {
		repo := &goit.Repo{}

		if job.RID != -1 {
			if r, err := goit.GetRepo(job.RID); err != nil {
				util.PrintError(err)
			} else if r != nil {
				repo = r
			}
		}

		data.Jobs = append(data.Jobs, row{
			ID:          fmt.Sprint(job.ID),
			Description: util.If(repo.Name != "", repo.Name, "Session Cleanup"),
			Repo:        repo.Name,
			Schedule:    job.Schedule.String(),
			Next:        job.Next.String(),
			Last:        util.If(job.Last == time.Time{}, "never", job.Last.String()),
		})
	}

	if err := goit.Tmpl.ExecuteTemplate(w, "admin/cron", data); err != nil {
		util.PrintError(err)
	}
}