farmstar-survey-backend/internal/worker/worker.go
2024-02-05 16:05:28 +03:00

69 lines
1.1 KiB
Go

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
}