diff --git a/docker-compose.yml b/docker-compose.yml index bb18e5b..8362fbb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1 +1,16 @@ -services: name +# Development compose file +services: + postgres: + image: docker.io/postgres:16.4 + restart: unless-stopped + environment: + POSTGRES_PASSWORD: linstrom + POSTGRES_USER: linstrom + POSTGRES_DB: linstrom + healthcheck: + test: ["CMD-SHELL", "pg_isready"] + interval: 1s + timeout: 5s + retries: 10 + ports: + - 5432:5432 diff --git a/main.go b/main.go index 4d6de79..677a965 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,13 @@ func main() { log.Fatal().Err(err).Msg("Failed to start cache") } + // var store *storage.Storage + // if config.GlobalConfig.Storage.DbIsPostgres != nil && *config.GlobalConfig.Storage.DbIsPostgres { + // store, err = storage.NewStoragePostgres(config.GlobalConfig.Storage.DatabaseUrl, storageCache) + // } else { + // store, err = storage.NewStorageSqlite(config.GlobalConfig.Storage.DatabaseUrl, storageCache) + // } + // store, err := storage.NewStorage(config.GlobalConfig.Storage.BuildPostgresDSN(), storageCache) if err != nil { diff --git a/storage/inboundJobs.go b/storage/inboundJobs.go index 909a0a8..736db97 100644 --- a/storage/inboundJobs.go +++ b/storage/inboundJobs.go @@ -2,6 +2,8 @@ package storage import ( "time" + + "gorm.io/gorm" ) // Auto-generate string names for the various constants @@ -34,3 +36,25 @@ type InboundJob struct { // If from an inbox, include the owner id here InboxOwner *string `gorm:"->;<-create"` } + +func (s *Storage) AddNewInboundJob(data []byte, source InboundJobSource, inboxOwner *string) { + newJob := InboundJob{ + RawData: data, + Source: source, + InboxOwner: inboxOwner, + } + s.db.Create(&newJob) +} + +// Get the specified amount of jobs, sorted by age (oldest first) +func (s *Storage) GetOldestInboundJobs(amount int) ([]InboundJob, error) { + jobs := []InboundJob{} + switch err := s.db.Order("id asc, created_at asc").Limit(amount).Find(jobs).Error; err { + case gorm.ErrRecordNotFound: + return nil, ErrEntryNotFound + case nil: + return jobs, nil + default: + return nil, err + } +} diff --git a/storage/storage.go.old b/storage/storage.go.old index 92ce234..837424d 100644 --- a/storage/storage.go.old +++ b/storage/storage.go.old @@ -45,6 +45,8 @@ func storageFromEmptyDb(db *gorm.DB, cache *cache.Cache) (*Storage, error) { Note{}, Role{}, PasskeySession{}, + InboundJob{}, + OutboundJob{}, ) if err != nil { return nil, err