mitchell852 commented on a change in pull request #5044:
URL: https://github.com/apache/trafficcontrol/pull/5044#discussion_r497130175



##########
File path: lib/go-tc/invalidationjobs.go
##########
@@ -272,9 +275,83 @@ func (job *InvalidationJobInput) Validate(tx *sql.Tx) 
error {
        if len(errs) > 0 {
                return errors.New(strings.Join(errs, ", "))
        }
+
+       errs = job.ValidateUniqueness(tx, dsID)
+       if len(errs) > 0 {
+               return errors.New(strings.Join(errs, ", "))
+       }
+
        return nil
 }
 
+func (job *InvalidationJobInput) ValidateUniqueness(tx *sql.Tx, dsID uint) 
[]string {
+       errors := []string{}
+
+       const readQuery = `
+SELECT job.id,
+       keyword,
+       parameters,
+       asset_url,
+       start_time
+FROM job
+WHERE job.job_deliveryservice = $1
+`
+       rows, err := tx.Query(readQuery, dsID)
+       if err != nil {
+               errors = append(errors, fmt.Sprintf("unable to query for other 
invalidation jobs"))
+               return errors
+       }
+
+       defer rows.Close()
+       jobStart := job.StartTime.Time
+       for rows.Next() {
+               testJob := InvalidationJob{}
+               err = rows.Scan(&testJob.ID, &testJob.Keyword, 
&testJob.Parameters, &testJob.AssetURL, &testJob.StartTime)
+               if err != nil {
+                       continue
+               }
+               if !strings.HasSuffix(*testJob.AssetURL, *job.Regex) {
+                       continue
+               }
+               testJobTTL := testJob.GetTTL()
+               if testJobTTL == 0 {
+                       continue
+               }
+               testJobStart := testJob.StartTime.Time
+               testJobEnd := testJobStart.Add(time.Hour * 
time.Duration(testJobTTL))
+               jobTTLHours, _ := job.TTLHours()
+               jobEnd := jobStart.Add(time.Hour * time.Duration(jobTTLHours))
+               // jobStart in testJob range
+               if (testJobStart.Before(jobStart) && 
jobStart.Before(testJobEnd)) ||
+                       // jobEnd in testJob range
+                       (testJobStart.Before(jobEnd) && 
jobEnd.Before(testJobEnd)) ||
+                       // job range encaspulates testJob range
+                       (testJobEnd.Before(jobEnd) && 
jobStart.Before(jobStart)) {
+                       errors = append(errors, fmt.Sprintf("job already has an 
invalidation job that overlaps, start:%v end:%v", testJobStart, testJobEnd))

Review comment:
       the only type of job is a "content invalidation request" job, so how 
about a message like this:
   
   "Unable to create content invalidation request as an active one already 
exists for [assetUrl]: start:[date] end:[date]"




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to