@@ -143,15 +143,42 @@ func (q *scheduledQueue) NotifyNewTask(clusterName string, info *hcommon.NotifyT
143143 return
144144 }
145145
146- nextTime := info .Tasks [0 ].GetVisibilityTimestamp ()
147- for i := 1 ; i < numTasks ; i ++ {
148- ts := info .Tasks [i ].GetVisibilityTimestamp ()
149- if ts .Before (nextTime ) {
150- nextTime = ts
146+ q .base .logger .Debug (
147+ "New timer task notification received" ,
148+ tag .Dynamic ("numTasks" , numTasks ),
149+ tag .Dynamic ("scheduleInMemory" , info .ScheduleInMemory ),
150+ tag .Dynamic ("persistenceError" , info .PersistenceError ),
151+ tag .Dynamic ("shardId" , q .base .shard .GetShardID ()),
152+ )
153+
154+ tasksToBeReadFromDB := make ([]persistence.Task , 0 )
155+
156+ if info .ScheduleInMemory && ! info .PersistenceError {
157+ for _ , task := range info .Tasks {
158+ ts := task .GetVisibilityTimestamp ()
159+ q .base .logger .Debug ("Submitting task to an in-memory queue" , tag .Dynamic ("scheduledTime" , ts ), tag .Dynamic ("shardId" , q .base .shard .GetShardID ()))
160+
161+ if ! q .base .insertSingleTask (q .base .taskInitializer (task )) {
162+ tasksToBeReadFromDB = append (tasksToBeReadFromDB , task )
163+ }
151164 }
165+ } else {
166+ tasksToBeReadFromDB = info .Tasks
167+ }
168+
169+ var nextReadTime time.Time
170+ for _ , task := range tasksToBeReadFromDB {
171+ ts := task .GetVisibilityTimestamp ()
172+ if nextReadTime .IsZero () || ts .Before (nextReadTime ) {
173+ nextReadTime = ts
174+ }
175+ }
176+
177+ if ! nextReadTime .IsZero () {
178+ q .base .removeScheduledTasksAfter (nextReadTime )
179+ q .notify (nextReadTime )
152180 }
153181
154- q .notify (nextTime )
155182 q .base .metricsScope .AddCounter (metrics .NewHistoryTaskCounter , int64 (numTasks ))
156183}
157184
0 commit comments