From 965553b285fd0ca28d537ced66f0de8bf5f21545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=99=AF=E9=98=B3?= <1656488874@qq.com> Date: Mon, 28 Oct 2019 12:36:00 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BB=A3=E7=A0=81=E9=81=97=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/utils/helpers.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/backend/utils/helpers.go b/backend/utils/helpers.go index 541d9002..1c8c96e3 100644 --- a/backend/utils/helpers.go +++ b/backend/utils/helpers.go @@ -1,6 +1,8 @@ package utils import ( + "context" + "crawlab/database" "crawlab/entity" "encoding/json" "github.com/apex/log" @@ -40,3 +42,22 @@ func Close(c io.Closer) { log.WithError(err).Error("关闭资源文件失败。") } } + +func Pub(channel string, msg entity.NodeMessage) error { + if _, err := database.RedisClient.Publish(channel, GetJson(msg)); err != nil { + log.Errorf("publish redis error: %s", err.Error()) + debug.PrintStack() + return err + } + return nil +} + +func Sub(channel string, consume database.ConsumeFunc) error { + ctx := context.Background() + if err := database.RedisClient.Subscribe(ctx, consume, channel); err != nil { + log.Errorf("subscribe redis error: %s", err.Error()) + debug.PrintStack() + return err + } + return nil +}