@@ -211,7 +211,7 @@ func (r *RuntimeScheduler) scheduleRoutine(ctx context.Context) {
211211 r .pathChecksumMap [checksumPath .path ] = checksumPath .checksum
212212 case <- time .After (r .config .ScheduleTime ):
213213 if err := r .jobMaintenance (ctx ); err != nil {
214- log .Errorf ("Errorf on job maintenance %s" , err )
214+ log .Errorf ("Error on job maintenance %s" , err )
215215 }
216216
217217 }
@@ -280,65 +280,63 @@ type ScheduleJobRequestResult struct {
280280}
281281
282282func (r * RuntimeScheduler ) scheduleJobRequest (ctx context.Context , jobRequest * model.JobRequest ) (job * model.Job , err error ) {
283+ l := log .WithFields (log.Fields {
284+ "source_path" : jobRequest .SourcePath ,
285+ })
283286 err = r .repo .WithTransaction (ctx , func (ctx context.Context , tx repository.Repository ) error {
284287 job , err = tx .GetJobByPath (ctx , jobRequest .SourcePath )
285288 if err != nil {
286289 return err
287290 }
288291
289- l := log .WithFields (log.Fields {
290- "source_path" : jobRequest .SourcePath ,
291- })
292-
293292 var eventsToAdd []* model.TaskEvent
294293 if job == nil {
295- newUUID , _ := uuid .NewUUID ()
296- job = & model.Job {
297- SourcePath : jobRequest .SourcePath ,
298- SourceSize : jobRequest .SourceSize ,
299- TargetPath : jobRequest .TargetPath ,
300- Id : newUUID ,
301- }
302- l .WithField ("job_id" , job .Id .String ()).Info ("Creating new job" )
303- err = tx .AddJob (ctx , job )
294+ job , err = r .newJob (ctx , tx , jobRequest )
304295 if err != nil {
305296 return err
306297 }
307- startEvent := job .AddEvent (model .NotificationEvent , model .JobNotification , model .QueuedNotificationStatus )
308- eventsToAdd = append (eventsToAdd , startEvent )
298+ eventsToAdd = job .Events
309299 } else {
310300 // If job exist we check if we can retry the job
311- lastEvent := job .Events .GetLatestPerNotificationType (model .JobNotification )
312- status := job .Events .GetStatus ()
313- if jobRequest .ForceAssigned && (status == model .AssignedNotificationStatus || status == model .StartedNotificationStatus ) {
314- cancelEvent := job .AddEvent (model .NotificationEvent , model .JobNotification , model .CanceledNotificationStatus )
315- eventsToAdd = append (eventsToAdd , cancelEvent )
316-
317- }
318- if (jobRequest .ForceCompleted && status == model .CompletedNotificationStatus ) ||
319- (jobRequest .ForceFailed && (status == model .FailedNotificationStatus || status == model .CanceledNotificationStatus )) ||
320- (jobRequest .ForceAssigned && (status == model .StartedNotificationStatus || status == model .AssignedNotificationStatus )) {
321- requeueEvent := job .AddEvent (model .NotificationEvent , model .JobNotification , model .QueuedNotificationStatus )
322- eventsToAdd = append (eventsToAdd , requeueEvent )
323- } else if ! (jobRequest .ForceAssigned && status == model .QueuedNotificationStatus ) {
324- return fmt .Errorf ("%s (%s) job is in %s state by %s, can not be rescheduled" , job .Id .String (), jobRequest .SourcePath , lastEvent .Status , lastEvent .WorkerName )
301+ eventsToAdd , err = r .updateTerminatedJobByRequest (job , jobRequest )
302+ if err != nil {
303+ return err
325304 }
326305 }
327- if len (eventsToAdd ) > 0 {
328- for _ , taskEvent := range eventsToAdd {
329- err = tx .AddNewTaskEvent (ctx , taskEvent )
330- if err != nil {
331- return err
332- }
333- l .WithField ("job_id" , job .Id .String ()).Infof ("job is now %s" , taskEvent .Status )
306+
307+ for _ , taskEvent := range eventsToAdd {
308+ err = tx .AddNewTaskEvent (ctx , taskEvent )
309+ if err != nil {
310+ return err
334311 }
312+ l .WithField ("job_id" , job .Id .String ()).Infof ("job is now %s" , taskEvent .Status )
335313 }
336314
337315 return nil
338316 })
339317 return job , err
340318}
341319
320+ func (r * RuntimeScheduler ) newJob (ctx context.Context , tx repository.Repository , jobRequest * model.JobRequest ) (* model.Job , error ) {
321+ l := log .WithFields (log.Fields {
322+ "source_path" : jobRequest .SourcePath ,
323+ })
324+ newUUID , _ := uuid .NewUUID ()
325+ job := & model.Job {
326+ SourcePath : jobRequest .SourcePath ,
327+ SourceSize : jobRequest .SourceSize ,
328+ TargetPath : jobRequest .TargetPath ,
329+ Id : newUUID ,
330+ }
331+ l .WithField ("job_id" , job .Id .String ()).Info ("Creating new job" )
332+ err := tx .AddJob (ctx , job )
333+ if err != nil {
334+ return nil , err
335+ }
336+ job .AddEvent (model .NotificationEvent , model .JobNotification , model .QueuedNotificationStatus )
337+ return job , nil
338+ }
339+
342340func (r * RuntimeScheduler ) ScheduleJobRequests (ctx context.Context , jobRequest * model.JobRequest ) (result * ScheduleJobRequestResult , returnError error ) {
343341 result = & ScheduleJobRequestResult {}
344342 searchJobRequestChan := make (chan * JobRequestResult , 10 )
@@ -546,6 +544,27 @@ func (r *RuntimeScheduler) assignedJobMaintenance(ctx context.Context) error {
546544 return nil
547545}
548546
547+ func (r * RuntimeScheduler ) updateTerminatedJobByRequest (job * model.Job , jobRequest * model.JobRequest ) ([]* model.TaskEvent , error ) {
548+ var eventsToAdd []* model.TaskEvent
549+ lastEvent := job .Events .GetLatestPerNotificationType (model .JobNotification )
550+ status := lastEvent .Status
551+
552+ switch {
553+ case jobRequest .ForceAssigned && (status == model .AssignedNotificationStatus || status == model .StartedNotificationStatus ):
554+ eventsToAdd = append (eventsToAdd , job .AddEvent (model .NotificationEvent , model .JobNotification , model .CanceledNotificationStatus ))
555+ eventsToAdd = append (eventsToAdd , job .AddEvent (model .NotificationEvent , model .JobNotification , model .QueuedNotificationStatus ))
556+
557+ case jobRequest .ForceCompleted && status == model .CompletedNotificationStatus ,
558+ jobRequest .ForceFailed && status == model .FailedNotificationStatus ,
559+ jobRequest .ForceCanceled && status == model .CanceledNotificationStatus :
560+ requeueEvent := job .AddEvent (model .NotificationEvent , model .JobNotification , model .QueuedNotificationStatus )
561+ eventsToAdd = append (eventsToAdd , requeueEvent )
562+ default :
563+ return nil , fmt .Errorf ("%s (%s) job is in %s state by %s, can not be rescheduled" , job .Id .String (), jobRequest .SourcePath , lastEvent .Status , lastEvent .WorkerName )
564+ }
565+ return eventsToAdd , nil
566+ }
567+
549568func simpleRegex (pattern string , string string ) bool {
550569 m , err := regexp .MatchString (strings .ToLower (pattern ), strings .ToLower (string ))
551570 if err != nil {
0 commit comments