From 01c49d13500be3d33d616f33b57bd0d02d945b28 Mon Sep 17 00:00:00 2001 From: Ian McEwen Date: Thu, 25 Jan 2024 10:37:58 -0700 Subject: [PATCH 1/2] CORE-1977: fetch the time_limit_seconds from the DB for a job, defaulting to 72h, instead of using a hardcoded value --- analyses.go | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/analyses.go b/analyses.go index f42ff41..9bdcd3d 100644 --- a/analyses.go +++ b/analyses.go @@ -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. @@ -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(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)) } From b8ef8b19bec60641680504c5318413bcbac3e1c9 Mon Sep 17 00:00:00 2001 From: Ian McEwen Date: Thu, 25 Jan 2024 10:45:30 -0700 Subject: [PATCH 2/2] Fix type mismatch error --- analyses.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/analyses.go b/analyses.go index 9bdcd3d..beb9e19 100644 --- a/analyses.go +++ b/analyses.go @@ -630,7 +630,7 @@ func CreateMessageHandler(dedb *sql.DB) func(context.Context, amqp.Delivery) { // 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(timeLimitSeconds*time.Second).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)) }