github.com/kaydxh/golang@v0.0.131/pkg/crontab/crontab.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package crontab
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"sync"
    28  	"time"
    29  
    30  	"github.com/kaydxh/golang/go/errors"
    31  	errors_ "github.com/kaydxh/golang/go/errors"
    32  	time_ "github.com/kaydxh/golang/go/time"
    33  	"github.com/sirupsen/logrus"
    34  	"go.uber.org/atomic"
    35  )
    36  
    37  // Crontab ...
    38  
    39  type CrontabSerivce struct {
    40  	checkInterval time.Duration
    41  	inShutdown    atomic.Bool // true when when server is in shutdown
    42  
    43  	mu     sync.Mutex
    44  	cancel func()
    45  	fs     []func(context.Context, *logrus.Entry) error
    46  }
    47  
    48  // NewCrontab ...
    49  func NewCrontabSerivce(
    50  	checkInterval time.Duration,
    51  ) *CrontabSerivce {
    52  	s := &CrontabSerivce{
    53  		checkInterval: checkInterval,
    54  	}
    55  	return s
    56  }
    57  
    58  // Register ...
    59  func (c *CrontabSerivce) Register(f func(context.Context, *logrus.Entry) error) {
    60  	c.fs = append(c.fs, f)
    61  }
    62  
    63  // Run will initialize the backend. It must not block, but may run go routines in the background.
    64  func (c *CrontabSerivce) Run(ctx context.Context) error {
    65  	logger := c.getLogger()
    66  	logger.Infoln("Crontab Run")
    67  	if c.inShutdown.Load() {
    68  		logger.Infoln("Crontab Shutdown")
    69  		return fmt.Errorf("server closed")
    70  	}
    71  	go func() {
    72  		errors.HandleError(c.Serve(ctx))
    73  	}()
    74  	return nil
    75  }
    76  
    77  // Serve ...
    78  func (c *CrontabSerivce) Serve(ctx context.Context) error {
    79  	logger := c.getLogger()
    80  	logger.Infoln("Crontab Serve")
    81  
    82  	if c.inShutdown.Load() {
    83  		logger.Infoln("Crontab Shutdown")
    84  		return fmt.Errorf("server closed")
    85  	}
    86  
    87  	defer c.inShutdown.Store(true)
    88  	ctx, cancel := context.WithCancel(ctx)
    89  	defer cancel()
    90  	c.mu.Lock()
    91  	c.cancel = cancel
    92  	c.mu.Unlock()
    93  
    94  	time_.UntilWithContxt(ctx, func(ctx context.Context) error {
    95  		err := c.check(ctx)
    96  		if err != nil {
    97  			return err
    98  		}
    99  		return nil
   100  	}, c.checkInterval)
   101  	if err := ctx.Err(); err != nil {
   102  		logger.WithError(err).Errorf("stopped checking")
   103  		return err
   104  	}
   105  	logger.Info("stopped checking")
   106  	return nil
   107  }
   108  
   109  // Shutdown ...
   110  func (c *CrontabSerivce) Shutdown() {
   111  	c.inShutdown.Store(true)
   112  	c.mu.Lock()
   113  	defer c.mu.Unlock()
   114  	if c.cancel != nil {
   115  		c.cancel()
   116  	}
   117  }
   118  
   119  func (c *CrontabSerivce) getLogger() *logrus.Entry {
   120  	return logrus.WithField("module", "Crontab")
   121  }
   122  
   123  func (c *CrontabSerivce) check(ctx context.Context) error {
   124  	var (
   125  		wg   sync.WaitGroup
   126  		errs []error
   127  	)
   128  	logger := c.getLogger()
   129  	c.mu.Lock()
   130  	fs := c.fs
   131  	c.mu.Unlock()
   132  
   133  	for _, f := range fs {
   134  		wg.Add(1)
   135  		go func(doFunc func(context.Context, *logrus.Entry) error) {
   136  			defer wg.Done()
   137  			err := doFunc(ctx, logger)
   138  			if err != nil {
   139  				c.mu.Lock()
   140  				errs = append(errs, err)
   141  				c.mu.Unlock()
   142  			}
   143  		}(f)
   144  	}
   145  	wg.Wait()
   146  	return errors_.NewAggregate(errs)
   147  }
   148  
   149  /*
   150  type CronProcessor struct {
   151  	cronRunner *cron.Cron
   152  }
   153  
   154  type Job interface {
   155  	Run()
   156  }
   157  
   158  func NewCronProcessor(specTime string, job Job) (*CronProcessor, error) {
   159  	cp := &CronProcessor{
   160  		cronRunner: cron.New(),
   161  	}
   162  
   163  	if cp.cronRunner == nil {
   164  		return nil, fmt.Errorf("failed to init cronRunner")
   165  	}
   166  
   167  	if specTime == "" {
   168  		return nil, fmt.Errorf("specTime is empty")
   169  	}
   170  
   171  	err := cp.cronRunner.AddJob(specTime, job)
   172  	if err != nil {
   173  		return nil, fmt.Errorf("failed to odd job in specTime: [%v]", specTime)
   174  	}
   175  
   176  	cp.cronRunner.Start()
   177  
   178  	return cp, nil
   179  }
   180  
   181  func (c *CronProcessor) Stop() {
   182  	c.cronRunner.Stop()
   183  }
   184  */