github.com/gogf/gf/v2@v2.7.4/os/gcron/gcron.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // Package gcron implements a cron pattern parser and job runner.
     8  package gcron
     9  
    10  import (
    11  	"context"
    12  	"time"
    13  
    14  	"github.com/gogf/gf/v2/os/glog"
    15  	"github.com/gogf/gf/v2/os/gtimer"
    16  )
    17  
    18  const (
    19  	StatusReady   = gtimer.StatusReady
    20  	StatusRunning = gtimer.StatusRunning
    21  	StatusStopped = gtimer.StatusStopped
    22  	StatusClosed  = gtimer.StatusClosed
    23  )
    24  
    25  var (
    26  	// Default cron object.
    27  	defaultCron = New()
    28  )
    29  
    30  // SetLogger sets the global logger for cron.
    31  func SetLogger(logger glog.ILogger) {
    32  	defaultCron.SetLogger(logger)
    33  }
    34  
    35  // GetLogger returns the global logger in the cron.
    36  func GetLogger() glog.ILogger {
    37  	return defaultCron.GetLogger()
    38  }
    39  
    40  // Add adds a timed task to default cron object.
    41  // A unique `name` can be bound with the timed task.
    42  // It returns and error if the `name` is already used.
    43  func Add(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error) {
    44  	return defaultCron.Add(ctx, pattern, job, name...)
    45  }
    46  
    47  // AddSingleton adds a singleton timed task, to default cron object.
    48  // A singleton timed task is that can only be running one single instance at the same time.
    49  // A unique `name` can be bound with the timed task.
    50  // It returns and error if the `name` is already used.
    51  func AddSingleton(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error) {
    52  	return defaultCron.AddSingleton(ctx, pattern, job, name...)
    53  }
    54  
    55  // AddOnce adds a timed task which can be run only once, to default cron object.
    56  // A unique `name` can be bound with the timed task.
    57  // It returns and error if the `name` is already used.
    58  func AddOnce(ctx context.Context, pattern string, job JobFunc, name ...string) (*Entry, error) {
    59  	return defaultCron.AddOnce(ctx, pattern, job, name...)
    60  }
    61  
    62  // AddTimes adds a timed task which can be run specified times, to default cron object.
    63  // A unique `name` can be bound with the timed task.
    64  // It returns and error if the `name` is already used.
    65  func AddTimes(ctx context.Context, pattern string, times int, job JobFunc, name ...string) (*Entry, error) {
    66  	return defaultCron.AddTimes(ctx, pattern, times, job, name...)
    67  }
    68  
    69  // DelayAdd adds a timed task to default cron object after `delay` time.
    70  func DelayAdd(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string) {
    71  	defaultCron.DelayAdd(ctx, delay, pattern, job, name...)
    72  }
    73  
    74  // DelayAddSingleton adds a singleton timed task after `delay` time to default cron object.
    75  func DelayAddSingleton(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string) {
    76  	defaultCron.DelayAddSingleton(ctx, delay, pattern, job, name...)
    77  }
    78  
    79  // DelayAddOnce adds a timed task after `delay` time to default cron object.
    80  // This timed task can be run only once.
    81  func DelayAddOnce(ctx context.Context, delay time.Duration, pattern string, job JobFunc, name ...string) {
    82  	defaultCron.DelayAddOnce(ctx, delay, pattern, job, name...)
    83  }
    84  
    85  // DelayAddTimes adds a timed task after `delay` time to default cron object.
    86  // This timed task can be run specified times.
    87  func DelayAddTimes(ctx context.Context, delay time.Duration, pattern string, times int, job JobFunc, name ...string) {
    88  	defaultCron.DelayAddTimes(ctx, delay, pattern, times, job, name...)
    89  }
    90  
    91  // Search returns a scheduled task with the specified `name`.
    92  // It returns nil if no found.
    93  func Search(name string) *Entry {
    94  	return defaultCron.Search(name)
    95  }
    96  
    97  // Remove deletes scheduled task which named `name`.
    98  func Remove(name string) {
    99  	defaultCron.Remove(name)
   100  }
   101  
   102  // Size returns the size of the timed tasks of default cron.
   103  func Size() int {
   104  	return defaultCron.Size()
   105  }
   106  
   107  // Entries return all timed tasks as slice.
   108  func Entries() []*Entry {
   109  	return defaultCron.Entries()
   110  }
   111  
   112  // Start starts running the specified timed task named `name`.
   113  // If no`name` specified, it starts the entire cron.
   114  func Start(name ...string) {
   115  	defaultCron.Start(name...)
   116  }
   117  
   118  // Stop stops running the specified timed task named `name`.
   119  // If no`name` specified, it stops the entire cron.
   120  func Stop(name ...string) {
   121  	defaultCron.Stop(name...)
   122  }
   123  
   124  // StopGracefully Blocks and waits all current running jobs done.
   125  func StopGracefully() {
   126  	defaultCron.StopGracefully()
   127  }