78 lines
2.7 KiB
Go
Executable file
78 lines
2.7 KiB
Go
Executable file
/*
|
|
ScheduleTogether Backend
|
|
Copyright (C) 2024, Zervó Zadachin
|
|
|
|
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License version 3
|
|
as published by the Free Software Foundation.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License version 3 for more details.
|
|
|
|
This program incorporates external libraries for certain functionalities.
|
|
These libraries are covered by their respective licenses, and their usage
|
|
agreements are as outlined in their respective documentation or source
|
|
code.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package workers
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
"git.zervo.org/scheduletogether/scheduletogetherbackend/pkg/helpers/logging"
|
|
)
|
|
|
|
var logboi = logging.NewLogger("workers")
|
|
|
|
// Reporter is a wrapper of Logger customized for workers
|
|
type Reporter struct {
|
|
workerName string
|
|
jobName string
|
|
startTime time.Time
|
|
}
|
|
|
|
// NewReporter creates a new Reporter instance
|
|
func NewReporter(workerName string, jobName string) *Reporter {
|
|
logboi.Info("<" + workerName + "/" + jobName + "> " + "[ " + logging.Yellow + logging.Bold + "START " + logging.Reset + " ]")
|
|
return &Reporter{workerName: workerName, jobName: jobName, startTime: time.Now()}
|
|
}
|
|
|
|
// Info wraps logboi.Info and reports job progress
|
|
func (l *Reporter) Status(msg string) {
|
|
logboi.Info("<" + l.workerName + "/" + l.jobName + "> " + "[ " + logging.Cyan + logging.Bold + "STATUS" + logging.Reset + " ] " + msg)
|
|
}
|
|
|
|
// Fail wraps logboi.Err and reports job failure
|
|
func (l *Reporter) Fail(msg string) {
|
|
logboi.Info("<" + l.workerName + "/" + l.jobName + "> " + "[ " + logging.Red + logging.Bold + "FAIL " + logging.Reset + " ] " + msg)
|
|
logboi.Err("Job failed! Task took " + time.Since(l.startTime).String() + ".")
|
|
}
|
|
|
|
// Finish wraps logboi.Info and announces end of a job
|
|
func (l *Reporter) Finish() {
|
|
logboi.Info("<" + l.workerName + "/" + l.jobName + "> " + "[ " + logging.Green + logging.Bold + "OK " + logging.Reset + " ]")
|
|
logboi.Info("Job took \"" + l.jobName + "\" " + time.Since(l.startTime).String() + " to complete.")
|
|
}
|
|
|
|
// Schedule workers with cron
|
|
func ScheduleWorkers() {
|
|
c := cron.New()
|
|
logboi.Info("Scheduling worker jobs...")
|
|
|
|
// Add functions / jobs
|
|
c.AddFunc("@every 30m", workerFriends_PurgeAbandonedRequests)
|
|
c.AddFunc("@every 30m", workerSchedules_PurgeAbandonedData)
|
|
|
|
c.Start()
|
|
logboi.Info("Worker jobs scheduled")
|
|
}
|