Task execution system
Go to file
2025-04-17 21:56:47 -06:00
go.mod fix: fix the module name 2025-04-17 16:29:44 -06:00
README.md chore: add a README 2025-04-17 21:56:47 -06:00
task_test.go fix: fix tests, add tiny sleep when starting task executor so all queued tasks are in the tasks slice 2025-04-17 16:23:32 -06:00
task.go fix: fix tests, add tiny sleep when starting task executor so all queued tasks are in the tasks slice 2025-04-17 16:23:32 -06:00

Task

A simple and efficient scheduled task execution library for Go applications.

Overview

The Task library provides a lightweight mechanism for scheduling and executing tasks with configurable intervals and rate limiting. It's particularly useful for applications that need to run background jobs, periodic maintenance tasks, or any recurring operations.

Features

  • Schedule tasks to run at regular intervals
  • Rate limiting to control concurrent task execution
  • Run tasks immediately (zero interval)
  • Simple interface-based design for easy integration

Installation

go get git.canoozie.net/jer/task

Usage

Basic Example

package main

import (
	"fmt"
	"time"

	"git.canoozie.net/jer/task"
)

// Define a task by implementing the Task interface
type MyTask struct {
	Name string
}

func (t *MyTask) Execute() error {
	fmt.Printf("Executing task: %s\n", t.Name)
	return nil
}

func main() {
	// Create a task executor with a rate limit of 2 concurrent tasks
	executor := task.NewTaskExecutor(2)

	// Add tasks with different intervals
	executor.AddTask(&MyTask{Name: "Every Second"}, 1*time.Second)
	executor.AddTask(&MyTask{Name: "Every 5 Seconds"}, 5*time.Second)
	executor.AddTask(&MyTask{Name: "Run Once Now"}, 0*time.Second)

	// Start the task executor
	executor.Start()

	// Keep the program running
	select {}
}

Rate Limiting

The task executor includes built-in rate limiting to control how many tasks can run concurrently. This is useful for resource-intensive tasks or when you need to limit API calls.

// Create an executor that allows up to 5 concurrent tasks
executor := task.NewTaskExecutor(5)

Task Interface

To create a task, implement the Task interface:

type Task interface {
	Execute() error
}

Any errors returned from Execute() will be logged but will not prevent future executions.

API Reference

Types

Task

type Task interface {
	Execute() error
}

Functions

NewTaskExecutor

func NewTaskExecutor(rateLimit int) *TaskExecutor

Creates a new task executor with the specified concurrency limit.

Methods

AddTask

func (te *TaskExecutor) AddTask(task Task, interval time.Duration)

Schedules a task to run at the specified interval. If interval is 0, the task is executed immediately. Negative intervals are ignored.

Start

func (te *TaskExecutor) Start()

Starts the task executor, which will begin processing scheduled tasks.

Len

func (te *TaskExecutor) Len() int

Returns the number of tasks managed by the executor.

License

Copyright (C) 2025 Jeremy Tregunna

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.