cronworker

package
v1.6.17 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2021 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 NewWorker

NewWorker create new cron worker

func UpdateIntervalActiveJob

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

UpdateIntervalActiveJob update active job

Types

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

Jump to

Keyboard shortcuts

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