Documentation
¶
Overview ¶
Package progress reports a workflow's progress as a stream of events over a tree of workflows, batches and activities, carried through the context; see README.md.
Example (Lazy) ¶
Example_lazy shows a workflow that reports nothing until it has something to report.
package main
import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/rs/zerolog"
"github.com/greenmaskio/arcave/pkg/progress"
"github.com/greenmaskio/arcave/pkg/progress/renderer"
)
// The kinds a consumer declares.
const (
kindAlbumPublish progress.Kind = "album.publish"
kindAlbumExtras progress.Kind = "album.extras"
kindAlbumRollback progress.Kind = "album.rollback"
)
type photo struct {
name string
size int64
}
type exampleClock struct {
now time.Time
}
func (c *exampleClock) Now() time.Time {
return c.now
}
func (c *exampleClock) advance(d time.Duration) {
c.now = c.now.Add(d)
}
var start = time.Date(2026, 10, 29, 12, 0, 0, 0, time.UTC)
func main() {
var (
clock = &exampleClock{now: start}
tracker = progress.New(renderer.NewLog(newExampleLogger()), clock.Now)
)
ctx, root := tracker.Begin(context.Background(), progress.Step{Kind: kindAlbumPublish, Name: "summer"})
_, extras := progress.Begin(ctx, progress.Step{Kind: kindAlbumExtras})
extras.End(nil)
fmt.Println("an empty workflow that succeeded reported nothing")
clock.advance(time.Second)
_, rollback := progress.Begin(ctx, progress.Step{Kind: kindAlbumRollback})
rollback.End(errors.New("queue unreachable"))
root.End(errors.New("rollback: queue unreachable"))
}
func newExampleLogger() zerolog.Logger {
return zerolog.New(zerolog.ConsoleWriter{
Out: os.Stdout,
NoColor: true,
PartsExclude: []string{zerolog.TimestampFieldName},
})
}
Output: an empty workflow that succeeded reported nothing INF album.publish[summer] status=started INF album.publish[summer]/album.rollback status=started ERR album.publish[summer]/album.rollback error="queue unreachable" elapsed=0s status=failed ERR album.publish[summer] elapsed=1s status=failed
Example (Publish) ¶
Example_publish runs a workflow to success through the log reporter.
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"time"
"github.com/rs/zerolog"
"github.com/greenmaskio/arcave/pkg/progress"
"github.com/greenmaskio/arcave/pkg/progress/renderer"
)
// The kinds a consumer declares.
const (
kindAlbumPublish progress.Kind = "album.publish"
kindAlbumBackup progress.Kind = "album.backup"
kindAlbumExport progress.Kind = "album.export"
kindAlbumExtras progress.Kind = "album.extras"
kindAlbumRollback progress.Kind = "album.rollback"
kindPhotoResize progress.Kind = "photo.resize"
kindCDNUpload progress.Kind = "cdn.upload"
kindCDNPurge progress.Kind = "cdn.purge"
kindQueuePause progress.Kind = "queue.pause"
kindQueueResume progress.Kind = "queue.resume"
kindManifestWrite progress.Kind = "manifest.write"
kindPhoto progress.Kind = "photo"
kindFile progress.Kind = "file"
resourceWorker progress.ResourceKind = "worker"
)
var errDenied = errors.New("403 forbidden")
type photo struct {
name string
size int64
}
// publisher stands for a consumer's service; its clock is the example's, moved by hand.
type publisher struct {
clock *exampleClock
}
type exampleClock struct {
now time.Time
}
func (c *exampleClock) Now() time.Time {
return c.now
}
func (c *exampleClock) advance(d time.Duration) {
c.now = c.now.Add(d)
}
var (
summer = []photo{{"beach.jpg", 2_100_000}, {"dunes.jpg", 1_800_000}, {"sunset.jpg", 2_400_000}}
start = time.Date(2026, 10, 29, 12, 0, 0, 0, time.UTC)
)
func main() {
var (
clock = &exampleClock{now: start}
logger = newExampleLogger()
tracker = progress.New(renderer.NewLog(logger), clock.Now)
p = publisher{clock: clock}
)
err := p.publish(context.Background(), tracker, summer[:2])
fmt.Println("err:", err)
}
func (p publisher) publish(ctx context.Context, tracker *progress.Tracker, photos []photo) (err error) {
ctx, root := tracker.Begin(ctx, progress.Step{Kind: kindAlbumPublish, Name: "summer"})
defer func() { root.End(err) }()
if err = p.pauseQueue(ctx); err != nil {
return err
}
if err = p.exportAlbum(ctx, photos); err != nil {
return err
}
if _, err = p.uploadFiles(ctx, photos, -1); err != nil {
return err
}
return p.resumeQueue(ctx)
}
func (p publisher) backup(ctx context.Context, tracker *progress.Tracker, photos []photo, failAt int) (err error) {
ctx, root := tracker.Begin(ctx, progress.Step{Kind: kindAlbumBackup, Name: "summer"})
defer func() { root.End(err) }()
if err = p.exportAlbum(ctx, photos); err != nil {
return err
}
uploaded, err := p.uploadFiles(ctx, append(photos, photo{"raw.dng", 900_000}, photo{"cover.jpg", 300_000}, photo{"map.png", 100_000}), failAt)
if err != nil {
if rollbackErr := p.rollbackAlbum(ctx, uploaded); rollbackErr != nil {
return errors.Join(err, rollbackErr)
}
return err
}
return nil
}
// pauseQueue is a summary activity: one call handles every worker, each a resource.
func (p publisher) pauseQueue(ctx context.Context) error {
_, step := progress.Run(ctx, progress.Step{Kind: kindQueuePause}, progress.MeasureResources)
for _, worker := range []string{"worker-1", "worker-2"} {
resource := progress.Resource{Kind: resourceWorker, ID: worker}
step.UpdateResource(resource, "Draining")
p.clock.advance(200 * time.Millisecond)
step.SettleResource(resource, "Paused")
}
step.Done(progress.Sample{})
return nil
}
func (p publisher) resumeQueue(ctx context.Context) error {
_, step := progress.Run(ctx, progress.Step{Kind: kindQueueResume}, progress.MeasureResources)
p.clock.advance(100 * time.Millisecond)
step.SettleResource(progress.Resource{Kind: resourceWorker, ID: "worker-1"}, "Running")
step.Done(progress.Sample{})
return nil
}
// exportAlbum is a shared workflow: it does not know which root it runs under.
func (p publisher) exportAlbum(ctx context.Context, photos []photo) (err error) {
ctx, step := progress.Begin(ctx, progress.Step{Kind: kindAlbumExport})
defer func() { step.End(err) }()
step.Warn("2 raw files skipped")
batchCtx, batch := progress.Batch(ctx, progress.Step{Kind: kindPhotoResize}, len(photos))
for _, item := range photos {
if err = p.resizePhoto(batchCtx, item); err != nil {
batch.End(err)
return err
}
}
batch.End(nil)
_, extras := progress.Begin(ctx, progress.Step{Kind: kindAlbumExtras})
extras.End(nil)
_, manifest := progress.Run(ctx, progress.Step{Kind: kindManifestWrite})
p.clock.advance(50 * time.Millisecond)
manifest.Done(progress.Sample{})
return nil
}
func (p publisher) resizePhoto(ctx context.Context, item photo) (err error) {
ctx, step := progress.Run(ctx, progress.Step{Kind: kindPhoto, ID: item.name}, progress.MeasureBytes)
defer func() { step.End(err) }()
resized, err := p.resize(ctx, bytes.NewReader(make([]byte, item.size)), item.size)
if err != nil {
return err
}
step.Done(progress.Sample{Bytes: &progress.Bytes{Done: item.size, Total: item.size, Stored: resized}})
return nil
}
// resize is a source deep in the call: it knows ctx and nothing of the tree.
func (p publisher) resize(ctx context.Context, src io.Reader, size int64) (int64, error) {
src = progress.NewReader(ctx, src, size)
buf := make([]byte, 1_000_000)
for {
p.clock.advance(150 * time.Millisecond)
if _, err := src.Read(buf); errors.Is(err, io.EOF) {
return size / 5, nil
} else if err != nil {
return 0, err
}
}
}
// uploadFiles is a batch whose objects report what the CDN counts itself; the first failure
// cancels the rest, as an errgroup would.
func (p publisher) uploadFiles(ctx context.Context, photos []photo, failAt int) (uploaded []photo, err error) {
ctx, batch := progress.Batch(ctx, progress.Step{Kind: kindCDNUpload}, len(photos))
defer func() { batch.End(err) }()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
for i, item := range photos {
uploadErr := p.uploadFile(ctx, item, i == failAt)
if uploadErr == nil {
uploaded = append(uploaded, item)
continue
}
if err == nil {
err = uploadErr
cancel()
}
}
return uploaded, err
}
func (p publisher) uploadFile(ctx context.Context, item photo, deny bool) (err error) {
_, step := progress.Run(ctx, progress.Step{Kind: kindFile, ID: item.name}, progress.MeasureBytes)
defer func() { step.End(err) }()
if err = ctx.Err(); err != nil {
return err
}
sampler := progress.NewSampler(step, progress.Sample{Bytes: &progress.Bytes{}})
sampler.Update(func(sample *progress.Sample) {
sample.Bytes.Done = item.size / 2
})
p.clock.advance(250 * time.Millisecond)
if deny {
step.Fail("unable to upload", errDenied)
return errDenied
}
sampler.Update(func(sample *progress.Sample) {
sample.Bytes.Done = item.size * 3 / 4
sample.Bytes.Total = item.size
})
p.clock.advance(100 * time.Millisecond)
sampler.Update(func(sample *progress.Sample) {
sample.Bytes.Done = item.size * 7 / 8
})
step.Done(progress.Sample{Bytes: &progress.Bytes{Done: item.size, Total: item.size}})
return nil
}
// rollbackAlbum purges what was uploaded; a purge that fails only warns.
func (p publisher) rollbackAlbum(ctx context.Context, uploaded []photo) (err error) {
ctx, step := progress.Begin(ctx, progress.Step{Kind: kindAlbumRollback})
defer func() { step.End(err) }()
ctx, batch := progress.Batch(ctx, progress.Step{Kind: kindCDNPurge}, len(uploaded))
defer func() { batch.End(err) }()
for _, item := range uploaded {
_, purge := progress.Run(ctx, progress.Step{Kind: kindFile, ID: item.name})
p.clock.advance(50 * time.Millisecond)
purge.Warn("purge failed, the file stays on the CDN: edge timeout")
purge.Done(progress.Sample{})
}
return nil
}
func newExampleLogger() zerolog.Logger {
return zerolog.New(zerolog.ConsoleWriter{
Out: os.Stdout,
NoColor: true,
PartsExclude: []string{zerolog.TimestampFieldName},
})
}
Output: INF album.publish[summer] status=started INF album.publish[summer]/queue.pause status=started DBG album.publish[summer]/queue.pause msg=Draining resource=worker:worker-1 status=in_progress INF album.publish[summer]/queue.pause msg=Paused resource=worker:worker-1 status=in_progress DBG album.publish[summer]/queue.pause msg=Draining resource=worker:worker-2 status=in_progress INF album.publish[summer]/queue.pause msg=Paused resource=worker:worker-2 status=in_progress INF album.publish[summer]/queue.pause elapsed=400ms status=succeeded INF album.publish[summer]/album.export status=started WRN album.publish[summer]/album.export msg="2 raw files skipped" status=warned INF album.publish[summer]/album.export/photo.resize items=0/2 status=started INF album.publish[summer]/album.export/photo.resize/photo[beach.jpg] status=started INF album.publish[summer]/album.export/photo.resize/photo[beach.jpg] bytes=2.1MB/2.1MB elapsed=600ms status=succeeded stored=420KB INF album.publish[summer]/album.export/photo.resize items=1/2 status=in_progress INF album.publish[summer]/album.export/photo.resize/photo[dunes.jpg] status=started INF album.publish[summer]/album.export/photo.resize/photo[dunes.jpg] bytes=1.8MB/1.8MB elapsed=450ms status=succeeded stored=360KB INF album.publish[summer]/album.export/photo.resize items=2/2 status=in_progress INF album.publish[summer]/album.export/photo.resize elapsed=1.05s items=2/2 status=succeeded INF album.publish[summer]/album.export/manifest.write status=started INF album.publish[summer]/album.export/manifest.write elapsed=50ms status=succeeded INF album.publish[summer]/album.export elapsed=1.1s status=succeeded INF album.publish[summer]/cdn.upload items=0/2 status=started INF album.publish[summer]/cdn.upload/file[beach.jpg] status=started INF album.publish[summer]/cdn.upload/file[beach.jpg] bytes=2.1MB/2.1MB elapsed=350ms status=succeeded INF album.publish[summer]/cdn.upload items=1/2 status=in_progress INF album.publish[summer]/cdn.upload/file[dunes.jpg] status=started INF album.publish[summer]/cdn.upload/file[dunes.jpg] bytes=1.8MB/1.8MB elapsed=350ms status=succeeded INF album.publish[summer]/cdn.upload items=2/2 status=in_progress INF album.publish[summer]/cdn.upload elapsed=700ms items=2/2 status=succeeded INF album.publish[summer]/queue.resume status=started INF album.publish[summer]/queue.resume msg=Running resource=worker:worker-1 status=in_progress INF album.publish[summer]/queue.resume elapsed=100ms status=succeeded INF album.publish[summer] elapsed=2.3s status=succeeded err: <nil>
Example (PublishFailure) ¶
Example_publishFailure runs the same export under another root and fails an upload, through the JSON reporter: the error is on the file alone, the rest of the batch is cancelled.
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"time"
"github.com/greenmaskio/arcave/pkg/progress"
"github.com/greenmaskio/arcave/pkg/progress/renderer"
)
// The kinds a consumer declares.
const (
kindAlbumPublish progress.Kind = "album.publish"
kindAlbumBackup progress.Kind = "album.backup"
kindAlbumExport progress.Kind = "album.export"
kindAlbumExtras progress.Kind = "album.extras"
kindAlbumRollback progress.Kind = "album.rollback"
kindPhotoResize progress.Kind = "photo.resize"
kindCDNUpload progress.Kind = "cdn.upload"
kindCDNPurge progress.Kind = "cdn.purge"
kindQueuePause progress.Kind = "queue.pause"
kindQueueResume progress.Kind = "queue.resume"
kindManifestWrite progress.Kind = "manifest.write"
kindPhoto progress.Kind = "photo"
kindFile progress.Kind = "file"
resourceWorker progress.ResourceKind = "worker"
)
var errDenied = errors.New("403 forbidden")
type photo struct {
name string
size int64
}
// publisher stands for a consumer's service; its clock is the example's, moved by hand.
type publisher struct {
clock *exampleClock
}
type exampleClock struct {
now time.Time
}
func (c *exampleClock) Now() time.Time {
return c.now
}
func (c *exampleClock) advance(d time.Duration) {
c.now = c.now.Add(d)
}
var (
summer = []photo{{"beach.jpg", 2_100_000}, {"dunes.jpg", 1_800_000}, {"sunset.jpg", 2_400_000}}
start = time.Date(2026, 10, 29, 12, 0, 0, 0, time.UTC)
)
func main() {
var (
clock = &exampleClock{now: start}
tracker = progress.New(renderer.NewJSON(os.Stdout), clock.Now)
p = publisher{clock: clock}
)
err := p.backup(context.Background(), tracker, summer[:1], 1)
fmt.Println("err:", err)
}
func (p publisher) publish(ctx context.Context, tracker *progress.Tracker, photos []photo) (err error) {
ctx, root := tracker.Begin(ctx, progress.Step{Kind: kindAlbumPublish, Name: "summer"})
defer func() { root.End(err) }()
if err = p.pauseQueue(ctx); err != nil {
return err
}
if err = p.exportAlbum(ctx, photos); err != nil {
return err
}
if _, err = p.uploadFiles(ctx, photos, -1); err != nil {
return err
}
return p.resumeQueue(ctx)
}
func (p publisher) backup(ctx context.Context, tracker *progress.Tracker, photos []photo, failAt int) (err error) {
ctx, root := tracker.Begin(ctx, progress.Step{Kind: kindAlbumBackup, Name: "summer"})
defer func() { root.End(err) }()
if err = p.exportAlbum(ctx, photos); err != nil {
return err
}
uploaded, err := p.uploadFiles(ctx, append(photos, photo{"raw.dng", 900_000}, photo{"cover.jpg", 300_000}, photo{"map.png", 100_000}), failAt)
if err != nil {
if rollbackErr := p.rollbackAlbum(ctx, uploaded); rollbackErr != nil {
return errors.Join(err, rollbackErr)
}
return err
}
return nil
}
// pauseQueue is a summary activity: one call handles every worker, each a resource.
func (p publisher) pauseQueue(ctx context.Context) error {
_, step := progress.Run(ctx, progress.Step{Kind: kindQueuePause}, progress.MeasureResources)
for _, worker := range []string{"worker-1", "worker-2"} {
resource := progress.Resource{Kind: resourceWorker, ID: worker}
step.UpdateResource(resource, "Draining")
p.clock.advance(200 * time.Millisecond)
step.SettleResource(resource, "Paused")
}
step.Done(progress.Sample{})
return nil
}
func (p publisher) resumeQueue(ctx context.Context) error {
_, step := progress.Run(ctx, progress.Step{Kind: kindQueueResume}, progress.MeasureResources)
p.clock.advance(100 * time.Millisecond)
step.SettleResource(progress.Resource{Kind: resourceWorker, ID: "worker-1"}, "Running")
step.Done(progress.Sample{})
return nil
}
// exportAlbum is a shared workflow: it does not know which root it runs under.
func (p publisher) exportAlbum(ctx context.Context, photos []photo) (err error) {
ctx, step := progress.Begin(ctx, progress.Step{Kind: kindAlbumExport})
defer func() { step.End(err) }()
step.Warn("2 raw files skipped")
batchCtx, batch := progress.Batch(ctx, progress.Step{Kind: kindPhotoResize}, len(photos))
for _, item := range photos {
if err = p.resizePhoto(batchCtx, item); err != nil {
batch.End(err)
return err
}
}
batch.End(nil)
_, extras := progress.Begin(ctx, progress.Step{Kind: kindAlbumExtras})
extras.End(nil)
_, manifest := progress.Run(ctx, progress.Step{Kind: kindManifestWrite})
p.clock.advance(50 * time.Millisecond)
manifest.Done(progress.Sample{})
return nil
}
func (p publisher) resizePhoto(ctx context.Context, item photo) (err error) {
ctx, step := progress.Run(ctx, progress.Step{Kind: kindPhoto, ID: item.name}, progress.MeasureBytes)
defer func() { step.End(err) }()
resized, err := p.resize(ctx, bytes.NewReader(make([]byte, item.size)), item.size)
if err != nil {
return err
}
step.Done(progress.Sample{Bytes: &progress.Bytes{Done: item.size, Total: item.size, Stored: resized}})
return nil
}
// resize is a source deep in the call: it knows ctx and nothing of the tree.
func (p publisher) resize(ctx context.Context, src io.Reader, size int64) (int64, error) {
src = progress.NewReader(ctx, src, size)
buf := make([]byte, 1_000_000)
for {
p.clock.advance(150 * time.Millisecond)
if _, err := src.Read(buf); errors.Is(err, io.EOF) {
return size / 5, nil
} else if err != nil {
return 0, err
}
}
}
// uploadFiles is a batch whose objects report what the CDN counts itself; the first failure
// cancels the rest, as an errgroup would.
func (p publisher) uploadFiles(ctx context.Context, photos []photo, failAt int) (uploaded []photo, err error) {
ctx, batch := progress.Batch(ctx, progress.Step{Kind: kindCDNUpload}, len(photos))
defer func() { batch.End(err) }()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
for i, item := range photos {
uploadErr := p.uploadFile(ctx, item, i == failAt)
if uploadErr == nil {
uploaded = append(uploaded, item)
continue
}
if err == nil {
err = uploadErr
cancel()
}
}
return uploaded, err
}
func (p publisher) uploadFile(ctx context.Context, item photo, deny bool) (err error) {
_, step := progress.Run(ctx, progress.Step{Kind: kindFile, ID: item.name}, progress.MeasureBytes)
defer func() { step.End(err) }()
if err = ctx.Err(); err != nil {
return err
}
sampler := progress.NewSampler(step, progress.Sample{Bytes: &progress.Bytes{}})
sampler.Update(func(sample *progress.Sample) {
sample.Bytes.Done = item.size / 2
})
p.clock.advance(250 * time.Millisecond)
if deny {
step.Fail("unable to upload", errDenied)
return errDenied
}
sampler.Update(func(sample *progress.Sample) {
sample.Bytes.Done = item.size * 3 / 4
sample.Bytes.Total = item.size
})
p.clock.advance(100 * time.Millisecond)
sampler.Update(func(sample *progress.Sample) {
sample.Bytes.Done = item.size * 7 / 8
})
step.Done(progress.Sample{Bytes: &progress.Bytes{Done: item.size, Total: item.size}})
return nil
}
// rollbackAlbum purges what was uploaded; a purge that fails only warns.
func (p publisher) rollbackAlbum(ctx context.Context, uploaded []photo) (err error) {
ctx, step := progress.Begin(ctx, progress.Step{Kind: kindAlbumRollback})
defer func() { step.End(err) }()
ctx, batch := progress.Batch(ctx, progress.Step{Kind: kindCDNPurge}, len(uploaded))
defer func() { batch.End(err) }()
for _, item := range uploaded {
_, purge := progress.Run(ctx, progress.Step{Kind: kindFile, ID: item.name})
p.clock.advance(50 * time.Millisecond)
purge.Warn("purge failed, the file stays on the CDN: edge timeout")
purge.Done(progress.Sample{})
}
return nil
}
Output: {"node":1,"status":"started","open":{"step":{"kind":"album.backup","type":"workflow","name":"summer"}},"sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:00Z"} {"node":2,"status":"started","open":{"parent":1,"step":{"kind":"album.export","type":"workflow"}},"sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:00Z"} {"node":2,"status":"warned","msg":"2 raw files skipped","sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:00Z"} {"node":3,"status":"started","open":{"parent":2,"step":{"kind":"photo.resize","type":"workflow"},"measures":["items"]},"sample":{"elapsed_ns":0,"items":{"done":0,"total":1}},"at":"2026-10-29T12:00:00Z"} {"node":4,"status":"started","open":{"parent":3,"step":{"kind":"photo","type":"activity","id":"beach.jpg"},"measures":["bytes"]},"sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:00Z"} {"node":4,"status":"in_progress","sample":{"elapsed_ns":150000000,"bytes":{"done":1000000,"total":2100000}},"at":"2026-10-29T12:00:00.15Z"} {"node":4,"status":"in_progress","sample":{"elapsed_ns":450000000,"bytes":{"done":2100000,"total":2100000}},"at":"2026-10-29T12:00:00.45Z"} {"node":4,"status":"succeeded","sample":{"elapsed_ns":600000000,"bytes":{"done":2100000,"total":2100000,"stored":420000}},"at":"2026-10-29T12:00:00.6Z"} {"node":3,"status":"in_progress","sample":{"elapsed_ns":600000000,"items":{"done":1,"total":1}},"at":"2026-10-29T12:00:00.6Z"} {"node":3,"status":"succeeded","sample":{"elapsed_ns":600000000,"items":{"done":1,"total":1}},"at":"2026-10-29T12:00:00.6Z"} {"node":5,"status":"started","open":{"parent":2,"step":{"kind":"manifest.write","type":"activity"}},"sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:00.6Z"} {"node":5,"status":"succeeded","sample":{"elapsed_ns":50000000},"at":"2026-10-29T12:00:00.65Z"} {"node":2,"status":"succeeded","sample":{"elapsed_ns":650000000},"at":"2026-10-29T12:00:00.65Z"} {"node":6,"status":"started","open":{"parent":1,"step":{"kind":"cdn.upload","type":"workflow"},"measures":["items"]},"sample":{"elapsed_ns":0,"items":{"done":0,"total":4}},"at":"2026-10-29T12:00:00.65Z"} {"node":7,"status":"started","open":{"parent":6,"step":{"kind":"file","type":"activity","id":"beach.jpg"},"measures":["bytes"]},"sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:00.65Z"} {"node":7,"status":"in_progress","sample":{"elapsed_ns":0,"bytes":{"done":1050000}},"at":"2026-10-29T12:00:00.65Z"} {"node":7,"status":"in_progress","sample":{"elapsed_ns":250000000,"bytes":{"done":1575000,"total":2100000}},"at":"2026-10-29T12:00:00.9Z"} {"node":7,"status":"succeeded","sample":{"elapsed_ns":350000000,"bytes":{"done":2100000,"total":2100000}},"at":"2026-10-29T12:00:01Z"} {"node":6,"status":"in_progress","sample":{"elapsed_ns":350000000,"items":{"done":1,"total":4}},"at":"2026-10-29T12:00:01Z"} {"node":8,"status":"started","open":{"parent":6,"step":{"kind":"file","type":"activity","id":"raw.dng"},"measures":["bytes"]},"sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:01Z"} {"node":8,"status":"in_progress","sample":{"elapsed_ns":0,"bytes":{"done":450000}},"at":"2026-10-29T12:00:01Z"} {"node":8,"status":"failed","msg":"unable to upload","error":"403 forbidden","sample":{"elapsed_ns":250000000,"bytes":{"done":450000}},"at":"2026-10-29T12:00:01.25Z"} {"node":6,"status":"in_progress","sample":{"elapsed_ns":600000000,"items":{"done":1,"failed":1,"total":4}},"at":"2026-10-29T12:00:01.25Z"} {"node":9,"status":"started","open":{"parent":6,"step":{"kind":"file","type":"activity","id":"cover.jpg"},"measures":["bytes"]},"sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:01.25Z"} {"node":9,"status":"cancelled","sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:01.25Z"} {"node":6,"status":"in_progress","sample":{"elapsed_ns":600000000,"items":{"done":1,"failed":1,"cancelled":1,"total":4}},"at":"2026-10-29T12:00:01.25Z"} {"node":10,"status":"started","open":{"parent":6,"step":{"kind":"file","type":"activity","id":"map.png"},"measures":["bytes"]},"sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:01.25Z"} {"node":10,"status":"cancelled","sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:01.25Z"} {"node":6,"status":"in_progress","sample":{"elapsed_ns":600000000,"items":{"done":1,"failed":1,"cancelled":2,"total":4}},"at":"2026-10-29T12:00:01.25Z"} {"node":6,"status":"failed","sample":{"elapsed_ns":600000000,"items":{"done":1,"failed":1,"cancelled":2,"total":4}},"at":"2026-10-29T12:00:01.25Z"} {"node":11,"status":"started","open":{"parent":1,"step":{"kind":"album.rollback","type":"workflow"}},"sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:01.25Z"} {"node":12,"status":"started","open":{"parent":11,"step":{"kind":"cdn.purge","type":"workflow"},"measures":["items"]},"sample":{"elapsed_ns":0,"items":{"done":0,"total":1}},"at":"2026-10-29T12:00:01.25Z"} {"node":13,"status":"started","open":{"parent":12,"step":{"kind":"file","type":"activity","id":"beach.jpg"}},"sample":{"elapsed_ns":0},"at":"2026-10-29T12:00:01.25Z"} {"node":13,"status":"warned","msg":"purge failed, the file stays on the CDN: edge timeout","sample":{"elapsed_ns":50000000},"at":"2026-10-29T12:00:01.3Z"} {"node":13,"status":"succeeded","sample":{"elapsed_ns":50000000},"at":"2026-10-29T12:00:01.3Z"} {"node":12,"status":"in_progress","sample":{"elapsed_ns":50000000,"items":{"done":1,"total":1}},"at":"2026-10-29T12:00:01.3Z"} {"node":12,"status":"succeeded","sample":{"elapsed_ns":50000000,"items":{"done":1,"total":1}},"at":"2026-10-29T12:00:01.3Z"} {"node":11,"status":"succeeded","sample":{"elapsed_ns":50000000},"at":"2026-10-29T12:00:01.3Z"} {"node":1,"status":"failed","sample":{"elapsed_ns":1300000000},"at":"2026-10-29T12:00:01.3Z"} err: 403 forbidden
Index ¶
- func NewReader(ctx context.Context, r io.Reader, total int64) io.Reader
- func NewWriter(ctx context.Context, w io.Writer, total int64) io.Writer
- func WithStep(ctx context.Context, reporter *StepReporter) context.Context
- func WithTracker(ctx context.Context, t *Tracker) context.Context
- type Bytes
- type Event
- type Items
- type Kind
- type Measure
- type Open
- type Renderer
- type Resource
- type ResourceKind
- type Sample
- type Sampler
- type Status
- type Step
- type StepID
- type StepReporter
- func Batch(ctx context.Context, step Step, total int) (context.Context, *StepReporter)
- func Begin(ctx context.Context, step Step) (context.Context, *StepReporter)
- func Current(ctx context.Context) *StepReporter
- func Run(ctx context.Context, step Step, measures ...Measure) (context.Context, *StepReporter)
- func (r *StepReporter) Done(sample Sample)
- func (r *StepReporter) End(err error)
- func (r *StepReporter) Fail(msg string, err error)
- func (r *StepReporter) IsMute() bool
- func (r *StepReporter) Progress(sample Sample)
- func (r *StepReporter) SettleResource(resource Resource, msg string)
- func (r *StepReporter) Update(msg string)
- func (r *StepReporter) UpdateResource(resource Resource, msg string)
- func (r *StepReporter) Warn(msg string)
- func (r *StepReporter) WarnInResult(msg string)
- func (r *StepReporter) WarnResource(resource Resource, msg string)
- type StepType
- type Tracker
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewReader ¶
NewReader counts the bytes read from r on the step of ctx, out of total, 0 if unknown. It returns r itself when that step does not report bytes.
func NewWriter ¶
NewWriter counts the bytes written to w on the step of ctx, out of total, 0 if unknown. It returns w itself when that step does not report bytes.
Types ¶
type Bytes ¶
type Bytes struct {
Done int64 `json:"done"`
Total int64 `json:"total,omitempty"`
Stored int64 `json:"stored,omitempty"`
Reused int64 `json:"reused,omitempty"`
}
Bytes counts bytes, accumulated; a Total of 0 is unknown.
type Event ¶
type Event struct {
// StepID is the step's id, "node" on the wire.
StepID StepID `json:"node"`
Status Status `json:"status"`
// Open is set on started only.
Open *Open `json:"open,omitempty"`
Msg string `json:"msg,omitempty"`
// Error is set on failed only, on the step the error arose on.
Error string `json:"error,omitempty"`
Resource *Resource `json:"resource,omitempty"`
// Settled marks an in_progress event of a resource that has reached its outcome, while the
// step goes on; one without it is the resource still under way.
Settled bool `json:"settled,omitempty"`
// InResult marks a warning the operation's result repeats: a renderer beside the result may
// leave it out.
InResult bool `json:"in_result,omitempty"`
Sample Sample `json:"sample"`
At time.Time `json:"at"`
}
Event is one event of a progress stream, a full snapshot of its step.
type Items ¶
type Items struct {
Done int64 `json:"done"`
Failed int64 `json:"failed,omitempty"`
Cancelled int64 `json:"cancelled,omitempty"`
Total int64 `json:"total,omitempty"`
}
Items counts items, accumulated: Done only the succeeded; a Total of 0 is unknown.
type Kind ¶
type Kind string
Kind names a step: "<namespace>.<action>" for an action, a bare noun for an object. The consumer declares its kinds.
type Measure ¶
type Measure string
Measure names what a step reports, and so which widget renders it. A step declaring none is a spinner: a status and a message.
const ( // MeasureBytes is a byte bar, Sample.Bytes: a copy or a transfer of one object // (2.1MB/4.0MB). Without a Total it is a running count. MeasureBytes Measure = "bytes" // MeasureItems is a counter, Sample.Items: a batch's finished children (3/5 failed=1). MeasureItems Measure = "items" // MeasureResources is a line per resource, Event.Resource: the set one call // handles, as compose's per-container rows (worker-1 Paused). MeasureResources Measure = "resources" )
type Open ¶
type Open struct {
// Parent is 0 for the root.
Parent StepID `json:"parent,omitempty"`
Step Step `json:"step"`
Measures []Measure `json:"measures,omitempty"`
}
Open describes a step once, on its started event: where it is and how to render it.
type Renderer ¶
type Renderer interface {
Render(evt Event)
}
Renderer renders a progress stream. A Tracker calls it under its own lock, one event at a time.
type Resource ¶
type Resource struct {
Kind ResourceKind `json:"kind"`
ID string `json:"id"`
Name string `json:"name,omitempty"`
}
Resource is one resource a summary activity handles.
type ResourceKind ¶
type ResourceKind string
ResourceKind names what a resource of a summary activity is. The consumer declares it.
type Sample ¶
type Sample struct {
Elapsed time.Duration `json:"elapsed_ns"`
Bytes *Bytes `json:"bytes,omitempty"`
Items *Items `json:"items,omitempty"`
}
Sample is a step's accumulated state at an event.
type Sampler ¶
type Sampler struct {
// contains filtered or unexported fields
}
Sampler accumulates a step's sample for a source deep in the call and sends it at most once per the tracker's interval. The last value is not flushed: the step's Done carries the total.
func NewSampler ¶
func NewSampler(topic *StepReporter, template Sample) *Sampler
NewSampler returns a Sampler for topic, its sample shaped by template and starting from its values. It is mute when topic is, or when topic does not declare a measure template sets.
type Status ¶
type Status string
Status is the state an event reports a step in.
func (Status) IsTerminal ¶
IsTerminal reports whether no event follows this one on its step.
type Step ¶
type Step struct {
Kind Kind `json:"kind"`
Type StepType `json:"type"`
// ID is the object's resource key under a batch; empty elsewhere.
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
Step says what a step does.
type StepReporter ¶
type StepReporter struct {
// contains filtered or unexported fields
}
StepReporter is the handle through which one step reports its progress. Its methods are safe for concurrent use.
func Batch ¶
Batch opens a loop over total objects under the step of ctx, at once; it counts its children's outcomes itself. A total of 0 is unknown.
func Begin ¶
Begin opens a workflow under the step of ctx, or the root of the tracker ctx carries when it carries no step. It reports nothing until its first child or Warn.
func Current ¶
func Current(ctx context.Context) *StepReporter
Current returns the step of ctx, a mute one when ctx carries none.
func Run ¶
Run opens an activity under the step of ctx, at once, reporting the measures it declares.
func (*StepReporter) Done ¶
func (r *StepReporter) Done(sample Sample)
Done ends the step succeeded, merging sample as Progress does. A lazy workflow never opened reports nothing, though a batch above it counts it.
func (*StepReporter) End ¶
func (r *StepReporter) End(err error)
End ends the step: Done with no sample for a nil err, Fail otherwise. Deferred, it takes a closure over a named error — defer func() { step.End(err) }() — or it reports success.
func (*StepReporter) Fail ¶
func (r *StepReporter) Fail(msg string, err error)
Fail ends the step failed, or cancelled when err is a context.Canceled or the cause the context it was opened on was cancelled with; a deadline is a failure. A workflow carries err only when none of its descendants failed: the error is reported where it arose.
func (*StepReporter) IsMute ¶
func (r *StepReporter) IsMute() bool
IsMute reports whether the step reports nothing: a context without a tracker carries one.
func (*StepReporter) Progress ¶
func (r *StepReporter) Progress(sample Sample)
Progress reports the step's accumulated sample; the fields it leaves nil keep their values. Like Update, it does not open a lazy workflow.
func (*StepReporter) SettleResource ¶
func (r *StepReporter) SettleResource(resource Resource, msg string)
SettleResource reports msg about one resource of a summary activity that has reached its outcome; the step goes on.
func (*StepReporter) Update ¶
func (r *StepReporter) Update(msg string)
Update reports msg as the step's current message; a lazy workflow keeps it unopened.
func (*StepReporter) UpdateResource ¶
func (r *StepReporter) UpdateResource(resource Resource, msg string)
UpdateResource reports msg about one resource of a summary activity, still under way.
func (*StepReporter) Warn ¶
func (r *StepReporter) Warn(msg string)
Warn reports a warning; it opens a lazy workflow.
func (*StepReporter) WarnInResult ¶
func (r *StepReporter) WarnInResult(msg string)
WarnInResult is Warn for a warning the operation's result repeats, marked InResult.
func (*StepReporter) WarnResource ¶
func (r *StepReporter) WarnResource(resource Resource, msg string)
WarnResource reports a warning about one resource of a summary activity.
type StepType ¶
type StepType string
StepType says whether a step is made of other steps or is one action.
Directories
¶
| Path | Synopsis |
|---|---|
|
Command examples runs a photo album's workflows through a progress renderer:
|
Command examples runs a photo album's workflows through a progress renderer: |
|
Package renderer holds progress.Renderers that render a stream as JSON lines or log records.
|
Package renderer holds progress.Renderers that render a stream as JSON lines or log records. |
|
internal/format
Package format renders what the renderers print alike.
|
Package format renders what the renderers print alike. |
|
tty
Package tty renders a progress stream as a live tree on a terminal, through Bubble Tea.
|
Package tty renders a progress stream as a live tree on a terminal, through Bubble Tea. |