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

CORE-1977: fetch the time_limit_seconds from the DB for a job #6

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Changes from all 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
32 changes: 30 additions & 2 deletions analyses.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,28 @@ func getUserIDForJob(ctx context.Context, dedb *sql.DB, analysisID string) (stri
return userID, nil
}

// getTimeLimitQuery is the query for calculating a number-of-seconds time limit for a job
// if a time_limit_seconds is not set for a tool, use 72 hours (72 * 60 * 60 = 259200)
const getTimeLimitQuery = `
SELECT sum(CASE WHEN tools.time_limit_seconds > 0 THEN tools.time_limit_seconds ELSE 259200 END)
FROM tools
JOIN tasks ON tools.id = tasks.tool_id
JOIN app_steps ON tasks.id = app_steps.task_id
JOIN jobs ON jobs.app_version_id = app_steps.app_version_id
WHERE jobs.id = ?
`

func getTimeLimit(ctx context.Context, dedb *sql.DB, analysisID string) (int64, error) {
var (
err error
timeLimitSeconds int64
)
if err = dedb.QueryRowContext(ctx, getTimeLimitQuery, analysisID).Scan(&timeLimitSeconds); err != nil {
return 0, err
}
return timeLimitSeconds, nil
}

// CreateMessageHandler returns a function that can be used by the messaging
// package to handle job status messages. The handler will set the planned
// end date for an analysis if it's not already set.
Expand Down Expand Up @@ -600,9 +622,15 @@ func CreateMessageHandler(dedb *sql.DB) func(context.Context, amqp.Delivery) {
}
sdnano := startDate.UnixNano()

// StartDate is in milliseconds, so convert it to nanoseconds, add 48 hours,
timeLimitSeconds, err := getTimeLimit(ctx, dedb, analysis.ID)
if err != nil {
log.Error(errors.Wrapf(err, "error fetching time limit for analysis %s", analysis.ID))
return
}

// StartDate is in milliseconds, so convert it to nanoseconds, add correct number of seconds,
// then convert back to milliseconds.
endDate := time.Unix(0, sdnano).Add(72*time.Hour).UnixNano() / 1000000
endDate := time.Unix(0, sdnano).Add(time.Duration(timeLimitSeconds)*time.Second).UnixNano() / 1000000
if err = setPlannedEndDate(ctx, dedb, analysis.ID, endDate); err != nil {
log.Error(errors.Wrapf(err, "error setting planned end date for analysis '%s' to '%d'", analysis.ID, endDate))
}
Expand Down
Loading