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

     1  package jobs
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  )
     7  
     8  // globalPool - the default job pool
     9  var globalPool *Pool
    10  var executionTimeLimit time.Duration = 5 * time.Minute
    11  var statusCheckInterval time.Duration = defaultRetryInterval
    12  var durationsMutex sync.Mutex = sync.Mutex{}
    13  
    14  func init() {
    15  	globalPool = newPool()
    16  }
    17  
    18  // UpdateDurations - updates settings int he jobs library
    19  func UpdateDurations(retryInterval time.Duration, executionTimeout time.Duration) {
    20  	durationsMutex.Lock()
    21  	defer durationsMutex.Unlock()
    22  	executionTimeLimit = executionTimeout
    23  	globalPool.setBackoff(newBackoffTimeout(retryInterval, 10*time.Minute, 2))
    24  	statusCheckInterval = retryInterval
    25  }
    26  
    27  // getStatusCheckInterval - get the status interval using a mutex
    28  func getStatusCheckInterval() time.Duration {
    29  	durationsMutex.Lock()
    30  	defer durationsMutex.Unlock()
    31  	return statusCheckInterval
    32  }
    33  
    34  // RegisterSingleRunJob - Runs a single run job in the globalPool
    35  func RegisterSingleRunJob(newJob Job) (string, error) {
    36  	return globalPool.RegisterSingleRunJob(newJob)
    37  }
    38  
    39  // RegisterSingleRunJobWithName - Runs a single run job in the globalPool
    40  func RegisterSingleRunJobWithName(newJob Job, name string) (string, error) {
    41  	return globalPool.RegisterSingleRunJobWithName(newJob, name)
    42  }
    43  
    44  // RegisterIntervalJob - Runs a job with a specific interval between each run in the globalPool
    45  func RegisterIntervalJob(newJob Job, interval time.Duration, opts ...jobOpt) (string, error) {
    46  	return globalPool.RegisterIntervalJob(newJob, interval, opts...)
    47  }
    48  
    49  // RegisterIntervalJobWithName - Runs a job with a specific interval between each run in the globalPool
    50  func RegisterIntervalJobWithName(newJob Job, interval time.Duration, name string, opts ...jobOpt) (string, error) {
    51  	return globalPool.RegisterIntervalJobWithName(newJob, interval, name, opts...)
    52  }
    53  
    54  // RegisterChannelJob - Runs a job with a specific interval between each run in the globalPool
    55  func RegisterChannelJob(newJob Job, stopChan chan interface{}) (string, error) {
    56  	return globalPool.RegisterChannelJob(newJob, stopChan)
    57  }
    58  
    59  // RegisterChannelJobWithName - Runs a job with a specific interval between each run in the globalPool
    60  func RegisterChannelJobWithName(newJob Job, stopChan chan interface{}, name string) (string, error) {
    61  	return globalPool.RegisterChannelJobWithName(newJob, stopChan, name)
    62  }
    63  
    64  // RegisterDetachedChannelJob -  Runs a job with a stop channel, detached from other jobs in the globalPool
    65  func RegisterDetachedChannelJob(newJob Job, stopChan chan interface{}) (string, error) {
    66  	return globalPool.RegisterDetachedChannelJob(newJob, stopChan)
    67  }
    68  
    69  // RegisterDetachedChannelJobWithName - Runs a named job with a stop channel, detached from other jobs in the globalPool
    70  func RegisterDetachedChannelJobWithName(newJob Job, stopChan chan interface{}, name string) (string, error) {
    71  	return globalPool.RegisterDetachedChannelJobWithName(newJob, stopChan, name)
    72  }
    73  
    74  // RegisterDetachedIntervalJob - Runs a job with a specific interval between each run in the globalPool, detached from other jobs to always run
    75  func RegisterDetachedIntervalJob(newJob Job, interval time.Duration) (string, error) {
    76  	return globalPool.RegisterDetachedIntervalJob(newJob, interval)
    77  }
    78  
    79  // RegisterDetachedIntervalJobWithName - Runs a job with a specific interval between each run in the globalPool, detached from other jobs to always run
    80  func RegisterDetachedIntervalJobWithName(newJob Job, interval time.Duration, name string) (string, error) {
    81  	return globalPool.RegisterDetachedIntervalJobWithName(newJob, interval, name)
    82  }
    83  
    84  // RegisterScheduledJob - Runs a job on a specific schedule in the globalPool
    85  func RegisterScheduledJob(newJob Job, schedule string, opts ...jobOpt) (string, error) {
    86  	return globalPool.RegisterScheduledJob(newJob, schedule, opts...)
    87  }
    88  
    89  // RegisterScheduledJobWithName - Runs a job on a specific schedule in the globalPool
    90  func RegisterScheduledJobWithName(newJob Job, schedule, name string, opts ...jobOpt) (string, error) {
    91  	return globalPool.RegisterScheduledJobWithName(newJob, schedule, name, opts...)
    92  }
    93  
    94  // RegisterRetryJob - Runs a job with a WithName
    95  func RegisterRetryJob(newJob Job, retries int) (string, error) {
    96  	return globalPool.RegisterRetryJob(newJob, retries)
    97  }
    98  
    99  // RegisterRetryJobWithName - Runs a job with a limited number of retries in the globalPool
   100  func RegisterRetryJobWithName(newJob Job, retries int, name string) (string, error) {
   101  	return globalPool.RegisterRetryJobWithName(newJob, retries, name)
   102  }
   103  
   104  // UnregisterJob - Removes the specified job in the globalPool
   105  func UnregisterJob(jobID string) {
   106  	globalPool.UnregisterJob(jobID)
   107  }
   108  
   109  // JobLock - Locks the job, returns when the lock is granted
   110  func JobLock(id string) {
   111  	globalPool.JobLock(id)
   112  }
   113  
   114  // JobUnlock - Unlocks the job
   115  func JobUnlock(id string) {
   116  	globalPool.JobUnlock(id)
   117  }
   118  
   119  // GetStatus - Returns the status from the globalPool
   120  func GetStatus() string {
   121  	return globalPool.GetStatus()
   122  }
   123  
   124  // GetJob - Returns the Job based on the id from the globalPool
   125  func GetJob(id string) JobExecution {
   126  	return globalPool.GetJob(id)
   127  }
   128  
   129  // GetJobStatus - Returns the Status of the Job based on the id in the globalPool
   130  func GetJobStatus(id string) string {
   131  	return globalPool.GetJobStatus(id)
   132  }