2022-03-13 12:45:07 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-05-09 09:19:29 +00:00
|
|
|
"sync/atomic"
|
2022-03-13 12:45:07 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
|
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/metric"
|
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-07-11 06:17:36 +00:00
|
|
|
scheduler *cron.Cron
|
|
|
|
lastUserCacheUpdate int64
|
|
|
|
// used for bolt and memory providers, so we avoid iterating all users/rules
|
2022-05-09 09:19:29 +00:00
|
|
|
// to find recently modified ones
|
|
|
|
lastUserUpdate int64
|
2022-07-11 06:17:36 +00:00
|
|
|
lastRuleUpdate int64
|
2022-03-13 12:45:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func stopScheduler() {
|
|
|
|
if scheduler != nil {
|
|
|
|
scheduler.Stop()
|
|
|
|
scheduler = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startScheduler() error {
|
|
|
|
stopScheduler()
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
scheduler = cron.New(cron.WithLocation(time.UTC))
|
|
|
|
_, err := scheduler.AddFunc("@every 60s", checkDataprovider)
|
2022-03-13 12:45:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to schedule dataprovider availability check: %w", err)
|
|
|
|
}
|
|
|
|
err = addScheduledCacheUpdates()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
EventManager.loadRules()
|
2022-03-13 12:45:07 +00:00
|
|
|
scheduler.Start()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addScheduledCacheUpdates() error {
|
2022-07-11 06:17:36 +00:00
|
|
|
lastUserCacheUpdate = util.GetTimeAsMsSinceEpoch(time.Now())
|
2022-03-13 12:45:07 +00:00
|
|
|
_, err := scheduler.AddFunc("@every 10m", checkCacheUpdates)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to schedule cache updates: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkDataprovider() {
|
|
|
|
err := provider.checkAvailability()
|
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "check availability error: %v", err)
|
|
|
|
}
|
|
|
|
metric.UpdateDataProviderAvailability(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkCacheUpdates() {
|
2022-07-11 06:17:36 +00:00
|
|
|
providerLog(logger.LevelDebug, "start caches check, update time %v", util.GetTimeFromMsecSinceEpoch(lastUserCacheUpdate))
|
2022-03-13 12:45:07 +00:00
|
|
|
checkTime := util.GetTimeAsMsSinceEpoch(time.Now())
|
2022-07-11 06:17:36 +00:00
|
|
|
users, err := provider.getRecentlyUpdatedUsers(lastUserCacheUpdate)
|
2022-03-13 12:45:07 +00:00
|
|
|
if err != nil {
|
|
|
|
providerLog(logger.LevelError, "unable to get recently updated users: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
2022-07-17 16:48:41 +00:00
|
|
|
providerLog(logger.LevelDebug, "invalidate caches for user %q", user.Username)
|
|
|
|
if user.DeletedAt > 0 {
|
|
|
|
deletedAt := util.GetTimeFromMsecSinceEpoch(user.DeletedAt)
|
|
|
|
if deletedAt.Add(30 * time.Minute).Before(time.Now()) {
|
|
|
|
providerLog(logger.LevelDebug, "removing user %q deleted at %s", user.Username, deletedAt)
|
|
|
|
go provider.deleteUser(user, false) //nolint:errcheck
|
|
|
|
}
|
|
|
|
webDAVUsersCache.remove(user.Username)
|
|
|
|
delayedQuotaUpdater.resetUserQuota(user.Username)
|
|
|
|
} else {
|
|
|
|
webDAVUsersCache.swap(&user)
|
|
|
|
}
|
2022-03-13 12:45:07 +00:00
|
|
|
cachedPasswords.Remove(user.Username)
|
|
|
|
}
|
|
|
|
|
2022-07-11 06:17:36 +00:00
|
|
|
lastUserCacheUpdate = checkTime
|
|
|
|
EventManager.loadRules()
|
|
|
|
providerLog(logger.LevelDebug, "end caches check, new update time %v", util.GetTimeFromMsecSinceEpoch(lastUserCacheUpdate))
|
2022-03-13 12:45:07 +00:00
|
|
|
}
|
2022-05-09 09:19:29 +00:00
|
|
|
|
|
|
|
func setLastUserUpdate() {
|
|
|
|
atomic.StoreInt64(&lastUserUpdate, util.GetTimeAsMsSinceEpoch(time.Now()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLastUserUpdate() int64 {
|
|
|
|
return atomic.LoadInt64(&lastUserUpdate)
|
|
|
|
}
|
2022-07-11 06:17:36 +00:00
|
|
|
|
|
|
|
func setLastRuleUpdate() {
|
|
|
|
atomic.StoreInt64(&lastRuleUpdate, util.GetTimeAsMsSinceEpoch(time.Now()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLastRuleUpdate() int64 {
|
|
|
|
return atomic.LoadInt64(&lastRuleUpdate)
|
|
|
|
}
|