farmstar-survey-backend/internal/worker/worker.go

69 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-02-05 14:05:28 +01:00
package worker
import (
"context"
"time"
"github.com/jackc/pgx/v5"
"github.com/riverqueue/river"
)
type (
Worker struct {
Client *river.Client[pgx.Tx]
}
)
func NewWorker(riverClient *river.Client[pgx.Tx]) *Worker {
return &Worker{
Client: riverClient,
}
}
func (w *Worker) Start() error {
return w.Client.Start(context.Background())
}
func (w *Worker) Stop() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
return w.Client.Stop(ctx)
}
func (w *Worker) QueueRewardsTask(phone string, amount uint) error {
_, err := w.Client.Insert(context.Background(), RewardsArgs{
Phone: phone,
Amount: amount,
}, nil)
if err != nil {
return err
}
return nil
}
func (w *Worker) QueueSMSTask(phone string, SMS SMSType, text string) error {
_, err := w.Client.Insert(context.Background(), SMSArgs{
Phone: phone,
SMS: SMS,
Text: text,
}, nil)
if err != nil {
return err
}
return nil
}
func (w *Worker) QueueTgTask(msg string) error {
_, err := w.Client.Insert(context.Background(), TelegramArgs{
Msg: msg,
}, nil)
if err != nil {
return err
}
return nil
}