Skip to content

Commit

Permalink
[ADD] example
Browse files Browse the repository at this point in the history
  • Loading branch information
withseid committed Aug 18, 2022
1 parent 5b784dc commit 4c273fc
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 20 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# 延迟队列 delayq



## 业务背景

在工作时,遇到以下业务需求
Expand All @@ -12,7 +14,7 @@



后来,我们采用的方案是***\*延迟队列\****,一开始考虑的是用 [beanstalkd](https://github.com/beanstalkd/beanstalkd) , 但是 beanstalkd 存在一个问题是,只支持入队操作,不支持出队操作。因此 beanstalkd 也被抛弃了。因为在我们的需求中,是需要有出队操作的。
后来,我们采用的方案是**延迟队列**,一开始考虑的是用 [beanstalkd](https://github.com/beanstalkd/beanstalkd) , 但是 beanstalkd 存在一个问题是,只支持入队操作,不支持出队操作。因此 beanstalkd 也被抛弃了。因为在我们的需求中,是需要有出队操作的。

例如,当用户删除某个资源时,将该准备删除的资源加入延迟队列,24小时后自动删除该资源,如果在 24 小时内,用户点击了撤销操作,此时需要将该资源的信息从延迟队列中删除。

Expand Down Expand Up @@ -44,7 +46,7 @@
## Quickstart
通过 go get 安装 delayq
```go
go get -u github.com/yangdodo/delayq
go get -u github.com/withseid/delayq
```

新建一个 Client,通过 Client 将延迟任务入队
Expand Down
43 changes: 43 additions & 0 deletions example/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"encoding/json"
"time"

"github.com/withseid/delayq"
"github.com/withseid/delayq/example/model"
)

func main() {
config := delayq.RedisConfiguration{
Host: "192.168.89.160",
Port: "6379",
}
client := delayq.NewClient(config)

space1 := model.DeletedSpace{
SpaceID: "space1",
}
data, err := json.Marshal(space1)
if err != nil {
panic(err)
}

// 假设当前时间是 2022-08-16 18:12
// delayq.ProcessAt(time.Now().AddDate(0, 0, 1)) 表示将在 2022-08-17 18:12 执行该任务
client.Enqueue(space1.Topic(), space1.SpaceID, data, delayq.ProcessAt(time.Now().AddDate(0, 0, 1)))

space2 := model.DeletedSpace{
SpaceID: "space2",
}
data, err = json.Marshal(space2)
if err != nil {
panic(err)
}
// 假设当前时间是 2022-08-16 18:12
// delayq.ProcessIn(time.Hour*24) 表示将在当前时间的基础上,延迟 24 小时后执行
client.Enqueue(space2.Topic(), space2.SpaceID, data, delayq.ProcessIn(time.Hour*24))

// 将 JobID 为 space2 的延迟任务出队
client.Dequeue(space2.Topic(), space2.SpaceID)
}
30 changes: 30 additions & 0 deletions example/model/mode,.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package model

import (
"context"
"encoding/json"
"fmt"

"github.com/withseid/delayq"
)

// const DeletedSpaceTopic = "deleted_space"

type DeletedSpace struct {
SpaceID string
}

func (d *DeletedSpace) Topic() string {
return "deleted_space"
}

func (d *DeletedSpace) Execute(ctx context.Context, job *delayq.Job) error {
ds := DeletedSpace{}
err := json.Unmarshal(job.Boday, &ds)
if err != nil {
return err
}

fmt.Println("DeletedSpace: ", ds)
return nil
}
20 changes: 20 additions & 0 deletions example/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"context"

"github.com/withseid/delayq"
"github.com/withseid/delayq/example/model"
)

func main() {
config := delayq.RedisConfiguration{
Host: "192.168.89.160",
Port: "6379",
}
server := delayq.NewServer(config)
ds := model.DeletedSpace{}

server.HandlerFunc(ds.Topic(), &ds)
server.Run(context.TODO())
}
9 changes: 2 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
module github.com/yangdodo/delayq
module github.com/withseid/delayq

go 1.17

require (
github.com/go-redis/redis/v8 v8.11.5
github.com/satori/go.uuid v1.2.0
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
)
require github.com/go-redis/redis/v8 v8.11.5

require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
11 changes: 0 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
Expand All @@ -49,8 +44,6 @@ github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAl
github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE=
github.com/onsi/gomega v1.18.1/go.mod h1:0q+aL8jAiMXy9hbwj2mr5GziHiwhAIQpFmmtT5hitRs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand All @@ -68,8 +61,6 @@ golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down Expand Up @@ -105,8 +96,6 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
Expand Down

0 comments on commit 4c273fc

Please sign in to comment.