• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

haraka / Haraka / 25824044976

13 May 2026 08:17PM UTC coverage: 72.45% (-0.08%) from 72.533%
25824044976

push

github

web-flow
doc updates (#3559)

1527 of 2027 branches covered (75.33%)

6974 of 9626 relevant lines covered (72.45%)

23.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.86
/outbound/timer_queue.js
1
'use strict'
8✔
2

8✔
3
const logger = require('../logger')
8✔
4

8✔
5
class TQTimer {
8✔
6
    constructor(id, fire_time, cb) {
8✔
7
        this.id = id
11✔
8
        this.fire_time = fire_time
11✔
9
        this.cb = cb
11✔
10
    }
11✔
11

8✔
12
    cancel() {
8✔
13
        this.cb = null
2✔
14
    }
2✔
15
}
8✔
16

8✔
17
class TimerQueue {
8✔
18
    constructor(interval = 1000) {
8✔
19
        this.name = 'outbound/timer_queue'
14✔
20
        this.queue = []
14✔
21
        this.interval_timer = setInterval(() => {
14✔
22
            this.fire()
15✔
23
        }, interval)
14✔
24
        this.interval_timer.unref() // allow server to exit
14✔
25
    }
14✔
26

8✔
27
    add(id, ms, cb) {
8✔
28
        const fire_time = Date.now() + ms
11✔
29

11✔
30
        const timer = new TQTimer(id, fire_time, cb)
11✔
31

11✔
32
        if (this.queue.length === 0 || fire_time >= this.queue[this.queue.length - 1].fire_time) {
11✔
33
            this.queue.push(timer)
10✔
34
            return timer
10✔
35
        }
10✔
36

1✔
37
        for (let i = 0; i < this.queue.length; i++) {
1✔
38
            if (this.queue[i].fire_time > fire_time) {
1✔
39
                this.queue.splice(i, 0, timer)
1✔
40
                return timer
1✔
41
            }
1✔
42
        }
1✔
43

×
44
        throw 'Should never get here'
×
45
    }
11✔
46

8✔
47
    discard(id) {
8✔
48
        for (let i = 0; i < this.queue.length; i++) {
2✔
49
            if (this.queue[i].id === id) {
3✔
50
                this.queue[i].cancel()
2✔
51
                return this.queue.splice(i, 1)
2✔
52
            }
2✔
53
        }
3✔
54

×
55
        throw `${id} not found`
×
56
    }
2✔
57

8✔
58
    fire() {
8✔
59
        if (this.queue.length === 0) return
15✔
60

8✔
61
        const now = Date.now()
8✔
62

8✔
63
        while (this.queue.length && this.queue[0].fire_time <= now) {
15!
64
            const to_run = this.queue.shift()
×
65
            if (to_run.cb) to_run.cb()
×
66
        }
×
67
    }
15✔
68

8✔
69
    length() {
8✔
70
        return this.queue.length
11✔
71
    }
11✔
72

8✔
73
    drain() {
8✔
74
        logger.debug(this, `Draining ${this.queue.length} items from the queue`)
1✔
75
        while (this.queue.length) {
1✔
76
            const to_run = this.queue.shift()
2✔
77
            if (to_run.cb) to_run.cb()
2!
78
        }
2✔
79
    }
1✔
80

8✔
81
    shutdown() {
8✔
82
        clearInterval(this.interval_timer)
4✔
83
    }
4✔
84
}
8✔
85

8✔
86
module.exports = TimerQueue
8✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc