package timeout

import (
	"context"
	"database/sql"
	"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal"
	"penahub.gitlab.yandexcloud.net/backend/quiz/worker/workers"
	"time"
)

// Timeout struct of worker for expiration worker
type Timeout struct {
	w *workers.Worker
	d *dal.DAL
}

// New creation of worker
func New(d *dal.DAL, p time.Duration) *Timeout {
	return &Timeout{
		w: workers.New(p),
		d: d,
	}
}

// Start method for starting worker with long polling from postgres
func (t *Timeout) Start(ctx context.Context) {
	t.w.Start(ctx, func(ctx context.Context) error {
		if err := t.d.WorkerRepo.WorkerTimeoutProcess(ctx); err != nil {
			if err != sql.ErrNoRows {
				return err
			}
		}

		return nil
	})
}

func (t *Timeout) ExposeErr(ctx context.Context, err *error) {
	t.w.ExposeErr(ctx, err)
}
