Documentation ¶
Overview ¶
Package jobs provides utilities to background job management to achieve simplicity.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Run ¶
Example ¶
package main import ( "context" jobspkg "github.com/adamluzsi/frameless/pkg/jobs" "log" "net/http" ) func main() { simpleJob := func(signal context.Context) error { <-signal.Done() // work until shutdown signal return signal.Err() } srv := http.Server{ Addr: "localhost:8080", Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusTeapot) }), } httpServerJob := jobspkg.WithShutdown(srv.ListenAndServe, srv.Shutdown) if err := jobspkg.Run(context.Background(), simpleJob, httpServerJob); err != nil { log.Println("ERROR", err.Error()) } }
Output:
Types ¶
type Job ¶
Job is the basic unit of jobs package, that represents an executable work.
Job at its core, is nothing more than a synchronous function. Working with synchronous functions removes the complexity of thinking about how to run your application. Your components become more stateless and focus on the domain rather than the lifecycle management. This less stateful approach can help to make testing your Job also easier.
func WithRepeat ¶ added in v0.104.0
WithRepeat will keep repeating a given Job until shutdown is signaled. It is most suitable for Job(s) meant to be short-lived and executed continuously until the shutdown signal.
Example ¶
package main import ( "context" "github.com/adamluzsi/frameless/pkg/jobs" "github.com/adamluzsi/frameless/pkg/jobs/schedule" "log" "time" ) func main() { job := jobs.WithRepeat(schedule.Interval(time.Second), func(ctx context.Context) error { // I'm a short-lived job, and prefer to be constantly executed, // Repeat will keep repeating me every second until shutdown is signaled. return nil }) ctx, cancel := context.WithTimeout(context.Background(), time.Hour) defer cancel() if err := job(ctx); err != nil { log.Println("ERROR", err.Error()) } }
Output:
func WithShutdown ¶ added in v0.104.0
func WithShutdown[StartFn, StopFn genericJob](start StartFn, stop StopFn) Job
WithShutdown will combine the start and stop/shutdown function into a single Job function. It supports a graceful shutdown period; upon reaching the deadline, it will cancel the context passed to the shutdown function. WithShutdown makes it easy to use components with graceful shutdown support as a Job, such as the http.Server.
jobs.JobWithShutdown(srv.ListenAndServe, srv.Shutdown)
Example ¶
package main import ( "context" "github.com/adamluzsi/frameless/pkg/jobs" "log" "net/http" ) func main() { srv := http.Server{ Addr: "localhost:8080", Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusTeapot) }), } httpServerJob := jobs.WithShutdown(srv.ListenAndServe, srv.Shutdown) _ = httpServerJob ctx, cancel := context.WithCancel(context.Background()) // listen to a cancellation signal and then call the cancel func // or use ShutdownManager. _ = cancel if err := httpServerJob(ctx); err != nil { log.Println("ERROR", err.Error()) } }
Output:
type Manager ¶ added in v0.104.0
type Manager struct { // Jobs is the list of background job that you wish to run concurrently. // They will act as a unit, if any of them fail with an error, // other jobs will be notified to shut down. Jobs []Job // Signals is an [OPTIONAL] parameter // where you can define what signals you want to consider as a shutdown signal. // If not defined, then the default signal values are INT, HUB and TERM. Signals []os.Signal }
Manager helps to manage concurrent background Jobs in your main. Each Job will run in its own goroutine. If any of the Job encounters a failure, the other jobs will receive a cancellation signal.
Example ¶
package main import ( "context" jobspkg "github.com/adamluzsi/frameless/pkg/jobs" "log" "net/http" ) func main() { simpleJob := func(signal context.Context) error { <-signal.Done() // work until shutdown signal return signal.Err() } srv := http.Server{ Addr: "localhost:8080", Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusTeapot) }), } httpServerJob := jobspkg.WithShutdown(srv.ListenAndServe, srv.Shutdown) sm := jobspkg.Manager{ Jobs: []jobspkg.Job{ // each Job will run on its own goroutine. simpleJob, httpServerJob, }, } if err := sm.Run(context.Background()); err != nil { log.Println("ERROR", err.Error()) } }
Output: