github.com/blend/go-sdk@v1.20220411.3/cron/job_builder.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package cron
     9  
    10  import (
    11  	"context"
    12  	"time"
    13  
    14  	"github.com/blend/go-sdk/ref"
    15  	"github.com/blend/go-sdk/stringutil"
    16  )
    17  
    18  // Interface assertions.
    19  var (
    20  	_ Job                = (*JobBuilder)(nil)
    21  	_ ScheduleProvider   = (*JobBuilder)(nil)
    22  	_ LifecycleProvider  = (*JobBuilder)(nil)
    23  	_ BackgroundProvider = (*JobBuilder)(nil)
    24  	_ ConfigProvider     = (*JobBuilder)(nil)
    25  )
    26  
    27  // NewJob returns a new job builder.
    28  func NewJob(options ...JobBuilderOption) *JobBuilder {
    29  	var jb JobBuilder
    30  	for _, option := range options {
    31  		option(&jb)
    32  	}
    33  	if jb.JobName == "" {
    34  		jb.JobName = stringutil.Random(stringutil.LowerLetters, 16)
    35  	}
    36  	return &jb
    37  }
    38  
    39  // JobBuilderOption is a job builder option.
    40  type JobBuilderOption func(*JobBuilder)
    41  
    42  // OptJobName sets the job name.
    43  func OptJobName(name string) JobBuilderOption {
    44  	return func(jb *JobBuilder) { jb.JobName = name }
    45  }
    46  
    47  // OptJobAction sets the job action.
    48  func OptJobAction(action func(context.Context) error) JobBuilderOption {
    49  	return func(jb *JobBuilder) { jb.JobAction = action }
    50  }
    51  
    52  // OptJobConfig sets the job config.
    53  func OptJobConfig(cfg JobConfig) JobBuilderOption {
    54  	return func(jb *JobBuilder) { jb.JobConfig = cfg }
    55  }
    56  
    57  // OptJobLabels is a job builder sets the job labels.
    58  func OptJobLabels(labels map[string]string) JobBuilderOption {
    59  	return func(jb *JobBuilder) { jb.JobConfig.Labels = labels }
    60  }
    61  
    62  // OptJobSchedule is a job builder sets the job schedule provder.
    63  func OptJobSchedule(schedule Schedule) JobBuilderOption {
    64  	return func(jb *JobBuilder) { jb.JobScheduleProvider = func() Schedule { return schedule } }
    65  }
    66  
    67  // OptJobTimeout is a job builder sets the job timeout provder.
    68  func OptJobTimeout(d time.Duration) JobBuilderOption {
    69  	return func(jb *JobBuilder) { jb.JobConfig.Timeout = d }
    70  }
    71  
    72  // OptJobShutdownGracePeriod is a job builder sets the job shutdown grace period provder.
    73  func OptJobShutdownGracePeriod(d time.Duration) JobBuilderOption {
    74  	return func(jb *JobBuilder) { jb.JobConfig.ShutdownGracePeriod = d }
    75  }
    76  
    77  // OptJobDisabled is a job builder sets the job timeout provder.
    78  func OptJobDisabled(disabled bool) JobBuilderOption {
    79  	return func(jb *JobBuilder) { jb.JobConfig.Disabled = ref.Bool(disabled) }
    80  }
    81  
    82  // OptJobOnBegin sets a lifecycle hook.
    83  func OptJobOnBegin(handler func(context.Context)) JobBuilderOption {
    84  	return func(jb *JobBuilder) { jb.JobLifecycle.OnBegin = handler }
    85  }
    86  
    87  // OptJobOnComplete sets a lifecycle hook.
    88  func OptJobOnComplete(handler func(context.Context)) JobBuilderOption {
    89  	return func(jb *JobBuilder) { jb.JobLifecycle.OnComplete = handler }
    90  }
    91  
    92  // OptJobOnError sets a lifecycle hook.
    93  func OptJobOnError(handler func(context.Context)) JobBuilderOption {
    94  	return func(jb *JobBuilder) { jb.JobLifecycle.OnError = handler }
    95  }
    96  
    97  // OptJobOnCancellation sets the on cancellation lifecycle hook.
    98  func OptJobOnCancellation(handler func(context.Context)) JobBuilderOption {
    99  	return func(jb *JobBuilder) { jb.JobLifecycle.OnCancellation = handler }
   100  }
   101  
   102  // OptJobOnSuccess sets a lifecycle hook.
   103  func OptJobOnSuccess(handler func(context.Context)) JobBuilderOption {
   104  	return func(jb *JobBuilder) { jb.JobLifecycle.OnSuccess = handler }
   105  }
   106  
   107  // OptJobOnBroken sets a lifecycle hook.
   108  func OptJobOnBroken(handler func(context.Context)) JobBuilderOption {
   109  	return func(jb *JobBuilder) { jb.JobLifecycle.OnBroken = handler }
   110  }
   111  
   112  // OptJobOnFixed sets a lifecycle hook.
   113  func OptJobOnFixed(handler func(context.Context)) JobBuilderOption {
   114  	return func(jb *JobBuilder) { jb.JobLifecycle.OnFixed = handler }
   115  }
   116  
   117  // OptJobOnEnabled sets a lifecycle hook.
   118  func OptJobOnEnabled(handler func(context.Context)) JobBuilderOption {
   119  	return func(jb *JobBuilder) { jb.JobLifecycle.OnEnabled = handler }
   120  }
   121  
   122  // OptJobOnDisabled sets a lifecycle hook.
   123  func OptJobOnDisabled(handler func(context.Context)) JobBuilderOption {
   124  	return func(jb *JobBuilder) { jb.JobLifecycle.OnDisabled = handler }
   125  }
   126  
   127  // OptJobOnLoad sets a lifecycle hook.
   128  func OptJobOnLoad(handler func(context.Context) error) JobBuilderOption {
   129  	return func(jb *JobBuilder) { jb.JobLifecycle.OnLoad = handler }
   130  }
   131  
   132  // OptJobOnUnload sets a lifecycle hook.
   133  func OptJobOnUnload(handler func(context.Context) error) JobBuilderOption {
   134  	return func(jb *JobBuilder) { jb.JobLifecycle.OnUnload = handler }
   135  }
   136  
   137  // OptJobBackground sets the background provider.
   138  func OptJobBackground(provider func(context.Context) context.Context) JobBuilderOption {
   139  	return func(jb *JobBuilder) { jb.BackgroundProvider = provider }
   140  }
   141  
   142  // JobBuilder allows for job creation w/o a fully formed struct.
   143  type JobBuilder struct {
   144  	JobName             string
   145  	JobConfig           JobConfig
   146  	JobLifecycle        JobLifecycle
   147  	JobAction           Action
   148  	JobScheduleProvider func() Schedule
   149  	BackgroundProvider  func(context.Context) context.Context
   150  }
   151  
   152  // Name returns the job name.
   153  func (jb *JobBuilder) Name() string {
   154  	return jb.JobName
   155  }
   156  
   157  // Background implements BackgroundProvider.
   158  func (jb *JobBuilder) Background(ctx context.Context) context.Context {
   159  	if jb.BackgroundProvider != nil {
   160  		return jb.BackgroundProvider(ctx)
   161  	}
   162  	return ctx
   163  }
   164  
   165  // Schedule returns the job schedule if a provider is set.
   166  func (jb *JobBuilder) Schedule() Schedule {
   167  	if jb.JobScheduleProvider != nil {
   168  		return jb.JobScheduleProvider()
   169  	}
   170  	return nil
   171  }
   172  
   173  // Config returns the job config.
   174  func (jb *JobBuilder) Config() JobConfig {
   175  	return jb.JobConfig
   176  }
   177  
   178  // Lifecycle returns the job lifecycle hooks.
   179  func (jb *JobBuilder) Lifecycle() JobLifecycle {
   180  	return jb.JobLifecycle
   181  }
   182  
   183  // Execute runs the job action if it's set.
   184  func (jb *JobBuilder) Execute(ctx context.Context) error {
   185  	if jb.JobAction != nil {
   186  		return jb.JobAction(ctx)
   187  	}
   188  	return nil
   189  }