-
Notifications
You must be signed in to change notification settings - Fork 86
/
meilisearch.go
717 lines (630 loc) · 20.6 KB
/
meilisearch.go
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
package meilisearch
import (
"context"
"fmt"
"net/http"
"strconv"
"time"
"github.com/golang-jwt/jwt/v4"
)
type meilisearch struct {
client *client
}
// New create new service manager for operating on meilisearch
func New(host string, options ...Option) ServiceManager {
defOpt := defaultMeiliOpt
for _, opt := range options {
opt(defOpt)
}
return &meilisearch{
client: newClient(
defOpt.client,
host,
defOpt.apiKey,
clientConfig{
contentEncoding: defOpt.contentEncoding.encodingType,
encodingCompressionLevel: defOpt.contentEncoding.level,
disableRetry: defOpt.disableRetry,
retryOnStatus: defOpt.retryOnStatus,
maxRetries: defOpt.maxRetries,
},
),
}
}
// Connect create service manager and check connection with meilisearch
func Connect(host string, options ...Option) (ServiceManager, error) {
meili := New(host, options...)
if !meili.IsHealthy() {
return nil, ErrConnectingFailed
}
return meili, nil
}
func (m *meilisearch) GetServiceReader() ServiceReader {
return m
}
func (m *meilisearch) GetTaskManager() TaskManager {
return m
}
func (m *meilisearch) GetTaskReader() TaskReader {
return m
}
func (m *meilisearch) GetKeyManager() KeyManager {
return m
}
func (m *meilisearch) GetKeyReader() KeyReader {
return m
}
func (m *meilisearch) Index(uid string) IndexManager {
return newIndex(m.client, uid)
}
func (m *meilisearch) GetIndex(indexID string) (*IndexResult, error) {
return m.GetIndexWithContext(context.Background(), indexID)
}
func (m *meilisearch) GetIndexWithContext(ctx context.Context, indexID string) (*IndexResult, error) {
return newIndex(m.client, indexID).FetchInfoWithContext(ctx)
}
func (m *meilisearch) GetRawIndex(uid string) (map[string]interface{}, error) {
return m.GetRawIndexWithContext(context.Background(), uid)
}
func (m *meilisearch) GetRawIndexWithContext(ctx context.Context, uid string) (map[string]interface{}, error) {
resp := map[string]interface{}{}
req := &internalRequest{
endpoint: "/indexes/" + uid,
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetRawIndex",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) ListIndexes(param *IndexesQuery) (*IndexesResults, error) {
return m.ListIndexesWithContext(context.Background(), param)
}
func (m *meilisearch) ListIndexesWithContext(ctx context.Context, param *IndexesQuery) (*IndexesResults, error) {
resp := new(IndexesResults)
req := &internalRequest{
endpoint: "/indexes",
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetIndexes",
}
if param != nil && param.Limit != 0 {
req.withQueryParams["limit"] = strconv.FormatInt(param.Limit, 10)
}
if param != nil && param.Offset != 0 {
req.withQueryParams["offset"] = strconv.FormatInt(param.Offset, 10)
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
for i := range resp.Results {
resp.Results[i].IndexManager = newIndex(m.client, resp.Results[i].UID)
}
return resp, nil
}
func (m *meilisearch) GetRawIndexes(param *IndexesQuery) (map[string]interface{}, error) {
return m.GetRawIndexesWithContext(context.Background(), param)
}
func (m *meilisearch) GetRawIndexesWithContext(ctx context.Context, param *IndexesQuery) (map[string]interface{}, error) {
resp := map[string]interface{}{}
req := &internalRequest{
endpoint: "/indexes",
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetRawIndexes",
}
if param != nil && param.Limit != 0 {
req.withQueryParams["limit"] = strconv.FormatInt(param.Limit, 10)
}
if param != nil && param.Offset != 0 {
req.withQueryParams["offset"] = strconv.FormatInt(param.Offset, 10)
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) CreateIndex(config *IndexConfig) (*TaskInfo, error) {
return m.CreateIndexWithContext(context.Background(), config)
}
func (m *meilisearch) CreateIndexWithContext(ctx context.Context, config *IndexConfig) (*TaskInfo, error) {
request := &CreateIndexRequest{
UID: config.Uid,
PrimaryKey: config.PrimaryKey,
}
resp := new(TaskInfo)
req := &internalRequest{
endpoint: "/indexes",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: request,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "CreateIndex",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) DeleteIndex(uid string) (*TaskInfo, error) {
return m.DeleteIndexWithContext(context.Background(), uid)
}
func (m *meilisearch) DeleteIndexWithContext(ctx context.Context, uid string) (*TaskInfo, error) {
resp := new(TaskInfo)
req := &internalRequest{
endpoint: "/indexes/" + uid,
method: http.MethodDelete,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "DeleteIndex",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) MultiSearch(queries *MultiSearchRequest) (*MultiSearchResponse, error) {
return m.MultiSearchWithContext(context.Background(), queries)
}
func (m *meilisearch) MultiSearchWithContext(ctx context.Context, queries *MultiSearchRequest) (*MultiSearchResponse, error) {
resp := new(MultiSearchResponse)
for i := 0; i < len(queries.Queries); i++ {
queries.Queries[i].validate()
}
req := &internalRequest{
endpoint: "/multi-search",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: queries,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "MultiSearch",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) CreateKey(request *Key) (*Key, error) {
return m.CreateKeyWithContext(context.Background(), request)
}
func (m *meilisearch) CreateKeyWithContext(ctx context.Context, request *Key) (*Key, error) {
parsedRequest := convertKeyToParsedKey(*request)
resp := new(Key)
req := &internalRequest{
endpoint: "/keys",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: &parsedRequest,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusCreated},
functionName: "CreateKey",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) GetKey(identifier string) (*Key, error) {
return m.GetKeyWithContext(context.Background(), identifier)
}
func (m *meilisearch) GetKeyWithContext(ctx context.Context, identifier string) (*Key, error) {
resp := new(Key)
req := &internalRequest{
endpoint: "/keys/" + identifier,
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetKey",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) GetKeys(param *KeysQuery) (*KeysResults, error) {
return m.GetKeysWithContext(context.Background(), param)
}
func (m *meilisearch) GetKeysWithContext(ctx context.Context, param *KeysQuery) (*KeysResults, error) {
resp := new(KeysResults)
req := &internalRequest{
endpoint: "/keys",
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetKeys",
}
if param != nil && param.Limit != 0 {
req.withQueryParams["limit"] = strconv.FormatInt(param.Limit, 10)
}
if param != nil && param.Offset != 0 {
req.withQueryParams["offset"] = strconv.FormatInt(param.Offset, 10)
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) UpdateKey(keyOrUID string, request *Key) (*Key, error) {
return m.UpdateKeyWithContext(context.Background(), keyOrUID, request)
}
func (m *meilisearch) UpdateKeyWithContext(ctx context.Context, keyOrUID string, request *Key) (*Key, error) {
parsedRequest := KeyUpdate{Name: request.Name, Description: request.Description}
resp := new(Key)
req := &internalRequest{
endpoint: "/keys/" + keyOrUID,
method: http.MethodPatch,
contentType: contentTypeJSON,
withRequest: &parsedRequest,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "UpdateKey",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) DeleteKey(keyOrUID string) (bool, error) {
return m.DeleteKeyWithContext(context.Background(), keyOrUID)
}
func (m *meilisearch) DeleteKeyWithContext(ctx context.Context, keyOrUID string) (bool, error) {
req := &internalRequest{
endpoint: "/keys/" + keyOrUID,
method: http.MethodDelete,
withRequest: nil,
withResponse: nil,
acceptedStatusCodes: []int{http.StatusNoContent},
functionName: "DeleteKey",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return false, err
}
return true, nil
}
func (m *meilisearch) GetTask(taskUID int64) (*Task, error) {
return m.GetTaskWithContext(context.Background(), taskUID)
}
func (m *meilisearch) GetTaskWithContext(ctx context.Context, taskUID int64) (*Task, error) {
return getTask(ctx, m.client, taskUID)
}
func (m *meilisearch) GetTasks(param *TasksQuery) (*TaskResult, error) {
return m.GetTasksWithContext(context.Background(), param)
}
func (m *meilisearch) GetTasksWithContext(ctx context.Context, param *TasksQuery) (*TaskResult, error) {
resp := new(TaskResult)
req := &internalRequest{
endpoint: "/tasks",
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetTasks",
}
if param != nil {
encodeTasksQuery(param, req)
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) CancelTasks(param *CancelTasksQuery) (*TaskInfo, error) {
return m.CancelTasksWithContext(context.Background(), param)
}
func (m *meilisearch) CancelTasksWithContext(ctx context.Context, param *CancelTasksQuery) (*TaskInfo, error) {
resp := new(TaskInfo)
req := &internalRequest{
endpoint: "/tasks/cancel",
method: http.MethodPost,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "CancelTasks",
}
if param != nil {
paramToSend := &TasksQuery{
UIDS: param.UIDS,
IndexUIDS: param.IndexUIDS,
Statuses: param.Statuses,
Types: param.Types,
BeforeEnqueuedAt: param.BeforeEnqueuedAt,
AfterEnqueuedAt: param.AfterEnqueuedAt,
BeforeStartedAt: param.BeforeStartedAt,
AfterStartedAt: param.AfterStartedAt,
}
encodeTasksQuery(paramToSend, req)
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) DeleteTasks(param *DeleteTasksQuery) (*TaskInfo, error) {
return m.DeleteTasksWithContext(context.Background(), param)
}
func (m *meilisearch) DeleteTasksWithContext(ctx context.Context, param *DeleteTasksQuery) (*TaskInfo, error) {
resp := new(TaskInfo)
req := &internalRequest{
endpoint: "/tasks",
method: http.MethodDelete,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "DeleteTasks",
}
if param != nil {
paramToSend := &TasksQuery{
UIDS: param.UIDS,
IndexUIDS: param.IndexUIDS,
Statuses: param.Statuses,
Types: param.Types,
CanceledBy: param.CanceledBy,
BeforeEnqueuedAt: param.BeforeEnqueuedAt,
AfterEnqueuedAt: param.AfterEnqueuedAt,
BeforeStartedAt: param.BeforeStartedAt,
AfterStartedAt: param.AfterStartedAt,
BeforeFinishedAt: param.BeforeFinishedAt,
AfterFinishedAt: param.AfterFinishedAt,
}
encodeTasksQuery(paramToSend, req)
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) SwapIndexes(param []*SwapIndexesParams) (*TaskInfo, error) {
return m.SwapIndexesWithContext(context.Background(), param)
}
func (m *meilisearch) SwapIndexesWithContext(ctx context.Context, param []*SwapIndexesParams) (*TaskInfo, error) {
resp := new(TaskInfo)
req := &internalRequest{
endpoint: "/swap-indexes",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: param,
withResponse: &resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "SwapIndexes",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) WaitForTask(taskUID int64, interval time.Duration) (*Task, error) {
return waitForTask(context.Background(), m.client, taskUID, interval)
}
func (m *meilisearch) WaitForTaskWithContext(ctx context.Context, taskUID int64, interval time.Duration) (*Task, error) {
return waitForTask(ctx, m.client, taskUID, interval)
}
func (m *meilisearch) GenerateTenantToken(
apiKeyUID string,
searchRules map[string]interface{},
options *TenantTokenOptions,
) (string, error) {
// validate the arguments
if searchRules == nil {
return "", fmt.Errorf("GenerateTenantToken: The search rules added in the token generation " +
"must be of type array or object")
}
if (options == nil || options.APIKey == "") && m.client.apiKey == "" {
return "", fmt.Errorf("GenerateTenantToken: The API key used for the token " +
"generation must exist and be a valid meilisearch key")
}
if apiKeyUID == "" || !IsValidUUID(apiKeyUID) {
return "", fmt.Errorf("GenerateTenantToken: The uid used for the token " +
"generation must exist and comply to uuid4 format")
}
if options != nil && !options.ExpiresAt.IsZero() && options.ExpiresAt.Before(time.Now()) {
return "", fmt.Errorf("GenerateTenantToken: When the expiresAt field in " +
"the token generation has a value, it must be a date set in the future")
}
var secret string
if options == nil || options.APIKey == "" {
secret = m.client.apiKey
} else {
secret = options.APIKey
}
// For HMAC signing method, the key should be any []byte
hmacSampleSecret := []byte(secret)
// Create the claims
claims := TenantTokenClaims{}
if options != nil && !options.ExpiresAt.IsZero() {
claims.RegisteredClaims = jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(options.ExpiresAt),
}
}
claims.APIKeyUID = apiKeyUID
claims.SearchRules = searchRules
// Create a new token object, specifying signing method and the claims
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// Sign and get the complete encoded token as a string using the secret
tokenString, err := token.SignedString(hmacSampleSecret)
return tokenString, err
}
func (m *meilisearch) GetStats() (*Stats, error) {
return m.GetStatsWithContext(context.Background())
}
func (m *meilisearch) GetStatsWithContext(ctx context.Context) (*Stats, error) {
resp := new(Stats)
req := &internalRequest{
endpoint: "/stats",
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetStats",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) CreateDump() (*TaskInfo, error) {
return m.CreateDumpWithContext(context.Background())
}
func (m *meilisearch) CreateDumpWithContext(ctx context.Context) (*TaskInfo, error) {
resp := new(TaskInfo)
req := &internalRequest{
endpoint: "/dumps",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "CreateDump",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) Version() (*Version, error) {
return m.VersionWithContext(context.Background())
}
func (m *meilisearch) VersionWithContext(ctx context.Context) (*Version, error) {
resp := new(Version)
req := &internalRequest{
endpoint: "/version",
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "Version",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) Health() (*Health, error) {
return m.HealthWithContext(context.Background())
}
func (m *meilisearch) HealthWithContext(ctx context.Context) (*Health, error) {
resp := new(Health)
req := &internalRequest{
endpoint: "/health",
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "Health",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) CreateSnapshot() (*TaskInfo, error) {
return m.CreateSnapshotWithContext(context.Background())
}
func (m *meilisearch) CreateSnapshotWithContext(ctx context.Context) (*TaskInfo, error) {
resp := new(TaskInfo)
req := &internalRequest{
endpoint: "/snapshots",
method: http.MethodPost,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
contentType: contentTypeJSON,
functionName: "CreateSnapshot",
}
if err := m.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func (m *meilisearch) IsHealthy() bool {
res, err := m.HealthWithContext(context.Background())
return err == nil && res.Status == "available"
}
func (m *meilisearch) Close() {
m.client.client.CloseIdleConnections()
}
func getTask(ctx context.Context, cli *client, taskUID int64) (*Task, error) {
resp := new(Task)
req := &internalRequest{
endpoint: "/tasks/" + strconv.FormatInt(taskUID, 10),
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetTask",
}
if err := cli.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}
func waitForTask(ctx context.Context, cli *client, taskUID int64, interval time.Duration) (*Task, error) {
if interval == 0 {
interval = 50 * time.Millisecond
}
// extract closure to get the task and check the status first before the ticker
fn := func() (*Task, error) {
getTask, err := getTask(ctx, cli, taskUID)
if err != nil {
return nil, err
}
if getTask.Status != TaskStatusEnqueued && getTask.Status != TaskStatusProcessing {
return getTask, nil
}
return nil, nil
}
// run first before the ticker, we do not want to wait for the first interval
task, err := fn()
if err != nil {
// Return error if it exists
return nil, err
}
// Return task if it exists
if task != nil {
return task, nil
}
// Create a ticker to check the task status, because our initial check was not successful
ticker := time.NewTicker(interval)
// Defer the stop of the ticker, help GC to cleanup
defer func() {
// we might want to revist this, go.mod now is 1.16
// however I still encouter the issue on go 1.22.2
// there are 2 issues regarding tickers
// https://go-review.googlesource.com/c/go/+/512355
// https://github.com/golang/go/issues/61542
ticker.Stop()
ticker = nil
}()
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-ticker.C:
task, err := fn()
if err != nil {
return nil, err
}
if task != nil {
return task, nil
}
}
}
}