redisworker

package
v1.7.8 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

README

Example

Create delivery handler

package workerhandler

import (
	"context"
	"time"

	"pkg.agungdp.dev/candi/codebase/factory/types"
	"pkg.agungdp.dev/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-push-notif", h.handleScheduledPushNotif)
	group.Add("heavy-push", h.handleHeavyPush)
}

func (h *RedisHandler) handleScheduledPushNotif(ctx context.Context, message []byte) error {
	// process usecase
	logger.LogIf("success handling message: %s", string(message))
	return err
}

func (h *RedisHandler) handleHeavyPush(ctx context.Context, message []byte) error {
	logger.LogI("start heavy push")
	time.Sleep(30 * time.Second)
	logger.LogI("heavy push done")
	return nil
}

Register in module

package examplemodule

import (

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

	"pkg.agungdp.dev/candi/codebase/factory/dependency"
	"pkg.agungdp.dev/candi/codebase/factory/types"
	"pkg.agungdp.dev/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"

	"pkg.agungdp.dev/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 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 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 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