background

package
v0.11.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package background runs an app's Go code while it isn't open, as the phone finds the time: syncing a feed overnight, uploading what waited for a network. BGTaskScheduler on iOS, WorkManager on Android; elsewhere Schedule reports errors.ErrUnsupported.

The system starts the app's process to run a task, without showing it, and main runs as ever: call Handle before App.Run, so that the handler is there by then.

background.Handle("sync", func(ctx context.Context) error {
	return syncFeed(ctx)
})
mygo.App.WhenReady(func() {
	go background.Schedule(context.Background(), "sync", background.Options{
		Every: 4 * time.Hour, Network: true,
	})
})

When a task runs is the system's choice: after Delay, around Every, with what it knows of the battery and of how much the user opens the app. iOS gives a task about 30 seconds, or minutes with Long; Android about 10 minutes. A handler stops when its context ends. A repeating task outlives the process and reboots until Cancel; one that runs once does until it succeeds.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cancel

func Cancel(ctx context.Context, name string) error

Cancel takes a task off the system's list.

func Handle

func Handle(name string, fn func(ctx context.Context) error)

Handle says what a task does, before App.Run. It runs on a goroutine of its own, off the main thread. A task whose handler returns an error runs again later.

func Schedule

func Schedule(ctx context.Context, name string, o Options) error

Schedule asks the system to run a task, in place of one of its name with other options: with the same ones, as an app does each time it starts, nothing changes, so that a task isn't put off again and again, nor stopped as it runs. Call it off the main goroutine, or with go.

Types

type Options

type Options struct {
	// Every repeats it, at least 15 minutes apart on either system. Without it
	// the task runs once.
	Every time.Duration
	// Delay is the least it waits before it runs the first time.
	Delay time.Duration
	// Network waits for a network; on iOS, for Long tasks only, as the system
	// decides when others run.
	Network bool
	// Charging waits for the phone to charge; on iOS, for Long tasks only.
	Charging bool
	// Long asks for minutes, which on iOS come rarely, overnight and charging,
	// rather than half a minute.
	Long bool
}

Options say when a task may run.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL