github.com/Axway/agent-sdk@v1.1.101/pkg/jobs/retryjob.go (about)

     1  package jobs
     2  
     3  import (
     4  	"github.com/Axway/agent-sdk/pkg/util/errors"
     5  )
     6  
     7  type retryJobProps struct {
     8  	retries int
     9  }
    10  
    11  type retryJob struct {
    12  	baseJob
    13  	retryJobProps
    14  }
    15  
    16  // newBaseJob - creates a single run job and sets up the structure for different job types
    17  func newRetryJob(newJob Job, retries int, name string, failJobChan chan string) (JobExecution, error) {
    18  	thisJob := retryJob{
    19  		createBaseJob(newJob, failJobChan, name, JobTypeRetry),
    20  		retryJobProps{
    21  			retries: retries,
    22  		},
    23  	}
    24  
    25  	go thisJob.start()
    26  	return &thisJob, nil
    27  }
    28  
    29  // start - calls the Execute function from the Job definition
    30  func (b *retryJob) start() {
    31  	b.startLog()
    32  	b.waitForReady()
    33  
    34  	b.SetStatus(JobStatusRunning)
    35  	for i := 0; i < b.retries; i++ {
    36  		b.executeJob()
    37  		if b.err == nil {
    38  			// job was successful
    39  			b.SetStatus(JobStatusFinished)
    40  			return
    41  		}
    42  		b.setExecutionRetryError()
    43  		b.SetStatus(JobStatusRetrying)
    44  	}
    45  
    46  	b.SetStatus(JobStatusFailed)
    47  }
    48  
    49  // stop - noop
    50  func (b *retryJob) stop() {
    51  	b.stopLog()
    52  }
    53  
    54  func (b *retryJob) setExecutionRetryError() {
    55  	b.err = errors.Wrap(ErrExecutingRetryJob, b.err.Error()).FormatError(b.jobType, b.id, b.retries)
    56  }