github.com/blend/go-sdk@v1.20220411.3/examples/cron/basic/main.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 main
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  	"log"
    14  	"time"
    15  
    16  	"net/http"
    17  	_ "net/http/pprof"
    18  
    19  	"github.com/blend/go-sdk/cron"
    20  	"github.com/blend/go-sdk/graceful"
    21  	"github.com/blend/go-sdk/logger"
    22  )
    23  
    24  // Variables
    25  var (
    26  	N = 1024
    27  )
    28  
    29  func main() {
    30  	go func() {
    31  		log.Println(http.ListenAndServe("localhost:6060", nil))
    32  	}()
    33  
    34  	log := logger.All()
    35  	jm := cron.New(cron.OptLog(log))
    36  
    37  	for x := 0; x < N; x++ {
    38  		jm.LoadJobs(
    39  			cron.NewJob(
    40  				cron.OptJobName(fmt.Sprintf("load-test-%d", x)),
    41  				cron.OptJobSchedule(cron.EverySecond()),
    42  				cron.OptJobAction(func(ctx context.Context) error {
    43  					select {
    44  					case <-ctx.Done():
    45  						return context.Canceled
    46  					case <-time.After(500 * time.Millisecond):
    47  						return nil
    48  					}
    49  				}),
    50  			),
    51  		)
    52  	}
    53  	if err := graceful.Shutdown(jm); err != nil {
    54  		log.Fatal(err)
    55  	}
    56  }