schedule

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2023 License: MIT Imports: 1 Imported by: 6

README

AtomicGo | schedule

Downloads Latest Release Tests Coverage Unit test count Go report


Documentation | Contributing | Code of Conduct


AtomicGo

go get atomicgo.dev/schedule

schedule

import "atomicgo.dev/schedule"

Package schedule provides a simple scheduler for Go.

It can run a function at a given time, in a given duration, or repeatedly at a given interval.

Index

type Task

Task holds information about the running task and can be used to stop running tasks.

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

func After
func After(d time.Duration, task func()) *Task

After executes the task after the given duration. The function is non-blocking. If you want to wait for the task to be executed, use the Task.Wait method.

Example

package main

import (
	"fmt"
	"time"

	"atomicgo.dev/schedule"
)

func main() {
	task := schedule.After(5*time.Second, func() {
		fmt.Println("5 seconds are over!")
	})

	fmt.Println("Some stuff happening...")

	task.Wait()
}

func At
func At(t time.Time, task func()) *Task

At executes the task at the given time. The function is non-blocking. If you want to wait for the task to be executed, use the Task.Wait method.

Example

package main

import (
	"fmt"
	"time"

	"atomicgo.dev/schedule"
)

func main() {
	task := schedule.At(time.Now().Add(5*time.Second), func() {
		fmt.Println("5 seconds are over!")
	})

	fmt.Println("Some stuff happening...")

	task.Wait()
}

func Every
func Every(interval time.Duration, task func() bool) *Task

Every executes the task in the given interval, as long as the task function returns true. The function is non-blocking. If you want to wait for the task to be executed, use the Task.Wait method.

Example

package main

import (
	"fmt"
	"time"

	"atomicgo.dev/schedule"
)

func main() {
	task := schedule.Every(time.Second, func() bool {
		fmt.Println("1 second is over!")
		return true // return false to stop the task
	})

	fmt.Println("Some stuff happening...")

	time.Sleep(10 * time.Second)

	task.Stop()
}

func (*Task) ExecutesIn
func (s *Task) ExecutesIn() time.Duration

ExecutesIn returns the duration until the next execution.

func (*Task) IsActive
func (s *Task) IsActive() bool

IsActive returns true if the scheduler is active.

func (*Task) NextExecutionTime
func (s *Task) NextExecutionTime() time.Time

NextExecutionTime returns the time when the next execution will happen.

func (*Task) StartedAt
func (s *Task) StartedAt() time.Time

StartedAt returns the time when the scheduler was started.

func (*Task) Stop
func (s *Task) Stop()

Stop stops the scheduler.

func (*Task) Wait
func (s *Task) Wait()

Wait blocks until the scheduler is stopped. After and At will stop automatically after the task is executed.

Generated by gomarkdoc


AtomicGo.dev  ·  with ❤️ by @MarvinJWendt | MarvinJWendt.com

Documentation

Overview

Package schedule provides a simple scheduler for Go.

It can run a function at a given time, in a given duration, or repeatedly at a given interval.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Task

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

Task holds information about the running task and can be used to stop running tasks.

func After

func After(d time.Duration, task func()) *Task

After executes the task after the given duration. The function is non-blocking. If you want to wait for the task to be executed, use the Task.Wait method.

Example
package main

import (
	"fmt"
	"time"

	"atomicgo.dev/schedule"
)

func main() {
	task := schedule.After(5*time.Second, func() {
		fmt.Println("5 seconds are over!")
	})

	fmt.Println("Some stuff happening...")

	task.Wait()
}
Output:

func At

func At(t time.Time, task func()) *Task

At executes the task at the given time. The function is non-blocking. If you want to wait for the task to be executed, use the Task.Wait method.

Example
package main

import (
	"fmt"
	"time"

	"atomicgo.dev/schedule"
)

func main() {
	task := schedule.At(time.Now().Add(5*time.Second), func() {
		fmt.Println("5 seconds are over!")
	})

	fmt.Println("Some stuff happening...")

	task.Wait()
}
Output:

func Every

func Every(interval time.Duration, task func() bool) *Task

Every executes the task in the given interval, as long as the task function returns true. The function is non-blocking. If you want to wait for the task to be executed, use the Task.Wait method.

Example
package main

import (
	"fmt"
	"time"

	"atomicgo.dev/schedule"
)

func main() {
	task := schedule.Every(time.Second, func() bool {
		fmt.Println("1 second is over!")
		return true // return false to stop the task
	})

	fmt.Println("Some stuff happening...")

	time.Sleep(10 * time.Second)

	task.Stop()
}
Output:

func (*Task) ExecutesIn

func (s *Task) ExecutesIn() time.Duration

ExecutesIn returns the duration until the next execution.

func (*Task) IsActive

func (s *Task) IsActive() bool

IsActive returns true if the scheduler is active.

func (*Task) NextExecutionTime

func (s *Task) NextExecutionTime() time.Time

NextExecutionTime returns the time when the next execution will happen.

func (*Task) StartedAt

func (s *Task) StartedAt() time.Time

StartedAt returns the time when the scheduler was started.

func (*Task) Stop

func (s *Task) Stop()

Stop stops the scheduler.

func (*Task) Wait

func (s *Task) Wait()

Wait blocks until the scheduler is stopped. After and At will stop automatically after the task is executed.

Jump to

Keyboard shortcuts

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