Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add worker command #10

Merged
merged 1 commit into from Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 39 additions & 1 deletion cmd/postmand/main.go
Expand Up @@ -3,6 +3,9 @@ package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"

"github.com/allisson/go-env"
"github.com/allisson/postmand/repository"
Expand Down Expand Up @@ -30,6 +33,7 @@ func main() {
if err := db.Ping(); err != nil {
logger.Fatal("database-ping-error", zap.Error(err))
}
db.SetMaxOpenConns(env.GetInt("POSTMAND_DATABASE_MAX_OPEN_CONNS", 2))

// Setup cli
app := cli.NewApp()
Expand All @@ -45,7 +49,7 @@ func main() {
{
Name: "migrate",
Aliases: []string{"m"},
Usage: "execute database migration",
Usage: "executes database migration",
Action: func(c *cli.Context) error {
migrationRepository := repository.NewMigration(
db,
Expand All @@ -55,6 +59,40 @@ func main() {
return migrationService.Run(c.Context)
},
},
{
Name: "worker",
Aliases: []string{"w"},
Usage: "executes worker to dispatch webhooks",
Action: func(c *cli.Context) error {
deliveryRepository := repository.NewDelivery(db)
pollingInterval := time.Duration(env.GetInt("POSTMAND_POLLING_INTERVAL", 1000)) * time.Millisecond
workerService := service.NewWorker(deliveryRepository, logger, pollingInterval)

// Graceful shutdown
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)

// interrupt signal sent from terminal
signal.Notify(sigint, os.Interrupt)
// sigterm signal sent from kubernetes
signal.Notify(sigint, syscall.SIGTERM)

<-sigint

// We received an interrupt signal, shut down.
workerService.Shutdown(c.Context)
close(idleConnsClosed)
}()

logger.Info("worker-started")
workerService.Run(c.Context)

<-idleConnsClosed

return nil
},
},
}
err = app.Run(os.Args)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions local.env
@@ -1,3 +1,5 @@
POSTMAND_DATABASE_URL='postgres://user:pass@localhost:5432/postmand?sslmode=disable'
# See https://github.com/golang-migrate/migrate/tree/master/source/file
POSTMAND_DATABASE_MIGRATION_DIR='file://db/migrations'
POSTMAND_DATABASE_MAX_OPEN_CONNS='2'
POSTMAND_POLLING_INTERVAL='1000'