goscheduler

package module
v0.0.0-...-12239c3 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: MIT Imports: 4 Imported by: 0

README

goscheduler

An efficient periodic task scheduler with concurrency limits.

CircleCI

Features

  • Limits the number of concurrently executing tasks. This avoids overloading the resource used by the task (a server or network link, for example).
  • Efficiently schedules tasks that must be repeated periodically.
  • Individual tasks are rescheduled after they have run. This prevents multiple invocations of the same task from overlapping.

Installation

$ go install github.com/thomasbratt/goscheduler

Example

  package main
  
  import (
      "fmt"
      "time"
      "github.com/thomasbratt/goscheduler"
  )
  
  func main(){
      fmt.Println("Starting...")
      
      s := new(goscheduler.Scheduler)
      s.Init(1)
      
      start := time.Now()
      
      s.RepeatEvery(  time.Millisecond * 100,
                      func() bool {
                          fmt.Printf("Scheduled task ran at: %s\n", time.Since(start))
                          return true
                      })
      
      time.Sleep(time.Second * 1)
      
      s.Close()

Output from Example

>  Starting...
>  Scheduled task ran at: 100.245068ms
>  Scheduled task ran at: 200.570528ms
>  Scheduled task ran at: 300.895522ms
>  Scheduled task ran at: 401.277121ms
>  Scheduled task ran at: 501.588894ms
>  Scheduled task ran at: 601.925013ms
>  Scheduled task ran at: 702.263688ms
>  Scheduled task ran at: 802.606226ms
>  Scheduled task ran at: 902.941508ms

Documentation

Overview

Package goscheduler provides a task scheduler with configurable concurrency limits.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Scheduler

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

func (*Scheduler) Close

func (s *Scheduler) Close()

Close stops the scheduler from running. Currently running jobs will not be cancelled by this call but no new jobs will be started.

func (*Scheduler) Init

func (s *Scheduler) Init(concurrency int)

Init initializes the task scheduler with a specified number of concurrent workers.

func (*Scheduler) RepeatEvery

func (s *Scheduler) RepeatEvery(
	interval time.Duration,
	action func() bool)

RepeatEvery adds a task to the scheduler that is invoked every interval.

interval time.Duration
  - The interval between the previous execution finishing and the next one starting.

action func() bool
  - The action to execute when the job is ready to run. Return false to stop future invocations.

func (*Scheduler) RepeatForCount

func (s *Scheduler) RepeatForCount(
	count int,
	interval time.Duration,
	action func() bool)

RepeatForCount adds a task to the scheduler that is invoked at intervals for a fixed number of times.

count int
  - The number of times the job should be run.

interval time.Duration
  - The interval between the previous execution finishing and the next one starting.

action func() bool
  - The action to execute when the job is ready to run. Return false to stop future invocations.

Jump to

Keyboard shortcuts

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