mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender-pwa.git
synced 2025-07-16 09:38:51 +02:00
75 lines
3.0 KiB
Go
75 lines
3.0 KiB
Go
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
|
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
|
|
|
//This program is free software: you can redistribute it and/or modify
|
|
//it under the terms of the GNU Affero General Public License as published by
|
|
//the Free Software Foundation, either version 3 of the License, or
|
|
//(at your option) any later version.
|
|
|
|
//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 for more details.
|
|
|
|
//You should have received a copy of the GNU Affero General Public License
|
|
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package service
|
|
|
|
import (
|
|
"github.com/pocketbase/pocketbase"
|
|
"github.com/pocketbase/pocketbase/core"
|
|
"github.com/pocketbase/pocketbase/tools/cron"
|
|
"htwkalender/service/course"
|
|
"htwkalender/service/feed"
|
|
"htwkalender/service/fetch/sport"
|
|
v2 "htwkalender/service/fetch/v2"
|
|
"htwkalender/service/functions/time"
|
|
"log/slog"
|
|
"strconv"
|
|
)
|
|
|
|
func AddSchedules(app *pocketbase.PocketBase) {
|
|
|
|
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
scheduler := cron.New()
|
|
|
|
// Every hour update all courses (5 segments - minute, hour, day, month, weekday) "0 * * * *"
|
|
// Every three hours update all courses (5 segments - minute, hour, day, month, weekday) "0 */3 * * *"
|
|
// Every 10 minutes update all courses (5 segments - minute, hour, day, month, weekday) "*/10 * * * *"
|
|
scheduler.MustAdd("updateCourse", "0 */3 * * *", func() {
|
|
slog.Info("Started updating courses schedule")
|
|
course.UpdateCourse(app)
|
|
})
|
|
|
|
// Every sunday at 1am clean all courses (5 segments - minute, hour, day, month, weekday) "0 3 * * 0"
|
|
scheduler.MustAdd("cleanFeeds", "0 1 * * 0", func() {
|
|
// clean feeds older than 6 months
|
|
slog.Info("Started cleaning feeds schedule")
|
|
feed.ClearFeeds(app.Dao(), 6, time.RealClock{})
|
|
})
|
|
|
|
// Every sunday at 3am fetch all sport events (5 segments - minute, hour, day, month, weekday) "0 2 * * 0"
|
|
scheduler.MustAdd("fetchSportEvents", "0 3 * * 0", func() {
|
|
slog.Info("Started fetching sport events schedule")
|
|
sportEvents, err := sport.FetchAndUpdateSportEvents(app)
|
|
if err != nil {
|
|
slog.Error("Failed to fetch and save sport events: %v", "error", err)
|
|
}
|
|
slog.Info("Successfully fetched " + strconv.FormatInt(int64(len(sportEvents)), 10) + " sport events")
|
|
})
|
|
|
|
//fetch all events for semester and delete from remote this should be done every sunday at 2am
|
|
scheduler.MustAdd("fetchEvents", "0 2 * * 0", func() {
|
|
savedEvents, err := v2.FetchAllEventsAndSave(app, time.RealClock{})
|
|
if err != nil {
|
|
slog.Error("Failed to fetch and save events: %v", "error", err)
|
|
} else {
|
|
slog.Info("Successfully fetched " + strconv.FormatInt(int64(len(savedEvents)), 10) + " events")
|
|
}
|
|
})
|
|
scheduler.Start()
|
|
return nil
|
|
})
|
|
}
|