taskcafe/internal/commands/web.go

122 lines
3.1 KiB
Go
Raw Normal View History

2020-07-16 01:20:08 +02:00
package commands
import (
"net/http"
"time"
"github.com/RichardKnop/machinery/v1"
2021-11-18 00:11:28 +01:00
mTasks "github.com/RichardKnop/machinery/v1/tasks"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/httpfs"
2020-07-16 01:20:08 +02:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2020-07-16 01:20:08 +02:00
"github.com/jmoiron/sqlx"
"github.com/jordanknott/taskcafe/internal/config"
2020-08-07 03:50:35 +02:00
"github.com/jordanknott/taskcafe/internal/route"
2020-07-16 01:20:08 +02:00
log "github.com/sirupsen/logrus"
)
var autoMigrate bool
2020-07-16 01:20:08 +02:00
func newWebCmd() *cobra.Command {
cc := &cobra.Command{
2020-07-16 01:20:08 +02:00
Use: "web",
Short: "Run the web server",
Long: "Run the web & api server",
RunE: func(cmd *cobra.Command, args []string) error {
2020-07-16 01:20:08 +02:00
Formatter := new(log.TextFormatter)
Formatter.TimestampFormat = "02-01-2006 15:04:05"
Formatter.FullTimestamp = true
log.SetFormatter(Formatter)
log.SetLevel(log.InfoLevel)
appConfig, err := config.GetAppConfig()
if err != nil {
return err
}
2021-11-18 00:11:28 +01:00
redisClient, err := appConfig.MessageQueue.GetMessageQueueClient()
if err != nil {
return err
}
defer redisClient.Close()
connection := appConfig.Database.GetDatabaseConnectionUri()
var db *sqlx.DB
var retryDuration time.Duration
maxRetryNumber := 4
for i := 0; i < maxRetryNumber; i++ {
db, err = sqlx.Connect("postgres", connection)
if err == nil {
break
}
retryDuration = time.Duration(i*2) * time.Second
log.WithFields(log.Fields{"retryNumber": i, "retryDuration": retryDuration}).WithError(err).Error("issue connecting to database, retrying")
if i != maxRetryNumber-1 {
time.Sleep(retryDuration)
}
}
if err != nil {
return err
2020-07-16 01:20:08 +02:00
}
db.SetMaxOpenConns(25)
db.SetMaxIdleConns(25)
db.SetConnMaxLifetime(5 * time.Minute)
defer db.Close()
if viper.GetBool("migrate") {
log.Info("running auto schema migrations")
if err = runMigration(db); err != nil {
return err
}
}
var server *machinery.Server
2021-11-18 00:11:28 +01:00
jobConfig := appConfig.Job.GetJobConfig()
server, err = machinery.NewServer(&jobConfig)
if err != nil {
return err
}
signature := &mTasks.Signature{
Name: "scheduleDueDateNotifications",
}
2021-11-18 00:11:28 +01:00
server.SendTask(signature)
2021-11-18 00:11:28 +01:00
r, _ := route.NewRouter(db, redisClient, server, appConfig)
log.WithFields(log.Fields{"url": viper.GetString("server.hostname")}).Info("starting server")
return http.ListenAndServe(viper.GetString("server.hostname"), r)
2020-07-16 01:20:08 +02:00
},
}
cc.Flags().Bool("migrate", false, "if true, auto run's schema migrations before starting the web server")
viper.BindPFlag("migrate", cc.Flags().Lookup("migrate"))
viper.SetDefault("migrate", false)
return cc
}
func runMigration(db *sqlx.DB) error {
driver, err := postgres.WithInstance(db.DB, &postgres.Config{})
if err != nil {
return err
}
src, err := httpfs.New(migration, "./")
if err != nil {
return err
}
m, err := migrate.NewWithInstance("httpfs", src, "postgres", driver)
if err != nil {
return err
}
logger := &MigrateLog{}
m.Log = logger
err = m.Up()
if err != nil && err != migrate.ErrNoChange {
return err
}
return nil
2020-07-16 01:20:08 +02:00
}