DeliveryRetryJob
This commit is contained in:
@@ -29,6 +29,7 @@ type Application struct {
|
||||
Database *db.Database
|
||||
Firebase *firebase.App
|
||||
DefaultChannel string
|
||||
Jobs []Job
|
||||
}
|
||||
|
||||
func NewApp(db *db.Database) *Application {
|
||||
@@ -38,10 +39,11 @@ func NewApp(db *db.Database) *Application {
|
||||
}
|
||||
}
|
||||
|
||||
func (app *Application) Init(cfg scn.Config, g *gin.Engine, fb *firebase.App) {
|
||||
func (app *Application) Init(cfg scn.Config, g *gin.Engine, fb *firebase.App, jobs []Job) {
|
||||
app.Config = cfg
|
||||
app.Gin = g
|
||||
app.Firebase = fb
|
||||
app.Jobs = jobs
|
||||
}
|
||||
|
||||
func (app *Application) Run() {
|
||||
@@ -59,22 +61,33 @@ func (app *Application) Run() {
|
||||
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
for _, job := range app.Jobs {
|
||||
job.Start()
|
||||
}
|
||||
|
||||
select {
|
||||
case <-stop:
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
log.Info().Msg("Stopping HTTP-Server")
|
||||
|
||||
err := httpserver.Shutdown(ctx)
|
||||
|
||||
if err != nil {
|
||||
log.Info().Err(err).Msg("Error while stopping the http-server")
|
||||
return
|
||||
} else {
|
||||
log.Info().Msg("Stopped HTTP-Server")
|
||||
}
|
||||
log.Info().Msg("Stopped HTTP-Server")
|
||||
|
||||
case err := <-errChan:
|
||||
log.Error().Err(err).Msg("HTTP-Server failed")
|
||||
}
|
||||
|
||||
for _, job := range app.Jobs {
|
||||
job.Start()
|
||||
}
|
||||
}
|
||||
|
||||
func (app *Application) GenerateRandomAuthKey() string {
|
||||
@@ -147,6 +160,11 @@ func (app *Application) StartRequest(g *gin.Context, uri any, query any, body an
|
||||
return actx, nil
|
||||
}
|
||||
|
||||
func (app *Application) NewSimpleTransactionContext(timeout time.Duration) *SimpleContext {
|
||||
ictx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
return CreateSimpleContext(ictx, cancel)
|
||||
}
|
||||
|
||||
func (app *Application) getPermissions(ctx *AppContext, hdr string) (PermissionSet, error) {
|
||||
if hdr == "" {
|
||||
return NewEmptyPermissions(), nil
|
||||
@@ -222,3 +240,15 @@ func (app *Application) NormalizeUsername(v string) string {
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func (app *Application) DeliverMessage(ctx context.Context, client models.Client, msg models.Message) (*string, error) {
|
||||
if client.FCMToken != nil {
|
||||
fcmDelivID, err := app.Firebase.SendNotification(ctx, client, msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return langext.Ptr(fcmDelivID), nil
|
||||
} else {
|
||||
return langext.Ptr(""), nil
|
||||
}
|
||||
}
|
||||
|
6
server/logic/jobs.go
Normal file
6
server/logic/jobs.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package logic
|
||||
|
||||
type Job interface {
|
||||
Start()
|
||||
Stop()
|
||||
}
|
94
server/logic/simplecontext.go
Normal file
94
server/logic/simplecontext.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"blackforestbytes.com/simplecloudnotifier/db"
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SimpleContext struct {
|
||||
inner context.Context
|
||||
cancelFunc context.CancelFunc
|
||||
cancelled bool
|
||||
transaction *sql.Tx
|
||||
}
|
||||
|
||||
func CreateSimpleContext(innerCtx context.Context, cancelFn context.CancelFunc) *SimpleContext {
|
||||
return &SimpleContext{
|
||||
inner: innerCtx,
|
||||
cancelFunc: cancelFn,
|
||||
cancelled: false,
|
||||
transaction: nil,
|
||||
}
|
||||
}
|
||||
|
||||
func (sc *SimpleContext) Deadline() (deadline time.Time, ok bool) {
|
||||
return sc.inner.Deadline()
|
||||
}
|
||||
|
||||
func (sc *SimpleContext) Done() <-chan struct{} {
|
||||
return sc.inner.Done()
|
||||
}
|
||||
|
||||
func (sc *SimpleContext) Err() error {
|
||||
return sc.inner.Err()
|
||||
}
|
||||
|
||||
func (sc *SimpleContext) Value(key any) any {
|
||||
return sc.inner.Value(key)
|
||||
}
|
||||
|
||||
func (sc *SimpleContext) Cancel() {
|
||||
sc.cancelled = true
|
||||
if sc.transaction != nil {
|
||||
log.Error().Msg("Rollback transaction")
|
||||
err := sc.transaction.Rollback()
|
||||
if err != nil {
|
||||
panic("failed to rollback transaction: " + err.Error())
|
||||
}
|
||||
sc.transaction = nil
|
||||
}
|
||||
sc.cancelFunc()
|
||||
}
|
||||
|
||||
func (sc *SimpleContext) GetOrCreateTransaction(db *db.Database) (*sql.Tx, error) {
|
||||
if sc.cancelled {
|
||||
return nil, errors.New("context cancelled")
|
||||
}
|
||||
if sc.transaction != nil {
|
||||
return sc.transaction, nil
|
||||
}
|
||||
tx, err := db.BeginTx(sc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sc.transaction = tx
|
||||
return tx, nil
|
||||
}
|
||||
|
||||
func (sc *SimpleContext) CommitTransaction() error {
|
||||
if sc.transaction == nil {
|
||||
return nil
|
||||
}
|
||||
err := sc.transaction.Commit()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sc.transaction = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sc *SimpleContext) RollbackTransaction() {
|
||||
if sc.transaction == nil {
|
||||
return
|
||||
}
|
||||
err := sc.transaction.Rollback()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sc.transaction = nil
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user