Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show manual cron run's last time #27544

Merged
merged 7 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions services/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ func ListTasks() TaskTable {
next = e.NextRun()
prev = e.PreviousRun()
}

// If the manual run is after the cron run, use that instead.
if prev.Before(task.LastRun) {
prev = task.LastRun
}
Comment on lines +111 to +113
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data race bug.

-> Fix data-race bug when accessing task.LastRun #27584


task.lock.Lock()
tTable = append(tTable, &TaskTableRow{
Name: task.Name,
Expand Down
9 changes: 9 additions & 0 deletions services/cron/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"strings"
"sync"
"time"

"code.gitea.io/gitea/models/db"
system_model "code.gitea.io/gitea/models/system"
Expand Down Expand Up @@ -37,6 +38,8 @@ type Task struct {
LastMessage string
LastDoer string
ExecTimes int64
// This stores the time of the last manual run of this task.
LastRun time.Time
}

// DoRunAtStart returns if this task should run at the start
Expand Down Expand Up @@ -88,6 +91,12 @@ func (t *Task) RunWithUser(doer *user_model.User, config Config) {
}
}()
graceful.GetManager().RunWithShutdownContext(func(baseCtx context.Context) {
// Store the time of this run, before the function is executed, so it
// matches the behavior of what the cron library does.
t.lock.Lock()
t.LastRun = time.Now()
t.lock.Unlock()

pm := process.GetManager()
doerName := ""
if doer != nil && doer.ID != -1 {
Expand Down
51 changes: 51 additions & 0 deletions tests/integration/api_admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"testing"
"time"

asymkey_model "code.gitea.io/gitea/models/asymkey"
auth_model "code.gitea.io/gitea/models/auth"
Expand Down Expand Up @@ -282,3 +283,53 @@ func TestAPIRenameUser(t *testing.T) {
})
MakeRequest(t, req, http.StatusOK)
}

func TestAPICron(t *testing.T) {
defer tests.PrepareTestEnv(t)()

// user1 is an admin user
session := loginUser(t, "user1")

t.Run("List", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()

token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadAdmin)
urlStr := fmt.Sprintf("/api/v1/admin/cron?token=%s", token)
req := NewRequest(t, "GET", urlStr)
resp := MakeRequest(t, req, http.StatusOK)

assert.Equal(t, "28", resp.Header().Get("X-Total-Count"))

var crons []api.Cron
DecodeJSON(t, resp, &crons)
assert.Len(t, crons, 28)
})

t.Run("Execute", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()

now := time.Now()
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteAdmin)
// Archive cleanup is harmless, because in the test environment there are none
// and is thus an NOOP operation and therefore doesn't interfere with any other
// tests.
urlStr := fmt.Sprintf("/api/v1/admin/cron/archive_cleanup?token=%s", token)
req := NewRequest(t, "POST", urlStr)
MakeRequest(t, req, http.StatusNoContent)

// Check for the latest run time for this cron, to ensure it
// has been run.
silverwind marked this conversation as resolved.
Show resolved Hide resolved
urlStr = fmt.Sprintf("/api/v1/admin/cron?token=%s", token)
req = NewRequest(t, "GET", urlStr)
resp := MakeRequest(t, req, http.StatusOK)

var crons []api.Cron
DecodeJSON(t, resp, &crons)

for _, cron := range crons {
if cron.Name == "archive_cleanup" {
assert.True(t, now.Before(cron.Prev))
}
}
})
}