cronworker

package
v1.14.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

README

Example

Create delivery handler

package workerhandler

import (
	"context"
	"fmt"
	"time"

	"github.com/golangid/candi/candishared"
	cronworker "github.com/golangid/candi/codebase/app/cron_worker"
	"github.com/golangid/candi/codebase/factory/types"
	"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(cronworker.CreateCronJobKey("push-notif", "message", "30s"), h.handleJob1)
	group.Add(cronworker.CreateCronJobKey("heavy-push-notif", "message", "22:43:07"), h.handleJob2)
}

func (h *CronHandler) handleJob1(eventContext *candishared.EventContext) error {
	trace := tracer.StartTrace(eventContext.Context(), "DeliveryCronWorker:HandleJob1")
	defer trace.Finish()

	logger.LogI("running...")
	return nil
}

func (h *CronHandler) handleJob2(eventContext *candishared.EventContext) error {
	trace := tracer.StartTrace(eventContext.Context(), "DeliveryCronWorker:HandleJob2")
	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 CreateCronJobKey added in v1.9.0

func CreateCronJobKey(jobName, args, interval string) string

CreateCronJobKey helper

Allowed interval:

* cron expression, example: * * * * *

* 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

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

type OptionFunc added in v1.7.4

type OptionFunc func(*option)

OptionFunc type

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