github.com/Axway/agent-sdk@v1.1.101/pkg/jobs/channeljob.go (about) 1 package jobs 2 3 type channelJobProps struct { 4 signalStop chan interface{} 5 stopChan chan bool 6 } 7 8 type channelJob struct { 9 baseJob 10 channelJobProps 11 } 12 13 // newDetachedChannelJob - creates a channel job, detached from other cron jobs 14 func newDetachedChannelJob(newJob Job, signalStop chan interface{}, name string, failJobChan chan string) (JobExecution, error) { 15 thisJob := channelJob{ 16 createBaseJob(newJob, failJobChan, name, JobTypeDetachedChannel), 17 channelJobProps{ 18 signalStop: signalStop, 19 stopChan: make(chan bool, 1), 20 }, 21 } 22 23 go thisJob.start() 24 return &thisJob, nil 25 } 26 27 // newChannelJob - creates a channel run job 28 func newChannelJob(newJob Job, signalStop chan interface{}, name string, failJobChan chan string) (JobExecution, error) { 29 thisJob := channelJob{ 30 createBaseJob(newJob, failJobChan, name, JobTypeChannel), 31 channelJobProps{ 32 signalStop: signalStop, 33 stopChan: make(chan bool, 1), 34 }, 35 } 36 37 go thisJob.start() 38 return &thisJob, nil 39 } 40 41 func (b *channelJob) handleExecution() { 42 // Execute the job 43 b.setError(b.job.Execute()) 44 if b.getError() != nil { 45 b.setExecutionError() 46 b.baseJob.logger.Error(b.err) 47 b.stop() // stop the job on error 48 b.consecutiveFails++ 49 } 50 b.setConsecutiveFails(0) 51 } 52 53 // start - calls the Execute function from the Job definition 54 func (b *channelJob) start() { 55 b.startLog() 56 b.waitForReady() 57 58 // This could happen while rescheduling the job, pool tries to start 59 // and one of the job fails which triggers stop setting the flag to not ready 60 // Return in this case to allow pool to reschedule the job 61 if !b.IsReady() { 62 return 63 } 64 65 go b.handleExecution() // start a single execution in a go routine as it runs forever 66 b.SetStatus(JobStatusRunning) 67 b.setIsStopped(false) 68 69 // Wait for a write on the stop channel 70 <-b.stopChan 71 b.signalStop <- nil // signal the execution to stop 72 b.SetStatus(JobStatusStopped) 73 } 74 75 // stop - write to the stop channel to stop the execution loop 76 func (b *channelJob) stop() { 77 if b.getIsStopped() { 78 b.logger.Tracef("job has already been stopped") 79 return 80 } 81 b.stopLog() 82 if b.IsReady() { 83 b.logger.Tracef("writing to %s stop channel", b.GetName()) 84 b.stopChan <- true 85 b.logger.Tracef("wrote to %s stop channel", b.GetName()) 86 b.UnsetIsReady() 87 } else { 88 b.stopReadyIfWaiting(0) 89 } 90 b.setIsStopped(true) 91 }