tsk-34: Fetch next ready scheduled message (#38)
Closes #34 Reviewed-on: phoenix/textsender-api#38 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -47,6 +47,30 @@ func (m *MockScheduledMessageStore) Get(ctx context.Context, id uuid.UUID) (*sch
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockScheduledMessageStore) FetchNextScheduledMessage(ctx context.Context) (*scheduling.ScheduledMessage, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return nil, m.Error
|
||||
}
|
||||
|
||||
var schMsg *scheduling.ScheduledMessage
|
||||
for _, msg := range m.ScheduledMessages {
|
||||
if msg.Status == scheduling.Ready {
|
||||
msg.Status = scheduling.Processing
|
||||
schMsg = msg
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if schMsg == nil {
|
||||
return nil, fmt.Errorf("No scheduled message is ready")
|
||||
} else {
|
||||
return schMsg, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockScheduledMessageStore) GetWithUserId(ctx context.Context, userId uuid.UUID) ([]*scheduling.ScheduledMessage, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
type ScheduledMessageStore interface {
|
||||
Get(ctx context.Context, id uuid.UUID) (*scheduling.ScheduledMessage, error)
|
||||
FetchNextScheduledMessage(ctx context.Context) (*scheduling.ScheduledMessage, error)
|
||||
GetWithUserId(ctx context.Context, userId uuid.UUID) ([]*scheduling.ScheduledMessage, error)
|
||||
CreateScheduledMessage(ctx context.Context, schedMsg *scheduling.ScheduledMessage) error
|
||||
UpdateStatus(ctx context.Context, id uuid.UUID, updatedStatus string) (*string, error)
|
||||
@@ -45,6 +46,33 @@ func (s *PGScheduledMessageStore) Get(ctx context.Context, id uuid.UUID) (*sched
|
||||
return &schMsg, nil
|
||||
}
|
||||
|
||||
func (s *PGScheduledMessageStore) FetchNextScheduledMessage(ctx context.Context) (*scheduling.ScheduledMessage, error) {
|
||||
query := `
|
||||
UPDATE scheduled_messages
|
||||
SET status = $1
|
||||
WHERE id = (
|
||||
SELECT id FROM scheduled_messages
|
||||
WHERE status = $2
|
||||
ORDER BY id
|
||||
FOR UPDATE SKIP LOCKED
|
||||
LIMIT 1
|
||||
)
|
||||
RETURNING id, scheduled, created, status, user_id
|
||||
`
|
||||
var schMsg scheduling.ScheduledMessage
|
||||
err := s.db.QueryRow(ctx, query, scheduling.Processing, scheduling.Ready).Scan(
|
||||
&schMsg.Id, &schMsg.Scheduled, &schMsg.Created, &schMsg.Status, &schMsg.UserId,
|
||||
)
|
||||
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, nil
|
||||
} else if err != nil {
|
||||
return nil, fmt.Errorf("Getting scheduled message: %w", err)
|
||||
} else {
|
||||
return &schMsg, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PGScheduledMessageStore) GetWithUserId(ctx context.Context, userId uuid.UUID) ([]*scheduling.ScheduledMessage, error) {
|
||||
query := `
|
||||
SELECT id, scheduled, created, status, user_id FROM scheduled_messages WHERE user_id = $1
|
||||
|
||||
Reference in New Issue
Block a user