github.com/zhongdalu/gf@v1.0.0/g/os/gcron/gcron.go (about)

     1  // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  
     7  // Package gcron implements a cron pattern parser and job runner.
     8  package gcron
     9  
    10  import (
    11  	"github.com/zhongdalu/gf/g/os/gtimer"
    12  	"math"
    13  	"time"
    14  )
    15  
    16  const (
    17  	STATUS_READY   = gtimer.STATUS_READY
    18  	STATUS_RUNNING = gtimer.STATUS_RUNNING
    19  	STATUS_STOPPED = gtimer.STATUS_STOPPED
    20  	STATUS_CLOSED  = gtimer.STATUS_CLOSED
    21  
    22  	gDEFAULT_TIMES = math.MaxInt32
    23  )
    24  
    25  var (
    26  	// Default cron object.
    27  	defaultCron = New()
    28  )
    29  
    30  // SetLogPath sets the logging folder path for default cron object.
    31  func SetLogPath(path string) {
    32  	defaultCron.SetLogPath(path)
    33  }
    34  
    35  // GetLogPath returns the logging folder path of default cron object.
    36  func GetLogPath() string {
    37  	return defaultCron.GetLogPath()
    38  }
    39  
    40  // SetLogLevel sets the logging level for default cron object.
    41  func SetLogLevel(level int) {
    42  	defaultCron.SetLogLevel(level)
    43  }
    44  
    45  // GetLogLevel returns the logging level for default cron object.
    46  func GetLogLevel() int {
    47  	return defaultCron.GetLogLevel()
    48  }
    49  
    50  // Add adds a timed task to default cron object.
    51  // A unique <name> can be bound with the timed task.
    52  // It returns and error if the <name> is already used.
    53  func Add(pattern string, job func(), name ...string) (*Entry, error) {
    54  	return defaultCron.Add(pattern, job, name...)
    55  }
    56  
    57  // AddSingleton adds a singleton timed task, to default cron object.
    58  // A singleton timed task is that can only be running one single instance at the same time.
    59  // A unique <name> can be bound with the timed task.
    60  // It returns and error if the <name> is already used.
    61  func AddSingleton(pattern string, job func(), name ...string) (*Entry, error) {
    62  	return defaultCron.AddSingleton(pattern, job, name...)
    63  }
    64  
    65  // AddOnce adds a timed task which can be run only once, to default cron object.
    66  // A unique <name> can be bound with the timed task.
    67  // It returns and error if the <name> is already used.
    68  func AddOnce(pattern string, job func(), name ...string) (*Entry, error) {
    69  	return defaultCron.AddOnce(pattern, job, name...)
    70  }
    71  
    72  // AddTimes adds a timed task which can be run specified times, to default cron object.
    73  // A unique <name> can be bound with the timed task.
    74  // It returns and error if the <name> is already used.
    75  func AddTimes(pattern string, times int, job func(), name ...string) (*Entry, error) {
    76  	return defaultCron.AddTimes(pattern, times, job, name...)
    77  }
    78  
    79  // DelayAdd adds a timed task to default cron object after <delay> time.
    80  func DelayAdd(delay time.Duration, pattern string, job func(), name ...string) {
    81  	defaultCron.DelayAdd(delay, pattern, job, name...)
    82  }
    83  
    84  // DelayAddSingleton adds a singleton timed task after <delay> time to default cron object.
    85  func DelayAddSingleton(delay time.Duration, pattern string, job func(), name ...string) {
    86  	defaultCron.DelayAddSingleton(delay, pattern, job, name...)
    87  }
    88  
    89  // DelayAddOnce adds a timed task after <delay> time to default cron object.
    90  // This timed task can be run only once.
    91  func DelayAddOnce(delay time.Duration, pattern string, job func(), name ...string) {
    92  	defaultCron.DelayAddOnce(delay, pattern, job, name...)
    93  }
    94  
    95  // DelayAddTimes adds a timed task after <delay> time to default cron object.
    96  // This timed task can be run specified times.
    97  func DelayAddTimes(delay time.Duration, pattern string, times int, job func(), name ...string) {
    98  	defaultCron.DelayAddTimes(delay, pattern, times, job, name...)
    99  }
   100  
   101  // Search returns a scheduled task with the specified <name>.
   102  // It returns nil if no found.
   103  func Search(name string) *Entry {
   104  	return defaultCron.Search(name)
   105  }
   106  
   107  // Remove deletes scheduled task which named <name>.
   108  func Remove(name string) {
   109  	defaultCron.Remove(name)
   110  }
   111  
   112  // Size returns the size of the timed tasks of default cron.
   113  func Size() int {
   114  	return defaultCron.Size()
   115  }
   116  
   117  // Entries return all timed tasks as slice.
   118  func Entries() []*Entry {
   119  	return defaultCron.Entries()
   120  }
   121  
   122  // Start starts running the specified timed task named <name>.
   123  func Start(name string) {
   124  	defaultCron.Start(name)
   125  }
   126  
   127  // Stop stops running the specified timed task named <name>.
   128  func Stop(name string) {
   129  	defaultCron.Stop(name)
   130  }