-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(destination): Introduced distributed lock, task delivery mapper,…
… and deployable workers (#77)
- Loading branch information
1 parent
7416db0
commit 9945004
Showing
29 changed files
with
383 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ logs/ | |
mise.log | ||
|
||
destination/logs.json | ||
destination/worker_log.json | ||
|
||
temp | ||
.env | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package etcd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"time" | ||
|
||
clientv3 "go.etcd.io/etcd/client/v3" | ||
"go.etcd.io/etcd/client/v3/concurrency" | ||
) | ||
|
||
type Config struct { | ||
Host string `koanf:"host"` | ||
Port int `koanf:"port"` | ||
DialTimeoutSeconds uint8 `koanf:"dial_timeout"` | ||
} | ||
|
||
type Adapter struct { | ||
client *clientv3.Client | ||
} | ||
|
||
func New(config Config) (Adapter, error) { | ||
etcdClient, err := clientv3.New(clientv3.Config{ | ||
Endpoints: []string{fmt.Sprintf("%s:%d", config.Host, config.Port)}, | ||
DialTimeout: time.Duration(config.DialTimeoutSeconds) * time.Second, | ||
}) | ||
if err != nil { | ||
slog.Error("Error creating etcd client: ", err) | ||
|
||
return Adapter{}, err | ||
} | ||
|
||
return Adapter{ | ||
client: etcdClient, | ||
}, nil | ||
} | ||
|
||
func (a Adapter) Client() *clientv3.Client { | ||
return a.client | ||
} | ||
|
||
func (a Adapter) Close() error { | ||
return a.client.Close() | ||
} | ||
|
||
func (a Adapter) Lock(ctx context.Context, key string, ttl int64) (unlock func() error, err error) { | ||
session, err := concurrency.NewSession(a.client, concurrency.WithTTL(int(ttl))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mutex := concurrency.NewMutex(session, key) | ||
if err := mutex.Lock(ctx); err != nil { | ||
return nil, err | ||
} | ||
|
||
return func() error { | ||
return mutex.Unlock(ctx) | ||
}, nil | ||
} |
108 changes: 108 additions & 0 deletions
108
cmd/destination/delivery_workers/webhook_delivery_worker.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"log/slog" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"time" | ||
|
||
"github.com/ormushq/ormus/adapter/etcd" | ||
"github.com/ormushq/ormus/adapter/redis" | ||
"github.com/ormushq/ormus/config" | ||
"github.com/ormushq/ormus/destination/taskdelivery" | ||
"github.com/ormushq/ormus/destination/taskdelivery/adapters/fakedeliveryhandler" | ||
"github.com/ormushq/ormus/destination/taskmanager/adapter/rabbitmqtaskmanager" | ||
"github.com/ormushq/ormus/destination/taskservice" | ||
"github.com/ormushq/ormus/destination/taskservice/adapter/idempotency/redistaskidempotency" | ||
"github.com/ormushq/ormus/destination/taskservice/adapter/repository/inmemorytaskrepo" | ||
"github.com/ormushq/ormus/destination/worker" | ||
"github.com/ormushq/ormus/logger" | ||
) | ||
|
||
const waitingAfterShutdownInSeconds = 2 | ||
|
||
func main() { | ||
done := make(chan bool) | ||
wg := sync.WaitGroup{} | ||
|
||
//----------------- Setup Logger -----------------// | ||
|
||
fileMaxSizeInMB := 10 | ||
fileMaxAgeInDays := 30 | ||
|
||
cfg := logger.Config{ | ||
FilePath: "./destination/worker_log.json", | ||
UseLocalTime: false, | ||
FileMaxSizeInMB: fileMaxSizeInMB, | ||
FileMaxAgeInDays: fileMaxAgeInDays, | ||
} | ||
|
||
logLevel := slog.LevelInfo | ||
if config.C().Destination.DebugMode { | ||
logLevel = slog.LevelDebug | ||
} | ||
|
||
opt := slog.HandlerOptions{ | ||
// todo should level debug be read from config? | ||
Level: logLevel, | ||
} | ||
l := logger.New(cfg, &opt) | ||
slog.SetDefault(l) | ||
|
||
//----------------- Setup Task Service -----------------// | ||
|
||
redisAdapter, err := redis.New(config.C().Redis) | ||
if err != nil { | ||
log.Panicf("error in new redis") | ||
} | ||
taskIdempotency := redistaskidempotency.New(redisAdapter, "tasks:", 30*24*time.Hour) | ||
|
||
taskRepo := inmemorytaskrepo.New() | ||
|
||
// Set up etcd as distributed locker. | ||
distributedLocker, err := etcd.New(config.C().Etcd) | ||
if err != nil { | ||
log.Panicf("Error on new etcd") | ||
} | ||
|
||
taskHandler := taskservice.New(taskIdempotency, taskRepo, distributedLocker) | ||
|
||
// Register delivery handlers | ||
// each destination type can have specific delivery handler | ||
fakeTaskDeliveryHandler := fakedeliveryhandler.New() | ||
taskdelivery.Register("webhook", fakeTaskDeliveryHandler) | ||
|
||
//----------------- Consume ProcessEvents -----------------// | ||
|
||
taskConsumerConf := config.C().Destination.RabbitMQTaskManagerConnection | ||
webhookTaskConsumer := rabbitmqtaskmanager.NewTaskConsumer(taskConsumerConf, "webhook_tasks_queue") | ||
|
||
processedEvents, err := webhookTaskConsumer.Consume(done, &wg) | ||
if err != nil { | ||
log.Panicf("Error on consuming tasks.") | ||
} | ||
|
||
w1 := worker.NewWorker(processedEvents, taskHandler) | ||
|
||
w1Err := w1.Run(done, &wg) | ||
if w1Err != nil { | ||
log.Panicf("%s: %s", "Error on webhook worker", err) | ||
} | ||
|
||
//----------------- Handling graceful shutdown -----------------// | ||
|
||
quit := make(chan os.Signal, 1) | ||
signal.Notify(quit, os.Interrupt) | ||
<-quit | ||
|
||
slog.Info("Received interrupt signal, shutting down gracefully...") | ||
done <- true | ||
|
||
close(done) | ||
|
||
// todo use config for waiting time after graceful shutdown | ||
time.Sleep(waitingAfterShutdownInSeconds * time.Second) | ||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/ormushq/ormus/adapter/etcd" | ||
"github.com/ormushq/ormus/adapter/redis" | ||
"github.com/ormushq/ormus/destination/dconfig" | ||
"github.com/ormushq/ormus/manager" | ||
"github.com/ormushq/ormus/source" | ||
) | ||
|
||
type Config struct { | ||
Manager manager.Config `koanf:"manager"` | ||
Redis redis.Config `koanf:"redis"` | ||
Etcd etcd.Config `koanf:"etcd"` | ||
Manager manager.Config `koanf:"manager"` | ||
Source source.Config `koanf:"source"` | ||
Destination dconfig.Config `koanf:"destination"` | ||
} |
Oops, something went wrong.