feat: added option to disable cron jobs

This commit is contained in:
divyam234 2024-07-01 17:11:55 +05:30
parent f33b0428e7
commit 1353ae7bd2
4 changed files with 23 additions and 8 deletions

View file

@ -27,6 +27,7 @@ import (
"github.com/gin-contrib/pprof"
ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"github.com/go-co-op/gocron"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
@ -54,6 +55,8 @@ func NewRun() *cobra.Command {
duration.DurationVar(runCmd.Flags(), &config.Server.GracefulShutdown, "server-graceful-shutdown", 15*time.Second, "Server graceful shutdown timeout")
runCmd.Flags().BoolVar(&config.Server.EnablePprof, "server-enable-pprof", false, "Enable Pprof Profiling")
runCmd.Flags().BoolVar(&config.CronJobs.Enable, "cronjobs-enable", true, "Run cron jobs")
runCmd.Flags().IntVarP(&config.Log.Level, "log-level", "", -1, "Logging level")
runCmd.Flags().StringVar(&config.Log.File, "log-file", "", "Logging file path")
runCmd.Flags().BoolVar(&config.Log.Development, "log-development", false, "Enable development mode")
@ -120,8 +123,11 @@ func runApplication(conf *config.Config) {
cancel()
}()
scheduler := gocron.NewScheduler(time.UTC)
app := fx.New(
fx.Supply(conf),
fx.Supply(scheduler),
fx.Supply(logging.DefaultLogger().Desugar()),
fx.NopLogger,
fx.StopTimeout(conf.Server.GracefulShutdown+time.Second),

View file

@ -8,6 +8,9 @@
max-lifetime = "10m"
max-open-connections = 25
[cronjobs]
enable = true
[jwt]
allowed-users = [""]
secret = ""

View file

@ -5,11 +5,12 @@ import (
)
type Config struct {
Server ServerConfig
Log LoggingConfig
JWT JWTConfig
DB DBConfig
TG TGConfig
Server ServerConfig
Log LoggingConfig
JWT JWTConfig
DB DBConfig
TG TGConfig
CronJobs CronJobConfig
}
type ServerConfig struct {
@ -18,6 +19,10 @@ type ServerConfig struct {
EnablePprof bool
}
type CronJobConfig struct {
Enable bool
}
type TGConfig struct {
AppId int
AppHash string

View file

@ -41,9 +41,10 @@ type CronService struct {
logger *zap.SugaredLogger
}
func StartCronJobs(db *gorm.DB, cnf *config.Config) {
scheduler := gocron.NewScheduler(time.UTC)
func StartCronJobs(scheduler *gocron.Scheduler, db *gorm.DB, cnf *config.Config) {
if !cnf.CronJobs.Enable {
return
}
ctx := context.Background()
cron := CronService{db: db, cnf: cnf, logger: logging.DefaultLogger()}