redisworker

package
v1.18.5 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

README

Example

Create delivery handler

package workerhandler

import (
	"context"
	"time"

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

// RedisHandler struct
type RedisHandler struct {
}

// NewRedisHandler constructor
func NewRedisHandler() *RedisHandler {
	return &RedisHandler{}
}

// MountHandlers return group map topic key to handler func
func (h *RedisHandler) MountHandlers(group *types.WorkerHandlerGroup) {
	group.Add("scheduled-job", h.handleScheduledJob)
}

func (h *RedisHandler) handleScheduledJob(eventContext *candishared.EventContext) error {
	trace := tracer.StartTrace(eventContext.Context(), "DeliveryRedisWorker:HandleScheduledJob")
	defer trace.Finish()

	log.Printf("message received. message: %s\n", eventContext.Message())
	// call usecase
	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.RedisSubscriber: workerhandler.NewRedisHandler(),
		},
	}
}

// ...another method

Add job in each usecase module

package usecase

import (
	"context"
	"time"

	"github.com/golangid/candi/candihelper"
)

func (uc *usecaseImpl) someUsecase(ctx context.Context) {
	// scheduled exec to "scheduled-push-notif" handler after 5 minutes from now

	timerDuration, _ := time.ParseDuration("5m")
	message := "hello"
	job := candishared.PublisherArgument{
		Topic: "foobar",
		Key:   "foo_007",
		Message: candihelper.ToBytes(message),
		Delay: timerDuration,
		IsDeleteMessage: true,
	}
	uc.deps.GetBroker(types.RedisSubscriber).GetPublisher().PublishMessage(ctx, &job)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateRedisPubSubMessage added in v1.9.0

func CreateRedisPubSubMessage(topic string, message any) string

CreateRedisPubSubMessage create new redis pubsub message

func DeleteRedisPubSubMessage added in v1.9.1

func DeleteRedisPubSubMessage(topic string, message any) string

DeleteRedisPubSubMessage delete redis key pubsub message pattern

func NewWorker

NewWorker create new redis subscriber

Types

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 SetMaxGoroutines added in v1.7.4

func SetMaxGoroutines(maxGoroutines int) OptionFunc

SetMaxGoroutines option func

type RedisMessage added in v1.9.0

type RedisMessage struct {
	HandlerName string `json:"h"`
	Message     string `json:"message"`
	EventID     string `json:"id,omitempty"`
}

RedisMessage model for redis subscriber key

func ParseRedisPubSubKeyTopic added in v1.9.0

func ParseRedisPubSubKeyTopic(key []byte) (redisMessage RedisMessage)

ParseRedisPubSubKeyTopic parse key to redis message

Jump to

Keyboard shortcuts

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