Files
tasks/service/history/queues/queue_immediate.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

170 lines
3.5 KiB
Go

package queues
import (
"sync/atomic"
"time"
"github.com/hanzoai/tasks/common"
"github.com/hanzoai/tasks/common/backoff"
"github.com/hanzoai/tasks/common/collection"
"github.com/hanzoai/tasks/common/log"
"github.com/hanzoai/tasks/common/log/tag"
"github.com/hanzoai/tasks/common/metrics"
"github.com/hanzoai/tasks/common/persistence"
"github.com/hanzoai/tasks/common/quotas"
historyi "github.com/hanzoai/tasks/service/history/interfaces"
"github.com/hanzoai/tasks/service/history/tasks"
)
var _ Queue = (*immediateQueue)(nil)
type (
immediateQueue struct {
*queueBase
notifyCh chan struct{}
}
)
func NewImmediateQueue(
shard historyi.ShardContext,
category tasks.Category,
scheduler Scheduler,
rescheduler Rescheduler,
options *Options,
hostRateLimiter quotas.RequestRateLimiter,
grouper Grouper,
logger log.Logger,
metricsHandler metrics.Handler,
factory ExecutableFactory,
) *immediateQueue {
paginationFnProvider := func(r Range) collection.PaginationFn[tasks.Task] {
return func(paginationToken []byte) ([]tasks.Task, []byte, error) {
ctx, cancel := newQueueIOContext()
defer cancel()
request := &persistence.GetHistoryTasksRequest{
ShardID: shard.GetShardID(),
TaskCategory: category,
InclusiveMinTaskKey: r.InclusiveMin,
ExclusiveMaxTaskKey: r.ExclusiveMax,
BatchSize: options.BatchSize(),
NextPageToken: paginationToken,
}
resp, err := shard.GetHistoryTasks(ctx, request)
if err != nil {
return nil, nil, err
}
return resp.Tasks, resp.NextPageToken, nil
}
}
return &immediateQueue{
queueBase: newQueueBase(
shard,
category,
paginationFnProvider,
scheduler,
rescheduler,
factory,
options,
hostRateLimiter,
NoopReaderCompletionFn,
grouper,
logger,
metricsHandler,
),
notifyCh: make(chan struct{}, 1),
}
}
func (p *immediateQueue) Start() {
if !atomic.CompareAndSwapInt32(&p.status, common.DaemonStatusInitialized, common.DaemonStatusStarted) {
return
}
p.logger.Info("", tag.LifeCycleStarting)
defer p.logger.Info("", tag.LifeCycleStarted)
p.queueBase.Start()
p.shutdownWG.Add(1)
go p.processEventLoop()
p.notify()
}
func (p *immediateQueue) Stop() {
if !atomic.CompareAndSwapInt32(&p.status, common.DaemonStatusStarted, common.DaemonStatusStopped) {
return
}
p.logger.Info("", tag.LifeCycleStopping)
defer p.logger.Info("", tag.LifeCycleStopped)
close(p.shutdownCh)
if success := common.AwaitWaitGroup(&p.shutdownWG, time.Minute); !success {
p.logger.Warn("", tag.LifeCycleStopTimedout)
}
p.queueBase.Stop()
}
func (p *immediateQueue) NotifyNewTasks(tasks []tasks.Task) {
if len(tasks) == 0 {
return
}
p.notify()
}
func (p *immediateQueue) processEventLoop() {
defer p.shutdownWG.Done()
pollTimer := time.NewTimer(backoff.Jitter(
p.options.MaxPollInterval(),
p.options.MaxPollIntervalJitterCoefficient(),
))
defer pollTimer.Stop()
for {
select {
case <-p.shutdownCh:
return
default:
}
select {
case <-p.shutdownCh:
return
case <-p.notifyCh:
p.processNewRange()
case <-pollTimer.C:
p.processPollTimer(pollTimer)
case <-p.checkpointTimer.C:
p.checkpoint()
case alert := <-p.alertCh:
p.handleAlert(alert)
}
}
}
func (p *immediateQueue) processPollTimer(pollTimer *time.Timer) {
p.processNewRange()
pollTimer.Reset(backoff.Jitter(
p.options.MaxPollInterval(),
p.options.MaxPollIntervalJitterCoefficient(),
))
}
func (p *immediateQueue) notify() {
select {
case p.notifyCh <- struct{}{}:
default:
}
}