From 9a2e47b23a6d460acfce9b1b77e6f9fb06ca1b75 Mon Sep 17 00:00:00 2001 From: zeripath Date: Sun, 17 May 2020 00:31:38 +0100 Subject: Refactor Cron and merge dashboard tasks (#10745) * Refactor Cron and merge dashboard tasks * Merge Cron and Dashboard tasks * Make every cron task report a system notice on completion * Refactor the creation of these tasks * Ensure that execution counts of tasks is correct * Allow cron tasks to be started from the cron page * golangci-lint fixes * Enforce that only one task with the same name can be registered Signed-off-by: Andrew Thornton * fix name check Signed-off-by: Andrew Thornton * as per @guillep2k * as per @lafriks Signed-off-by: Andrew Thornton * Add git.CommandContext variants Signed-off-by: Andrew Thornton Co-authored-by: Lauris BH Co-authored-by: Lunny Xiao Co-authored-by: techknowlogick --- models/user.go | 51 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 19 deletions(-) (limited to 'models/user.go') diff --git a/models/user.go b/models/user.go index 01cce2506d..8875840db7 100644 --- a/models/user.go +++ b/models/user.go @@ -1321,16 +1321,30 @@ func DeleteUser(u *User) (err error) { return sess.Commit() } -// DeleteInactivateUsers deletes all inactivate users and email addresses. -func DeleteInactivateUsers() (err error) { +// DeleteInactiveUsers deletes all inactive users and email addresses. +func DeleteInactiveUsers(ctx context.Context, olderThan time.Duration) (err error) { users := make([]*User, 0, 10) - if err = x. - Where("is_active = ?", false). - Find(&users); err != nil { - return fmt.Errorf("get all inactive users: %v", err) + if olderThan > 0 { + if err = x. + Where("is_active = ? and created_unix < ?", false, time.Now().Add(-olderThan).Unix()). + Find(&users); err != nil { + return fmt.Errorf("get all inactive users: %v", err) + } + } else { + if err = x. + Where("is_active = ?", false). + Find(&users); err != nil { + return fmt.Errorf("get all inactive users: %v", err) + } + } // FIXME: should only update authorized_keys file once after all deletions. for _, u := range users { + select { + case <-ctx.Done(): + return ErrCancelledf("Before delete inactive user %s", u.Name) + default: + } if err = DeleteUser(u); err != nil { // Ignore users that were set inactive by admin. if IsErrUserOwnRepos(err) || IsErrUserHasOrgs(err) { @@ -1814,25 +1828,23 @@ func synchronizeLdapSSHPublicKeys(usr *User, s *LoginSource, sshPublicKeys []str } // SyncExternalUsers is used to synchronize users with external authorization source -func SyncExternalUsers(ctx context.Context) { +func SyncExternalUsers(ctx context.Context, updateExisting bool) error { log.Trace("Doing: SyncExternalUsers") ls, err := LoginSources() if err != nil { log.Error("SyncExternalUsers: %v", err) - return + return err } - updateExisting := setting.Cron.SyncExternalUsers.UpdateExisting - for _, s := range ls { if !s.IsActived || !s.IsSyncEnabled { continue } select { case <-ctx.Done(): - log.Warn("SyncExternalUsers: Aborted due to shutdown before update of %s", s.Name) - return + log.Warn("SyncExternalUsers: Cancelled before update of %s", s.Name) + return ErrCancelledf("Before update of %s", s.Name) default: } @@ -1850,12 +1862,12 @@ func SyncExternalUsers(ctx context.Context) { Find(&users) if err != nil { log.Error("SyncExternalUsers: %v", err) - return + return err } select { case <-ctx.Done(): - log.Warn("SyncExternalUsers: Aborted due to shutdown before update of %s", s.Name) - return + log.Warn("SyncExternalUsers: Cancelled before update of %s", s.Name) + return ErrCancelledf("Before update of %s", s.Name) default: } @@ -1877,7 +1889,7 @@ func SyncExternalUsers(ctx context.Context) { for _, su := range sr { select { case <-ctx.Done(): - log.Warn("SyncExternalUsers: Aborted due to shutdown at update of %s before completed update of users", s.Name) + log.Warn("SyncExternalUsers: Cancelled at update of %s before completed update of users", s.Name) // Rewrite authorized_keys file if LDAP Public SSH Key attribute is set and any key was added or removed if sshKeysNeedUpdate { err = RewriteAllPublicKeys() @@ -1885,7 +1897,7 @@ func SyncExternalUsers(ctx context.Context) { log.Error("RewriteAllPublicKeys: %v", err) } } - return + return ErrCancelledf("During update of %s before completed update of users", s.Name) default: } if len(su.Username) == 0 { @@ -1980,8 +1992,8 @@ func SyncExternalUsers(ctx context.Context) { select { case <-ctx.Done(): - log.Warn("SyncExternalUsers: Aborted due to shutdown at update of %s before delete users", s.Name) - return + log.Warn("SyncExternalUsers: Cancelled during update of %s before delete users", s.Name) + return ErrCancelledf("During update of %s before delete users", s.Name) default: } @@ -2008,4 +2020,5 @@ func SyncExternalUsers(ctx context.Context) { } } } + return nil } -- cgit v1.2.3