schedule

package module
v0.0.0-...-c28b0b2 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2021 License: MIT Imports: 3 Imported by: 0

README

go-schedule CI GoDoc Go Report Card

Launch a go function(s) on a schedule.

Usage

Sample usage where two functions get scheduled on different timers, the first runs 5 seconds, the second every 15 seconds:

package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"
	"time"

	schedule "github.com/mcneilcode/go-schedule"
)

func someFunc(ctx context.Context) {
	test := "Testing 1, 2, 3"
	fmt.Println(test)
}

func someOtherFunc(ctx context.Context) {
	test := "Testing 4, 5, 6"
	fmt.Println(test)
}

func main() {
	ctx := context.Background()
	worker := schedule.New()
	worker.Add(ctx, someFunc, time.Second*time.Duration(5))
	worker.Add(ctx, someOtherFunc, time.Second*time.Duration(15))
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt, os.Interrupt)
	<-quit
	worker.Stop()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

type Job func(ctx context.Context)

Job type is any function with a context.

type Scheduler

type Scheduler struct {
	// contains filtered or unexported fields
}

Scheduler type defines a structure containing a WaitGroup and cancellation functions.

func New

func New() *Scheduler

New returns a new scheduler containing a wait group and process control contexts.

func (*Scheduler) Add

func (s *Scheduler) Add(ctx context.Context, job Job, interval time.Duration)

Add spawns a goroutine to run the job at the provided interval.

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop cancels all running jobs of a scheduled process.

Jump to

Keyboard shortcuts

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