aneka 0.17.5 → 0.17.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGES.md +4 -0
- package/package.json +1 -1
- package/src/set-interval.js +3 -2
- package/test/set-interval.js +16 -0
package/CHANGES.md
CHANGED
package/package.json
CHANGED
package/src/set-interval.js
CHANGED
|
@@ -17,13 +17,13 @@ import fs from 'fs-extra'
|
|
|
17
17
|
* @param {string} [options.lockFile] The path to the lock file. If provided, the handler will only execute if the lock can be acquired.
|
|
18
18
|
* @param {Array} [options.args=[]] Arguments to pass to the handler.
|
|
19
19
|
* @param {Object} [options.lockFileOpts] Options for the lock file.
|
|
20
|
-
* @param {boolean} [options.silent=
|
|
20
|
+
* @param {boolean} [options.silent=false] If true, errors during lock acquisition will be ignored.
|
|
21
21
|
* @param {number} [options.maxIterations=Infinity] Maximum number of times to execute the handler.
|
|
22
22
|
* @param {Object} [options.scope] The scope to bind the handler to.
|
|
23
23
|
* @returns {Promise<void>} Promise resolved after the interval.
|
|
24
24
|
*/
|
|
25
25
|
async function setInterval (handler, timeout, options = {}) {
|
|
26
|
-
const { lockFile, args = [], lockFileOpts, silent =
|
|
26
|
+
const { lockFile, args = [], lockFileOpts, silent = false, maxIterations = Infinity, scope } = options
|
|
27
27
|
timeout = parseDuration(timeout)
|
|
28
28
|
let iterations = 0
|
|
29
29
|
|
|
@@ -39,6 +39,7 @@ async function setInterval (handler, timeout, options = {}) {
|
|
|
39
39
|
await release()
|
|
40
40
|
} catch (err) {
|
|
41
41
|
if (!silent) throw err
|
|
42
|
+
console.log(err)
|
|
42
43
|
}
|
|
43
44
|
}
|
|
44
45
|
|
package/test/set-interval.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/* eslint-env mocha */
|
|
2
2
|
import { expect } from 'chai'
|
|
3
3
|
import lockfile from '@bybrave/proper-lockfile2'
|
|
4
|
+
import fs from 'fs-extra'
|
|
4
5
|
import fn from '../src/set-interval.js'
|
|
5
6
|
|
|
6
7
|
const originalSetTimeout = global.setTimeout
|
|
7
8
|
const originalClearTimeout = global.clearTimeout
|
|
8
9
|
const originalLock = lockfile.lock
|
|
10
|
+
const originalEnsureFileSync = fs.ensureFileSync
|
|
9
11
|
|
|
10
12
|
async function flushMicrotasks (turns = 6) {
|
|
11
13
|
for (let index = 0; index < turns; index++) {
|
|
@@ -52,6 +54,7 @@ describe('setInterval', function () {
|
|
|
52
54
|
global.setTimeout = originalSetTimeout
|
|
53
55
|
global.clearTimeout = originalClearTimeout
|
|
54
56
|
lockfile.lock = originalLock
|
|
57
|
+
fs.ensureFileSync = originalEnsureFileSync
|
|
55
58
|
})
|
|
56
59
|
|
|
57
60
|
it('returns a promise and schedules the first execution immediately', async function () {
|
|
@@ -133,6 +136,10 @@ describe('setInterval', function () {
|
|
|
133
136
|
it('locks and releases around every iteration when a lock file is configured', async function () {
|
|
134
137
|
const timers = installTimerQueue()
|
|
135
138
|
const calls = []
|
|
139
|
+
const ensured = []
|
|
140
|
+
fs.ensureFileSync = lockFile => {
|
|
141
|
+
ensured.push(lockFile)
|
|
142
|
+
}
|
|
136
143
|
lockfile.lock = async (lockFile, lockFileOpts) => {
|
|
137
144
|
calls.push(['lock', lockFile, lockFileOpts])
|
|
138
145
|
return async () => {
|
|
@@ -149,6 +156,10 @@ describe('setInterval', function () {
|
|
|
149
156
|
|
|
150
157
|
await timers.drainAll()
|
|
151
158
|
|
|
159
|
+
expect(ensured).to.deep.equal([
|
|
160
|
+
'/tmp/aneka-set-interval.lock',
|
|
161
|
+
'/tmp/aneka-set-interval.lock'
|
|
162
|
+
])
|
|
152
163
|
expect(calls).to.deep.equal([
|
|
153
164
|
['lock', '/tmp/aneka-set-interval.lock', { stale: 2000 }],
|
|
154
165
|
['handler'],
|
|
@@ -162,6 +173,10 @@ describe('setInterval', function () {
|
|
|
162
173
|
it('suppresses lock errors when silent is left at the default', async function () {
|
|
163
174
|
const timers = installTimerQueue()
|
|
164
175
|
const calls = []
|
|
176
|
+
const ensured = []
|
|
177
|
+
fs.ensureFileSync = lockFile => {
|
|
178
|
+
ensured.push(lockFile)
|
|
179
|
+
}
|
|
165
180
|
lockfile.lock = async () => {
|
|
166
181
|
throw new Error('lock failed')
|
|
167
182
|
}
|
|
@@ -174,6 +189,7 @@ describe('setInterval', function () {
|
|
|
174
189
|
|
|
175
190
|
await timers.drainAll()
|
|
176
191
|
|
|
192
|
+
expect(ensured).to.deep.equal(['/tmp/aneka-set-interval.lock'])
|
|
177
193
|
expect(calls).to.deep.equal([])
|
|
178
194
|
expect(timers.queue).to.have.length(0)
|
|
179
195
|
})
|