Skip to content

Commit 291adb8

Browse files
authored
doc: add v1 to v2 migration guide (#890)
1 parent 4278ff7 commit 291adb8

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

migration_v1_to_v2.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Migration Guide: `gocron` v1 → v2
2+
3+
This guide helps you migrate your code from the `v1` branch to the `v2` branch of [go-co-op/gocron](https://github.com/go-co-op/gocron).
4+
Version 2 is a major rewrite focusing on improving the internals of gocron, while also enhancing the user interfaces and error handling.
5+
All major functionality has been ported over.
6+
7+
---
8+
9+
## Table of Contents
10+
11+
- [Overview of Major Changes](#overview-of-major-changes)
12+
- [Installation](#installation)
13+
- [API Changes](#api-changes)
14+
- [Scheduler Creation](#scheduler-creation)
15+
- [Job Definition](#job-definition)
16+
- [Starting and Stopping the Scheduler](#starting-and-stopping-the-scheduler)
17+
- [Error Handling](#error-handling)
18+
- [Distributed Scheduling](#distributed-scheduling)
19+
- [Examples Migration](#examples-migration)
20+
- [Testing and Validation](#testing-and-validation)
21+
- [Troubleshooting](#troubleshooting)
22+
- [References](#references)
23+
24+
---
25+
26+
## Overview of Major Changes
27+
28+
- **Breaking API changes**: All major interfaces and types have changed.
29+
- **Improved error reporting**: Most functions now return errors.
30+
- **Job IDs and cancellation**: Jobs have unique IDs and can be cancelled.
31+
- **Distributed and monitored scheduling**: Built-in support for distributed schedulers and job monitors.
32+
- **Context and logging enhancements**: Improved support for cancellation, context, and custom logging interfaces.
33+
34+
---
35+
36+
## Installation
37+
38+
Update your dependency to v2:
39+
40+
```sh
41+
go get github.com/go-co-op/gocron/v2
42+
```
43+
44+
**Note:** The import path is `github.com/go-co-op/gocron/v2`.
45+
46+
---
47+
48+
## API Changes
49+
50+
### 1. Scheduler Creation
51+
52+
**v1:**
53+
```go
54+
import "github.com/go-co-op/gocron"
55+
56+
s := gocron.NewScheduler(time.UTC)
57+
```
58+
59+
**v2:**
60+
```go
61+
import "github.com/go-co-op/gocron/v2"
62+
63+
s, err := gocron.NewScheduler()
64+
if err != nil { panic(err) }
65+
```
66+
- **v2** returns an error on creation.
67+
- **v2** does not require a location/timezone argument. Use `WithLocation()` if needed.
68+
69+
---
70+
71+
### 2. Job Creation
72+
73+
**v1:**
74+
```go
75+
s.Every(1).Second().Do(taskFunc)
76+
```
77+
78+
**v2:**
79+
```go
80+
j, err := s.NewJob(
81+
gocron.DurationJob(1*time.Second),
82+
gocron.NewTask(taskFunc),
83+
)
84+
if err != nil { panic(err) }
85+
```
86+
- **v2** uses explicit job types (`DurationJob`, `CronJob`, etc).
87+
- **v2** jobs have unique IDs: `j.ID()`.
88+
- **v2** returns an error on job creation.
89+
90+
#### Cron Expressions
91+
92+
**v1:**
93+
```go
94+
s.Cron("*/5 * * * *").Do(taskFunc)
95+
```
96+
97+
**v2:**
98+
```go
99+
j, err := s.NewJob(
100+
gocron.CronJob("*/5 * * * *"),
101+
gocron.NewTask(taskFunc),
102+
)
103+
```
104+
105+
#### Arguments
106+
107+
**v1:**
108+
```go
109+
s.Every(1).Second().Do(taskFunc, arg1, arg2)
110+
```
111+
112+
**v2:**
113+
```go
114+
j, err := s.NewJob(
115+
gocron.DurationJob(1*time.Second),
116+
gocron.NewTask(taskFunc, arg1, arg2),
117+
)
118+
```
119+
120+
---
121+
122+
### 3. Starting and Stopping the Scheduler
123+
124+
**v1:**
125+
```go
126+
s.StartAsync()
127+
s.Stop()
128+
```
129+
130+
**v2:**
131+
```go
132+
s.Start()
133+
s.Shutdown()
134+
```
135+
136+
- Always call `Shutdown()` for graceful cleanup.
137+
138+
---
139+
140+
### 4. Error Handling
141+
142+
- Most v2 methods return errors. Always check `err`.
143+
- Use `errors.go` for error definitions.
144+
145+
---
146+
147+
## References
148+
149+
- [v2 API Documentation](https://pkg.go.dev/github.com/go-co-op/gocron/v2)
150+
- [Examples](https://pkg.go.dev/github.com/go-co-op/gocron/v2#pkg-examples)
151+
- [Release Notes](https://github.com/go-co-op/gocron/releases)
152+
153+
---
154+
155+
**If you encounter issues, open a GitHub Issue or consider contributing a fix by checking out the [CONTRIBUTING.md](CONTRIBUTING.md) guide.**

0 commit comments

Comments
 (0)