{"id":13561355,"url":"https://github.com/codnect/chrono","last_synced_at":"2026-01-16T18:16:15.335Z","repository":{"id":42987768,"uuid":"367404527","full_name":"codnect/chrono","owner":"codnect","description":"Chrono is a scheduler library that lets you run your task and code periodically","archived":false,"fork":false,"pushed_at":"2024-07-22T04:10:07.000Z","size":75,"stargazers_count":443,"open_issues_count":10,"forks_count":23,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-09T22:02:49.969Z","etag":null,"topics":["chrono","cron","go","golang","golang-library","schedule-task","scheduler","scheduler-library"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codnect.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-14T15:27:30.000Z","updated_at":"2025-02-25T04:02:27.000Z","dependencies_parsed_at":"2023-11-26T21:28:59.260Z","dependency_job_id":"c7b972d1-c526-4f28-a765-9531f87367dc","html_url":"https://github.com/codnect/chrono","commit_stats":null,"previous_names":["codnect/chrono","procyon-projects/chrono"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codnect%2Fchrono","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codnect%2Fchrono/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codnect%2Fchrono/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codnect%2Fchrono/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codnect","download_url":"https://codeload.github.com/codnect/chrono/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247046764,"owners_count":20874718,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chrono","cron","go","golang","golang-library","schedule-task","scheduler","scheduler-library"],"created_at":"2024-08-01T13:00:55.228Z","updated_at":"2026-01-16T18:16:15.321Z","avatar_url":"https://github.com/codnect.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"![Chrono Logo](https://user-images.githubusercontent.com/5354910/196008920-1ca88967-3d7d-449c-b165-fe38c5e1fb57.png)\n# Chrono\n[![Go Report Card](https://goreportcard.com/badge/codnect.io/chrono)](https://goreportcard.com/report/codnect.io/chrono)\n[![CircleCI](https://dl.circleci.com/status-badge/img/gh/procyon-projects/chrono/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/procyon-projects/chrono/tree/main)\n[![codecov](https://codecov.io/gh/procyon-projects/chrono/branch/main/graph/badge.svg?token=OREV0YI8VU)](https://codecov.io/gh/procyon-projects/chrono)\n\nChrono is a scheduler library that lets you run your tasks and code periodically. It provides different scheduling functionalities to make it easier to create a scheduling task.\n\n## Scheduling a One-Shot Task\nThe Schedule method helps us schedule the task to run once at the specified time. In the following example, the task will first be executed 1 second after the current time.\n**WithTime** option is used to specify the execution time.\n\n```go\ntaskScheduler := chrono.NewDefaultTaskScheduler()\nnow := time.Now()\nstartTime := now.Add(time.Second * 1)\n\ntask, err := taskScheduler.Schedule(func(ctx context.Context) {\n\tlog.Print(\"One-Shot Task\")\n}, chrono.WithTime(startTime))\n\nif err == nil {\n\tlog.Print(\"Task has been scheduled successfully.\")\n}\n```\n\nAlso, **WithStartTime** option can be used to specify the execution time. **But It's deprecated.**\n\n```go\ntaskScheduler := chrono.NewDefaultTaskScheduler()\n\ntask, err := taskScheduler.Schedule(func(ctx context.Context) {\n\tlog.Print(\"One-Shot Task\")\n}, chrono.WithStartTime(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()+1))\n\nif err == nil {\n\tlog.Print(\"Task has been scheduled successfully.\")\n}\n```\n\n## Scheduling a Task with Fixed Delay\nLet's schedule a task to run with a fixed delay between the finish time of the last execution of the task and the start time of the next execution of the task.\nThe fixed delay counts the delay after the completion of the last execution.\n\n```go\ntaskScheduler := chrono.NewDefaultTaskScheduler()\n\ntask, err := taskScheduler.ScheduleWithFixedDelay(func(ctx context.Context) {\n\tlog.Print(\"Fixed Delay Task\")\n\ttime.Sleep(3 * time.Second)\n}, 5 * time.Second)\n\nif err == nil {\n\tlog.Print(\"Task has been scheduled successfully.\")\n}\n```\n\nSince the task itself takes 3 seconds to complete and we have specified a delay of 5 seconds between the finish time of the last execution of the task and the start time of the next execution of the task, there will be a delay of 8 seconds between each execution.\n\n**WithStartTime** and **WithLocation** options can be combined with this.\n\n## Schedule Task at a Fixed Rate\nLet's schedule a task to run at a fixed rate of seconds.\n\n```go\ntaskScheduler := chrono.NewDefaultTaskScheduler()\n\ntask, err := taskScheduler.ScheduleAtFixedRate(func(ctx context.Context) {\n\tlog.Print(\"Fixed Rate of 5 seconds\")\n}, 5 * time.Second)\n\nif err == nil {\n\tlog.Print(\"Task has been scheduled successfully.\")\n}\n```\n\nThe next task will run always after 5 seconds no matter the status of the previous task, which may be still running. So even if the previous task isn't done, the next task will run.\nWe can also use the **WithStartTime** option to specify the desired first execution time of the task.\n\n```go\nnow := time.Now()\n\ntask, err := taskScheduler.ScheduleAtFixedRate(func(ctx context.Context) {\n\tlog.Print(\"Fixed Rate of 5 seconds\")\n}, 5 * time.Second, chrono.WithStartTime(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second() + 2))\n```\n\nWhen we use this option, the task will run at the specified execution time and subsequently with the given period. In the above example, the task will first be executed 2 seconds after the current time.\n\nWe can also combine this option with **WithLocation** based on our requirements.\n\n```go\nnow := time.Now()\n\ntask, err := taskScheduler.ScheduleAtFixedRate(func(ctx context.Context) {\n\tlog.Print(\"Fixed Rate of 5 seconds\")\n}, 5 * time.Second, chrono.WithStartTime(now.Year(), now.Month(), now.Day(), 18, 45, 0),\nchrono.WithLocation(\"America/New_York\"))\n```\n\nIn the above example, the task will first be executed at 18:45 of the current date in America/New York time.\n**If the start time is in the past, the task will be executed immediately.**\n\n## Scheduling a Task using Cron Expression\nSometimes Fixed Rate and Fixed Delay can not fulfill your needs, and we need the flexibility of cron expressions to schedule the execution of your tasks. With the help of the provided **ScheduleWithCron method**, we can schedule a task based on a cron expression.\n\n```go\ntaskScheduler := chrono.NewDefaultTaskScheduler()\n\ntask, err := taskScheduler.ScheduleWithCron(func(ctx context.Context) {\n\tlog.Print(\"Scheduled Task With Cron\")\n}, \"0 45 18 10 * *\")\n\nif err == nil {\n\tlog.Print(\"Task has been scheduled\")\n}\n```\n\nIn this case, we're scheduling a task to be executed at 18:45  on the 10th day of every month\n\nBy default, the local time is used for the cron expression. However, we can use the **WithLocation** option to change this.\n\n```go\ntask, err := taskScheduler.ScheduleWithCron(func(ctx context.Context) {\n\tlog.Print(\"Scheduled Task With Cron\")\n}, \"0 45 18 10 * *\", chrono.WithLocation(\"America/New_York\"))\n```\n\nIn the above example, Task will be scheduled to be executed at 18:45 on the 10th day of every month in America/New York time.\n\n**WithStartTimeoption** cannot be used with **ScheduleWithCron**.\n\n## Canceling a Scheduled Task\nSchedule methods return an instance of type ScheduledTask, which allows us to cancel a task or to check if the task is canceled. The Cancel method cancels the scheduled task but running tasks won't be interrupted.\n\n\n```go\ntaskScheduler := chrono.NewDefaultTaskScheduler()\n\ntask, err := taskScheduler.ScheduleAtFixedRate(func(ctx context.Context) {\n\tlog.Print(\"Fixed Rate of 5 seconds\")\n}, 5 * time.Second)\n\n/* ... */\n\t\ntask.Cancel()\n```\n\n## Shutting Down a Scheduler\nThe **Shutdown()** method doesn't cause immediate shut down of the Scheduler and returns a channel. It will make the Scheduler stop accepting new tasks and shut down after all running tasks finish their current work.\n\n\n```go\ntaskScheduler := chrono.NewDefaultTaskScheduler()\n\n/* ... */\n\nshutdownChannel := taskScheduler.Shutdown()\n\u003c- shutdownChannel\n\t\n/* after all running task finished their works */\n```\n\nStargazers\n-----------\n[![Stargazers repo roster for @procyon-projects/chrono](https://reporoster.com/stars/procyon-projects/chrono)](https://codnect.io/chrono/stargazers)\n\nForkers\n-----------\n[![Forkers repo roster for @procyon-projects/chrono](https://reporoster.com/forks/procyon-projects/chrono)](https://codnect.io/chrono/network/members)\n\n# License\nChrono is released under MIT License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodnect%2Fchrono","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodnect%2Fchrono","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodnect%2Fchrono/lists"}