Top Related Projects
a cron library for go
Easy and fluent Go cron scheduling. This is a fork from https://github.com/jasonlvhit/gocron
Framework for performing work asynchronously, outside of the request flow
Minimalist and zero-dependency scheduling library for Go
A Distributed, Fault-Tolerant Cron-Style Job System.
Quick Overview
gocron is a simple, lightweight Go library for scheduling and running periodic tasks. It provides a flexible way to execute functions at specified intervals or at specific times, making it useful for background jobs, maintenance tasks, or any recurring operations in Go applications.
Pros
- Easy to use with a simple API
- Supports various scheduling options (intervals, specific times, cron-like expressions)
- Lightweight with no external dependencies
- Concurrent execution of tasks
Cons
- Limited advanced features compared to more robust job scheduling libraries
- No built-in persistence for job states
- Lacks distributed scheduling capabilities
- Documentation could be more comprehensive
Code Examples
- Basic task scheduling:
s := gocron.NewScheduler(time.UTC)
s.Every(1).Hour().Do(func(){
fmt.Println("Task runs every hour")
})
s.StartBlocking()
- Scheduling with cron-like expressions:
s := gocron.NewScheduler(time.UTC)
s.Cron("0 30 * * * *").Do(func(){
fmt.Println("Task runs at 30 minutes past every hour")
})
s.StartAsync()
- Scheduling with specific weekdays:
s := gocron.NewScheduler(time.UTC)
s.Every(1).Monday().At("18:30").Do(func(){
fmt.Println("Task runs every Monday at 18:30")
})
s.StartBlocking()
Getting Started
To use gocron in your Go project, follow these steps:
-
Install the library:
go get github.com/jasonlvhit/gocron
-
Import the library in your Go code:
import "github.com/jasonlvhit/gocron"
-
Create a scheduler and add tasks:
s := gocron.NewScheduler(time.UTC) s.Every(5).Seconds().Do(func(){ fmt.Println("Task running every 5 seconds") }) s.StartBlocking()
This will create a scheduler that runs a task every 5 seconds. Adjust the timing and task function as needed for your specific use case.
Competitor Comparisons
a cron library for go
Pros of cron
- More mature and widely adopted project with a larger community
- Supports more advanced scheduling options, including timezone support
- Better documentation and examples available
Cons of cron
- Slightly more complex API, which may have a steeper learning curve
- Lacks some convenience features like automatic job naming
Code Comparison
gocron:
s := gocron.NewScheduler(time.UTC)
s.Every(1).Day().At("10:30").Do(task)
cron:
c := cron.New()
c.AddFunc("30 10 * * *", task)
c.Start()
Key Differences
- gocron uses a more fluent, chainable API for job scheduling
- cron uses standard cron syntax for defining schedules
- gocron provides built-in methods for common intervals (e.g., Every(1).Day())
- cron offers more flexibility in schedule definition but requires understanding cron syntax
Conclusion
Both libraries offer cron-like functionality for Go applications. gocron provides a more user-friendly API for simple scheduling tasks, while cron offers more advanced features and flexibility for complex scheduling needs. The choice between the two depends on the specific requirements of your project and your familiarity with cron syntax.
Easy and fluent Go cron scheduling. This is a fork from https://github.com/jasonlvhit/gocron
Pros of gocron (go-co-op)
- More active development and maintenance
- Better concurrency support with goroutines
- Improved error handling and logging capabilities
Cons of gocron (go-co-op)
- Slightly more complex API compared to the simpler interface of gocron (jasonlvhit)
- May have a steeper learning curve for beginners
Code Comparison
gocron (jasonlvhit):
s := gocron.NewScheduler()
s.Every(1).Day().At("10:30").Do(task)
s.Start()
gocron (go-co-op):
s := gocron.NewScheduler(time.UTC)
s.Every(1).Day().At("10:30").Do(task)
s.StartAsync()
The code structure is similar, but go-co-op/gocron offers more flexibility with timezone handling and asynchronous execution. The go-co-op version also provides better support for context and error handling, making it more suitable for complex scheduling scenarios in production environments.
While both libraries serve the purpose of job scheduling in Go, go-co-op/gocron offers more features and active development, making it a better choice for most modern applications. However, jasonlvhit/gocron might be preferred for simpler use cases or projects that require minimal dependencies.
Framework for performing work asynchronously, outside of the request flow
Pros of jobrunner
- Simpler API with fewer methods, making it easier to learn and use
- Built-in support for concurrent job execution
- Includes a web interface for monitoring jobs
Cons of jobrunner
- Less flexible scheduling options compared to gocron
- Fewer stars and contributors on GitHub, potentially indicating less community support
- Limited documentation and examples available
Code Comparison
gocron:
s := gocron.NewScheduler(time.UTC)
s.Every(1).Day().At("10:30").Do(task)
s.StartBlocking()
jobrunner:
jobrunner.Start()
jobrunner.Schedule("@every 1d", MyJob{})
jobrunner.Run()
Both libraries offer straightforward ways to schedule and run jobs, but gocron provides more granular control over scheduling intervals and times. jobrunner uses a simpler approach with predefined scheduling patterns.
While gocron focuses solely on job scheduling, jobrunner includes additional features like concurrent execution and a web interface for job monitoring. However, gocron offers more flexibility in defining custom scheduling patterns.
The choice between these libraries depends on the specific requirements of your project, such as the need for concurrent execution, monitoring capabilities, or fine-grained scheduling control.
Minimalist and zero-dependency scheduling library for Go
Pros of go-quartz
- More feature-rich, offering advanced scheduling options like cron expressions and calendar-based triggers
- Better support for distributed environments and clustering
- More actively maintained with recent updates and contributions
Cons of go-quartz
- More complex API and steeper learning curve compared to gocron's simplicity
- Heavier dependency footprint, which may increase project size
Code Comparison
gocron:
s := gocron.NewScheduler(time.UTC)
s.Every(1).Day().At("10:30").Do(task)
s.StartBlocking()
go-quartz:
scheduler := quartz.NewStdScheduler()
job := quartz.NewJobDetail(myJob, "group1", "job1")
trigger := quartz.NewCronTrigger("0 30 10 * * ?")
scheduler.ScheduleJob(job, trigger)
scheduler.Start()
Summary
go-quartz offers more advanced features and better support for complex scheduling scenarios, making it suitable for larger, distributed applications. However, this comes at the cost of increased complexity and a larger footprint. gocron, on the other hand, provides a simpler, more straightforward approach that may be preferable for smaller projects or those requiring basic scheduling functionality.
A Distributed, Fault-Tolerant Cron-Style Job System.
Pros of cronsun
- Distributed architecture for better scalability and fault tolerance
- Web-based UI for easier management and monitoring
- Supports multiple node execution for load balancing
Cons of cronsun
- More complex setup and configuration
- Requires additional dependencies (etcd, MongoDB)
- Steeper learning curve for beginners
Code comparison
gocron:
s := gocron.NewScheduler(time.UTC)
s.Every(1).Day().At("10:30").Do(task)
s.StartBlocking()
cronsun:
node := cronsun.NewNode(cfg)
node.Run()
Key differences
- gocron is a lightweight, in-process scheduler library
- cronsun is a distributed job system with a client-server architecture
- gocron focuses on simplicity and ease of use
- cronsun offers more advanced features for complex scheduling scenarios
Both projects serve different use cases:
- gocron is suitable for small to medium-sized applications with straightforward scheduling needs
- cronsun is better for large-scale distributed systems requiring high availability and scalability
When choosing between the two, consider your project's requirements, infrastructure, and team expertise.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Note from current maintainers:
A currently maintained fork of this project has been migrated to https://github.com/go-co-op/gocron
Disclaimer: we (the maintainers) tried, with no luck, to get in contact with Jason (the repository owner) in order to add new maintainers or leave the project within an organization. Unfortunately, he hasn't replied for months now (March, 2020).
So, we decided to move the project to a new repository (as stated above), in order to keep the evolution of the project coming from as many people as possible. Feel free to reach over!
goCron: A Golang Job Scheduling Package.
This package is currently looking for new maintainers (cause @jasonlvhit is in ICU). Please message @jasonlvhit if you are interested.
goCron is a Golang job scheduling package which lets you run Go functions periodically at pre-determined interval using a simple, human-friendly syntax.
goCron is a Golang implementation of Ruby module clockwork and Python job scheduling package schedule, and personally, this package is my first Golang program, just for fun and practice.
See also this two great articles:
If you want to chat, you can find us at Slack!
Back to this package, you could just use this simple API as below, to run a cron scheduler.
package main
import (
"fmt"
"time"
"github.com/jasonlvhit/gocron"
)
func task() {
fmt.Println("I am running task.")
}
func taskWithParams(a int, b string) {
fmt.Println(a, b)
}
func main() {
// Do jobs without params
gocron.Every(1).Second().Do(task)
gocron.Every(2).Seconds().Do(task)
gocron.Every(1).Minute().Do(task)
gocron.Every(2).Minutes().Do(task)
gocron.Every(1).Hour().Do(task)
gocron.Every(2).Hours().Do(task)
gocron.Every(1).Day().Do(task)
gocron.Every(2).Days().Do(task)
gocron.Every(1).Week().Do(task)
gocron.Every(2).Weeks().Do(task)
// Do jobs with params
gocron.Every(1).Second().Do(taskWithParams, 1, "hello")
// Do jobs on specific weekday
gocron.Every(1).Monday().Do(task)
gocron.Every(1).Thursday().Do(task)
// Do a job at a specific time - 'hour:min:sec' - seconds optional
gocron.Every(1).Day().At("10:30").Do(task)
gocron.Every(1).Monday().At("18:30").Do(task)
gocron.Every(1).Tuesday().At("18:30:59").Do(task)
// Begin job immediately upon start
gocron.Every(1).Hour().From(gocron.NextTick()).Do(task)
// Begin job at a specific date/time
t := time.Date(2019, time.November, 10, 15, 0, 0, 0, time.Local)
gocron.Every(1).Hour().From(&t).Do(task)
// NextRun gets the next running time
_, time := gocron.NextRun()
fmt.Println(time)
// Remove a specific job
gocron.Remove(task)
// Clear all scheduled jobs
gocron.Clear()
// Start all the pending jobs
<- gocron.Start()
// also, you can create a new scheduler
// to run two schedulers concurrently
s := gocron.NewScheduler()
s.Every(3).Seconds().Do(task)
<- s.Start()
}
and full test cases and document will be coming soon (help is wanted! If you want to contribute, pull requests are welcome).
If you need to prevent a job from running at the same time from multiple cron instances (like running a cron app from multiple servers), you can provide a Locker implementation and lock the required jobs.
gocron.SetLocker(lockerImplementation)
gocron.Every(1).Hour().Lock().Do(task)
Once again, thanks to the great works of Ruby clockwork and Python schedule package. BSD license is used, see the file License for detail.
Looking to contribute? Try to follow these guidelines:
- Use issues for everything
- For a small change, just send a PR!
- For bigger changes, please open an issue for discussion before sending a PR.
- PRs should have: tests, documentation and examples (if it makes sense)
- You can also contribute by:
- Reporting issues
- Suggesting new features or enhancements
- Improving/fixing documentation
Have fun!
Top Related Projects
a cron library for go
Easy and fluent Go cron scheduling. This is a fork from https://github.com/jasonlvhit/gocron
Framework for performing work asynchronously, outside of the request flow
Minimalist and zero-dependency scheduling library for Go
A Distributed, Fault-Tolerant Cron-Style Job System.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot