cronworker

package
v1.10.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

README

Example

Create delivery handler

package workerhandler

import (
	"context"
	"fmt"
	"time"

	"github.com/golangid/candi/codebase/factory/types"
	"github.com/golangid/candi/candihelper"
	"github.com/golangid/candi/logger"
	"github.com/golangid/candi/tracer"
)

// CronHandler struct
type CronHandler struct {
}

// NewCronHandler constructor
func NewCronHandler() *CronHandler {
	return &CronHandler{}
}

// MountHandlers return group map topic key to handler func
func (h *CronHandler) MountHandlers(group *types.WorkerHandlerGroup) {

	group.Add(candihelper.CronJobKeyToString("push-notif", "message", "30s"), h.handlePushNotif)
	group.Add(candihelper.CronJobKeyToString("heavy-push-notif", "message", "22:43:07"), h.handleHeavyPush)
}

func (h *CronHandler) handlePushNotif(ctx context.Context, message []byte) error {
	trace := tracer.StartTrace(ctx, "CronDelivery-HandlePushNotif")
	defer trace.Finish()

	logger.LogI("processing")
	logger.LogI("done")
	return nil
}

func (h *CronHandler) handleHeavyPush(ctx context.Context, message []byte) error {
	trace := tracer.StartTrace(ctx, "CronDelivery-HandleHeavyPush")
	defer trace.Finish()

	fmt.Println("processing")
	time.Sleep(30 * time.Second)
	fmt.Println("done")
	return nil
}

Register in module

package examplemodule

import (

	"example.service/internal/modules/examplemodule/delivery/workerhandler"

	"github.com/golangid/candi/codebase/factory/dependency"
	"github.com/golangid/candi/codebase/factory/types"
	"github.com/golangid/candi/codebase/interfaces"
)

type Module struct {
	// ...another delivery handler
	workerHandlers map[types.Worker]interfaces.WorkerHandler
}

func NewModules(deps dependency.Dependency) *Module {
	return &Module{
		workerHandlers: map[types.Worker]interfaces.WorkerHandler{
			// ...another worker handler
			// ...
			types.Scheduler: workerhandler.NewCronHandler(),
		},
	}
}

// ...another method

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddJob

func AddJob(job Job) error

AddJob to cron worker

func CreateCronJobKey added in v1.9.0

func CreateCronJobKey(jobName, args, interval string) string

CreateCronJobKey helper

Allowed interval:

* standard time duration string, example: 2s, 10m

* custom start time and repeat duration, example:

  • 23:00@daily, will repeated at 23:00 every day
  • 23:00@weekly, will repeated at 23:00 every week
  • 23:00@10s, will repeated at 23:00 and next repeat every 10 seconds

func NewWorker

func NewWorker(service factory.ServiceFactory, opts ...OptionFunc) factory.AppServerFactory

NewWorker create new cron worker

func ParseCronJobKey added in v1.9.0

func ParseCronJobKey(str string) (jobName, args, interval string)

ParseCronJobKey helper

func UpdateIntervalActiveJob

func UpdateIntervalActiveJob(jobNumber int, newInterval string) (err error)

UpdateIntervalActiveJob update active job

Types

type CronJobKey added in v1.9.0

type CronJobKey struct {
	JobName  string `json:"jobName"`
	Args     string `json:"args"`
	Interval string `json:"interval"`
}

CronJobKey model

func (CronJobKey) String added in v1.9.0

func (c CronJobKey) String() string

String implement stringer

type Job

type Job struct {
	HandlerName string              `json:"handler_name"`
	Interval    string              `json:"interval"`
	Handler     types.WorkerHandler `json:"-"`
	Params      string              `json:"params"`
	WorkerIndex int                 `json:"worker_index"`
	// contains filtered or unexported fields
}

Job model

func GetActiveJobs

func GetActiveJobs() []*Job

GetActiveJobs get registered jobs

type OptionFunc added in v1.7.4

type OptionFunc func(*option)

OptionFunc type

func SetConsul added in v1.7.4

func SetConsul(consul *candiutils.Consul) OptionFunc

SetConsul option func

func SetDebugMode added in v1.7.4

func SetDebugMode(debugMode bool) OptionFunc

SetDebugMode option func

func SetLocker added in v1.8.8

func SetLocker(locker candiutils.Locker) OptionFunc

SetLocker option func

func SetMaxGoroutines added in v1.7.4

func SetMaxGoroutines(maxGoroutines int) OptionFunc

SetMaxGoroutines option func

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL