60 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package tasks
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"encoding/json"
 | |
| 	"fmt"
 | |
| 
 | |
| 	"management/internal/erpserver/model/system"
 | |
| 
 | |
| 	"github.com/hibiken/asynq"
 | |
| )
 | |
| 
 | |
| const TaskConsumeAuditLog = "task:audit"
 | |
| 
 | |
| type PayloadConsumeAuditLog struct {
 | |
| 	*system.AuditLog
 | |
| }
 | |
| 
 | |
| func (d *RedisTaskDistributor) DistributeTaskConsumeAuditLog(
 | |
| 	ctx context.Context,
 | |
| 	payload *PayloadConsumeAuditLog,
 | |
| 	opts ...asynq.Option,
 | |
| ) error {
 | |
| 	jsonPayload, err := json.Marshal(payload)
 | |
| 	if err != nil {
 | |
| 		return fmt.Errorf("failed to marshal task payload: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	task := asynq.NewTask(TaskConsumeAuditLog, jsonPayload, opts...)
 | |
| 	_, err = d.client.EnqueueContext(ctx, task)
 | |
| 	if err != nil {
 | |
| 		return fmt.Errorf("failed to enqueue task: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	//d.log.Info("enqueued task",
 | |
| 	//	zap.String("type", task.Type()),
 | |
| 	//	zap.Binary("payload", task.Payload()),
 | |
| 	//	zap.String("queue", info.Queue),
 | |
| 	//	zap.Int("max_retry", info.MaxRetry),
 | |
| 	//)
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func (p *RedisTaskProcessor) ProcessTaskConsumeAuditLog(ctx context.Context, task *asynq.Task) error {
 | |
| 	var payload PayloadConsumeAuditLog
 | |
| 	if err := json.Unmarshal(task.Payload(), &payload); err != nil {
 | |
| 		return fmt.Errorf("failed to unmarshal payload: %w", asynq.SkipRetry)
 | |
| 	}
 | |
| 
 | |
| 	if err := p.auditService.Create(ctx, payload.AuditLog); err != nil {
 | |
| 		return fmt.Errorf("failed to process task: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	//p.log.Info("processed task",
 | |
| 	//	zap.String("type", task.Type()),
 | |
| 	//	zap.Binary("payload", task.Payload()),
 | |
| 	//)
 | |
| 	return nil
 | |
| }
 |