redisworker

package
v1.13.16 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2023 License: Apache-2.0 Imports: 15 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
	key := candihelper.BuildRedisPubSubKeyTopic("scheduled-push-notif", map[string]string{"message": "hello"})
	uc.cache.Set(ctx, key, "ok", 5*time.Minute)
}

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 interface{}) string

CreateRedisPubSubMessage create new redis pubsub message

func DeleteRedisPubSubMessage added in v1.9.1

func DeleteRedisPubSubMessage(topic string, message interface{}) string

DeleteRedisPubSubMessage delete redis key pubsub message pattern

func NewWorker

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

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