@@ -11,10 +11,14 @@ import (
1111)
1212
1313const redisNamespace = "boomerang"
14+
1415var (
15- ErrUnexpectedReturnCodeFromRedis = errors .New ("unexpected return code from redis" )
16- ErrTaskAlreadyExists = errors .New ("task already exists" )
17- ErrTaskDoesNotExist = errors .New ("task does not exist" )
16+ ErrUnexpectedReturnCode = errors .New ("unexpected return code from redis" )
17+ ErrUnexpectedReturnCodeType = errors .New ("unexpected return code type from redis, expect integer" )
18+ ErrTaskAlreadyExists = errors .New ("task already exists" )
19+ ErrTaskDoesNotExist = errors .New ("task does not exist" )
20+ ErrTaskDataDoesNotExist = errors .New ("task data does not exist" )
21+ ErrTaskDataInvalidFormat = errors .New ("task data has invalid format, expected JSON" )
1822)
1923
2024type Schedule interface {
@@ -96,7 +100,7 @@ func (s *ScheduleImpl) Add(ctx context.Context, task *Task) error {
96100 case - 1 :
97101 return ErrTaskAlreadyExists
98102 default :
99- return ErrUnexpectedReturnCodeFromRedis
103+ return ErrUnexpectedReturnCode
100104 }
101105}
102106
@@ -160,7 +164,7 @@ func (s *ScheduleImpl) Update(ctx context.Context, task *Task) error {
160164 case - 1 :
161165 return ErrTaskDoesNotExist
162166 default :
163- return ErrUnexpectedReturnCodeFromRedis
167+ return ErrUnexpectedReturnCode
164168 }
165169}
166170
@@ -203,7 +207,7 @@ func (s *ScheduleImpl) Remove(ctx context.Context, kind string, id string) error
203207 case - 1 :
204208 return ErrTaskDoesNotExist
205209 default :
206- return ErrUnexpectedReturnCodeFromRedis
210+ return ErrUnexpectedReturnCode
207211 }
208212}
209213
@@ -250,7 +254,7 @@ func (s *ScheduleImpl) RunNow(ctx context.Context, kind string, id string) error
250254 case - 1 :
251255 return ErrTaskDoesNotExist
252256 default :
253- return ErrUnexpectedReturnCodeFromRedis
257+ return ErrUnexpectedReturnCode
254258 }
255259}
256260
@@ -267,6 +271,7 @@ func (s *ScheduleImpl) On(ctx context.Context, kind string, handler func(ctx con
267271
268272 local res = redis.call("ZPOPMIN", queueKey)
269273 if #res == 0 then
274+ -- Error: No tasks scheduled
270275 return { -1 }
271276 end
272277
@@ -277,19 +282,23 @@ func (s *ScheduleImpl) On(ctx context.Context, kind string, handler func(ctx con
277282
278283 if score > (now + 1000) then
279284 redis.call("ZADD", queueKey, score, id)
285+
286+ -- Error: Next task is scheduled for more than 1 second in the future
280287 return { -1 }
281288 end
282289
283290 -- Get the task data
284291
285292 local taskDataRaw = redis.call("HGET", taskDataKey, id)
286293 if taskDataRaw == nil then
287- return { -1 }
294+ -- Error: task data does not exist
295+ return { -2 }
288296 end
289297
290298 local taskData = cjson.decode(taskDataRaw)
291299 if taskData == nil then
292- return { -1 }
300+ -- Error: task data has invalid format
301+ return { -3 }
293302 end
294303
295304 -- Schedule the next execution
@@ -335,21 +344,38 @@ func (s *ScheduleImpl) On(ctx context.Context, kind string, handler func(ctx con
335344 return err
336345 }
337346
338- if len (resSlice ) != 3 {
347+ code , ok := resSlice [0 ].(int64 )
348+ if ! ok {
349+ return ErrUnexpectedReturnCodeType
350+ }
351+
352+ if code == - 1 {
339353 select {
340354 case <- ctx .Done ():
341355 return ctx .Err ()
342- case <- time .After (1 * time .Second ):
356+ case <- time .After (time .Second ):
343357 continue
344358 }
345359 }
346360
347- id , ok := resSlice [0 ].(string )
361+ if code == - 2 {
362+ return ErrTaskDataDoesNotExist
363+ }
364+
365+ if code == - 3 {
366+ return ErrTaskDataInvalidFormat
367+ }
368+
369+ if code != 0 {
370+ return ErrUnexpectedReturnCode
371+ }
372+
373+ id , ok := resSlice [1 ].(string )
348374 if ! ok {
349375 return errors .New ("unexpected type for id" )
350376 }
351377
352- score , ok := resSlice [1 ].(int64 )
378+ score , ok := resSlice [2 ].(int64 )
353379 if ! ok {
354380 return errors .New ("unexpected type for score" )
355381 }
@@ -359,7 +385,7 @@ func (s *ScheduleImpl) On(ctx context.Context, kind string, handler func(ctx con
359385 time .Sleep (time .Duration (delta ) * time .Millisecond )
360386 }
361387
362- taskDataRaw , ok := resSlice [2 ].(string )
388+ taskDataRaw , ok := resSlice [3 ].(string )
363389 if ! ok {
364390 return errors .New ("unexpected type for taskDataRaw" )
365391 }
0 commit comments