go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/cron/context.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package cron
     9  
    10  import "context"
    11  
    12  type jobManagerKey struct{}
    13  
    14  // WithJobManager adds a job manager to a context.
    15  func WithJobManager(ctx context.Context, jm *JobManager) context.Context {
    16  	return context.WithValue(ctx, jobManagerKey{}, jm)
    17  }
    18  
    19  // GetJobManager gets a JobManager off a context.
    20  func GetJobManager(ctx context.Context) *JobManager {
    21  	if value := ctx.Value(jobManagerKey{}); value != nil {
    22  		if typed, ok := value.(*JobManager); ok {
    23  			return typed
    24  		}
    25  	}
    26  	return nil
    27  }
    28  
    29  type jobSchedulerKey struct{}
    30  
    31  // WithJobScheduler adds a job scheduler to a context.
    32  func WithJobScheduler(ctx context.Context, js *JobScheduler) context.Context {
    33  	return context.WithValue(ctx, jobSchedulerKey{}, js)
    34  }
    35  
    36  // GetJobScheduler gets a JobScheduler off a context.
    37  func GetJobScheduler(ctx context.Context) *JobScheduler {
    38  	if value := ctx.Value(jobSchedulerKey{}); value != nil {
    39  		if typed, ok := value.(*JobScheduler); ok {
    40  			return typed
    41  		}
    42  	}
    43  	return nil
    44  }
    45  
    46  type contextKeyJobParameters struct{}
    47  
    48  // WithJobParameterValues adds job invocation parameter values to a context.
    49  func WithJobParameterValues(ctx context.Context, values JobParameters) context.Context {
    50  	return context.WithValue(ctx, contextKeyJobParameters{}, values)
    51  }
    52  
    53  // GetJobParameterValues gets parameter values from a given context.
    54  func GetJobParameterValues(ctx context.Context) JobParameters {
    55  	if value := ctx.Value(contextKeyJobParameters{}); value != nil {
    56  		if typed, ok := value.(JobParameters); ok {
    57  			return typed
    58  		}
    59  	}
    60  	return nil
    61  }
    62  
    63  // MergeJobParameterValues merges values from many sources.
    64  // The order is important for which value set's keys take precedence.
    65  func MergeJobParameterValues(values ...JobParameters) JobParameters {
    66  	output := make(JobParameters)
    67  	for _, set := range values {
    68  		for key, value := range set {
    69  			output[key] = value
    70  		}
    71  	}
    72  	return output
    73  }
    74  
    75  type contextKeyJobInvocation struct{}
    76  
    77  // WithJobInvocation adds job invocation to a context.
    78  func WithJobInvocation(ctx context.Context, ji *JobInvocation) context.Context {
    79  	return context.WithValue(ctx, contextKeyJobInvocation{}, ji)
    80  }
    81  
    82  // GetJobInvocation gets the job invocation from a given context.
    83  func GetJobInvocation(ctx context.Context) *JobInvocation {
    84  	if value := ctx.Value(contextKeyJobInvocation{}); value != nil {
    85  		if typed, ok := value.(*JobInvocation); ok {
    86  			return typed
    87  		}
    88  	}
    89  	return nil
    90  }