debian-forge-composer/internal/weldr/compose.go
Lars Karlitski b5769add2c store: move queue out of the store
The store is responsible for two things: user state and the compose queue. This
is problematic, because the rcm API has slightly different semantics from weldr
and only used the queue part of the store. Also, the store is simply too
complex.

This commit splits the queue part out, using the new jobqueue package in both
the weldr and the rcm package. The queue is saved to a new directory `queue/`.

The weldr package now also has access to a worker server to enqueue and list
jobs. Its store continues to track composes, but the `QueueStatus` for each
compose (and image build) is deprecated. The field in `ImageBuild` is kept for
backwards compatibility for composes which finished before this change, but a
lot of code dealing with it in package compose is dropped.

store.PushCompose() is degraded to storing a new compose. It should probably be
renamed in the future. store.PopJob() is removed.

Job ids are now independent of compose ids. Because of that, the local
target gains ComposeId and ImageBuildId fields, because a worker cannot
infer those from a job anymore. This also necessitates a change in the
worker API: the job routes are changed to expect that instead of a
(compose id, image build id) pair. The route that accepts built images
keeps that pair, because it reports the image back to weldr.

worker.Server() now interacts with a job queue instead of the store. It gains
public functions that allow enqueuing an osbuild job and getting its status,
because only it knows about the specific argument and result types in the job
queue (OSBuildJob and OSBuildJobResult). One oddity remains: it needs to report
an uploaded image to weldr. Do this with a function that's passed in for now,
so that the dependency to the store can be dropped completely.

The rcm API drops its dependencies to package blueprint and store, because it
too interacts only with the worker server now.

Fixes #342
2020-05-08 14:53:00 +02:00

70 lines
2.5 KiB
Go

package weldr
import (
"sort"
"time"
"github.com/google/uuid"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/osbuild/osbuild-composer/internal/compose"
)
type ComposeEntry struct {
ID uuid.UUID `json:"id"`
Blueprint string `json:"blueprint"`
Version string `json:"version"`
ComposeType common.ImageType `json:"compose_type"`
ImageSize uint64 `json:"image_size"` // This is user-provided image size, not actual file size
QueueStatus common.ImageBuildState `json:"queue_status"`
JobCreated float64 `json:"job_created"`
JobStarted float64 `json:"job_started,omitempty"`
JobFinished float64 `json:"job_finished,omitempty"`
Uploads []uploadResponse `json:"uploads,omitempty"`
}
func composeToComposeEntry(id uuid.UUID, compose compose.Compose, state common.ComposeState, queued, started, finished time.Time, includeUploads bool) *ComposeEntry {
var composeEntry ComposeEntry
composeEntry.ID = id
composeEntry.Blueprint = compose.Blueprint.Name
composeEntry.Version = compose.Blueprint.Version
composeEntry.ComposeType = compose.ImageBuilds[0].ImageType
if includeUploads {
composeEntry.Uploads = targetsToUploadResponses(compose.ImageBuilds[0].Targets)
}
switch state {
case common.CWaiting:
composeEntry.QueueStatus = common.IBWaiting
composeEntry.JobCreated = float64(queued.UnixNano()) / 1000000000
case common.CRunning:
composeEntry.QueueStatus = common.IBRunning
composeEntry.JobCreated = float64(queued.UnixNano()) / 1000000000
composeEntry.JobStarted = float64(started.UnixNano()) / 1000000000
case common.CFinished:
composeEntry.QueueStatus = common.IBFinished
composeEntry.ImageSize = compose.ImageBuilds[0].Size
composeEntry.JobCreated = float64(queued.UnixNano()) / 1000000000
composeEntry.JobStarted = float64(started.UnixNano()) / 1000000000
composeEntry.JobFinished = float64(finished.UnixNano()) / 1000000000
case common.CFailed:
composeEntry.QueueStatus = common.IBFailed
composeEntry.JobCreated = float64(queued.UnixNano()) / 1000000000
composeEntry.JobStarted = float64(started.UnixNano()) / 1000000000
composeEntry.JobFinished = float64(finished.UnixNano()) / 1000000000
default:
panic("invalid compose state")
}
return &composeEntry
}
func sortComposeEntries(entries []*ComposeEntry) {
sort.Slice(entries, func(i, j int) bool {
return entries[i].ID.String() < entries[j].ID.String()
})
}