2025-01-19 06:02:07 -06:00
|
|
|
package job
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
|
|
|
|
|
"github.com/go-co-op/gocron/v2"
|
2025-02-05 18:08:01 +01:00
|
|
|
"github.com/pocket-id/pocket-id/backend/internal/service"
|
2025-01-19 06:02:07 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type LdapJobs struct {
|
|
|
|
|
ldapService *service.LdapService
|
|
|
|
|
appConfigService *service.AppConfigService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RegisterLdapJobs(ldapService *service.LdapService, appConfigService *service.AppConfigService) {
|
|
|
|
|
jobs := &LdapJobs{ldapService: ldapService, appConfigService: appConfigService}
|
|
|
|
|
|
|
|
|
|
scheduler, err := gocron.NewScheduler()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Failed to create a new scheduler: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register the job to run every hour
|
|
|
|
|
registerJob(scheduler, "SyncLdap", "0 * * * *", jobs.syncLdap)
|
|
|
|
|
|
|
|
|
|
// Run the job immediately on startup
|
|
|
|
|
if err := jobs.syncLdap(); err != nil {
|
2025-01-19 13:09:16 +01:00
|
|
|
log.Printf("Failed to sync LDAP: %s", err)
|
2025-01-19 06:02:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scheduler.Start()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j *LdapJobs) syncLdap() error {
|
|
|
|
|
if j.appConfigService.DbConfig.LdapEnabled.Value == "true" {
|
|
|
|
|
return j.ldapService.SyncAll()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|