Update storage queues and remote server info

This commit is contained in:
Melody Becker 2024-09-17 15:45:43 +02:00
parent fff753e3aa
commit 8f75009848
4 changed files with 110 additions and 14 deletions

View file

@ -11,3 +11,44 @@ type OutboundJob struct {
TargetPath string `gorm:"->;<-:create"` // The full path of api endpoint targeted
Data []byte `gorm:"->;<-:create"` // The raw data to send
}
func (s *Storage) AddNewOutboundJob(data []byte, targetDomain string, targetUrl string) {
newJob := OutboundJob{
Data: data,
TargetServer: targetDomain,
TargetPath: targetUrl,
}
s.db.Create(&newJob)
}
// Get the specified amount of jobs, sorted by age (oldest first)
func (s *Storage) GetOldestOutboundJobs(amount uint) ([]OutboundJob, error) {
jobs := []OutboundJob{}
err := s.db.Order("id asc, created_at asc").Limit(int(amount)).Find(jobs).Error
switch err {
case gorm.ErrRecordNotFound:
return nil, ErrEntryNotFound
case nil:
return jobs, nil
default:
return nil, err
}
}
func (s *Storage) GetOutboundJobsForDomain(domain string, amount uint) ([]OutboundJob, error) {
jobs := []OutboundJob{}
err := s.db.Where("target_server = ?", domain).Order("id asc, created_at asc").Limit(int(amount)).Find(jobs).Error
switch err {
case gorm.ErrRecordNotFound:
return nil, ErrEntryNotFound
case nil:
return jobs, nil
default:
return nil, err
}
}
func (s *Storage) CompleteOutboundJob(id uint) error {
s.db.Delete(OutboundJob{Model: gorm.Model{ID: id}})
return nil
}