File: README.md

package info (click to toggle)
golang-github-atomicgo-schedule 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 120 kB
  • sloc: makefile: 2
file content (281 lines) | stat: -rw-r--r-- 6,928 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<h1 align="center">AtomicGo | schedule</h1>

<p align="center">
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fatomicgo.dev%2Fapi%2Fshields%2Fschedule&style=flat-square" alt="Downloads">

<a href="https://github.com/atomicgo/schedule/releases">
<img src="https://img.shields.io/github/v/release/atomicgo/schedule?style=flat-square" alt="Latest Release">
</a>

<a href="https://codecov.io/gh/atomicgo/schedule" target="_blank">
<img src="https://img.shields.io/github/actions/workflow/status/atomicgo/schedule/go.yml?style=flat-square" alt="Tests">
</a>

<a href="https://codecov.io/gh/atomicgo/schedule" target="_blank">
<img src="https://img.shields.io/codecov/c/gh/atomicgo/schedule?color=magenta&logo=codecov&style=flat-square" alt="Coverage">
</a>

<a href="https://codecov.io/gh/atomicgo/schedule">
<!-- unittestcount:start --><img src="https://img.shields.io/badge/Unit_Tests-0-magenta?style=flat-square" alt="Unit test count"><!-- unittestcount:end -->
</a>

<a href="https://opensource.org/licenses/MIT" target="_blank">
<img src="https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square" alt="License: MIT">
</a>
  
<a href="https://goreportcard.com/report/github.com/atomicgo/schedule" target="_blank">
<img src="https://goreportcard.com/badge/github.com/atomicgo/schedule?style=flat-square" alt="Go report">
</a>   

</p>

---

<p align="center">
<strong><a href="https://pkg.go.dev/atomicgo.dev/schedule#section-documentation" target="_blank">Documentation</a></strong>
|
<strong><a href="https://github.com/atomicgo/atomicgo/blob/main/CONTRIBUTING.md" target="_blank">Contributing</a></strong>
|
<strong><a href="https://github.com/atomicgo/atomicgo/blob/main/CODE_OF_CONDUCT.md" target="_blank">Code of Conduct</a></strong>
</p>

---

<p align="center">
  <img src="https://raw.githubusercontent.com/atomicgo/atomicgo/main/assets/header.png" alt="AtomicGo">
</p>

<p align="center">
<table>
<tbody>
</tbody>
</table>
</p>
<h3  align="center"><pre>go get atomicgo.dev/schedule</pre></h3>
<p align="center">
<table>
<tbody>
</tbody>
</table>
</p>

<!-- gomarkdoc:embed:start -->

<!-- Code generated by gomarkdoc. DO NOT EDIT -->

# schedule

```go
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>)
  - [func After\(d time.Duration, task func\(\)\) \*Task](<#After>)
  - [func At\(t time.Time, task func\(\)\) \*Task](<#At>)
  - [func Every\(interval time.Duration, task func\(\) bool\) \*Task](<#Every>)
  - [func \(s \*Task\) ExecutesIn\(\) time.Duration](<#Task.ExecutesIn>)
  - [func \(s \*Task\) IsActive\(\) bool](<#Task.IsActive>)
  - [func \(s \*Task\) NextExecutionTime\(\) time.Time](<#Task.NextExecutionTime>)
  - [func \(s \*Task\) StartedAt\(\) time.Time](<#Task.StartedAt>)
  - [func \(s \*Task\) Stop\(\)](<#Task.Stop>)
  - [func \(s \*Task\) Wait\(\)](<#Task.Wait>)


<a name="Task"></a>
## type [Task](<https://github.com/atomicgo/schedule/blob/main/schedule.go#L6-L10>)

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

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

<a name="After"></a>
### func [After](<https://github.com/atomicgo/schedule/blob/main/schedule.go#L58>)

```go
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.

<details><summary>Example</summary>
<p>



```go
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()
}
```

</p>
</details>

<a name="At"></a>
### func [At](<https://github.com/atomicgo/schedule/blob/main/schedule.go#L77>)

```go
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.

<details><summary>Example</summary>
<p>



```go
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()
}
```

</p>
</details>

<a name="Every"></a>
### func [Every](<https://github.com/atomicgo/schedule/blob/main/schedule.go#L96>)

```go
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.

<details><summary>Example</summary>
<p>



```go
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()
}
```

</p>
</details>

<a name="Task.ExecutesIn"></a>
### func \(\*Task\) [ExecutesIn](<https://github.com/atomicgo/schedule/blob/main/schedule.go#L31>)

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

ExecutesIn returns the duration until the next execution.

<a name="Task.IsActive"></a>
### func \(\*Task\) [IsActive](<https://github.com/atomicgo/schedule/blob/main/schedule.go#L36>)

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

IsActive returns true if the scheduler is active.

<a name="Task.NextExecutionTime"></a>
### func \(\*Task\) [NextExecutionTime](<https://github.com/atomicgo/schedule/blob/main/schedule.go#L26>)

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

NextExecutionTime returns the time when the next execution will happen.

<a name="Task.StartedAt"></a>
### func \(\*Task\) [StartedAt](<https://github.com/atomicgo/schedule/blob/main/schedule.go#L21>)

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

StartedAt returns the time when the scheduler was started.

<a name="Task.Stop"></a>
### func \(\*Task\) [Stop](<https://github.com/atomicgo/schedule/blob/main/schedule.go#L52>)

```go
func (s *Task) Stop()
```

Stop stops the scheduler.

<a name="Task.Wait"></a>
### func \(\*Task\) [Wait](<https://github.com/atomicgo/schedule/blob/main/schedule.go#L47>)

```go
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](<https://github.com/princjef/gomarkdoc>)


<!-- gomarkdoc:embed:end -->

---

> [AtomicGo.dev](https://atomicgo.dev) &nbsp;&middot;&nbsp;
> with ❤️ by [@MarvinJWendt](https://github.com/MarvinJWendt) |
> [MarvinJWendt.com](https://marvinjwendt.com)