// Copyright 2021 Kentaro Hibino. All rights reserved.// Use of this source code is governed by a MIT license// that can be found in the LICENSE file.package asynqimport ()// A janitor is responsible for deleting expired completed tasks from the specified// queues. It periodically checks for any expired tasks in the completed set, and// deletes them.type janitor struct { logger *log.Logger broker base.Broker// channel to communicate back to the long running "janitor" goroutine. done chanstruct{}// list of queue names to check. queues []string// average interval between checks. avgInterval time.Duration// number of tasks to be deleted when janitor runs to delete the expired completed tasks. batchSize int}type janitorParams struct { logger *log.Logger broker base.Broker queues []string interval time.Duration batchSize int}func newJanitor( janitorParams) *janitor {return &janitor{logger: .logger,broker: .broker,done: make(chanstruct{}),queues: .queues,avgInterval: .interval,batchSize: .batchSize, }}func ( *janitor) () { .logger.Debug("Janitor shutting down...")// Signal the janitor goroutine to stop. .done <- struct{}{}}// start starts the "janitor" goroutine.func ( *janitor) ( *sync.WaitGroup) { .Add(1) := time.NewTimer(.avgInterval) // randomize this interval with margin of 1sgofunc() {defer .Done()for {select {case<-.done: .logger.Debug("Janitor done")returncase<-.C: .exec() .Reset(.avgInterval) } } }()}func ( *janitor) () {for , := range .queues {if := .broker.DeleteExpiredCompletedTasks(, .batchSize); != nil { .logger.Errorf("Failed to delete expired completed tasks from queue %q: %v", , ) } }}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.