Skip to content

Commit 5563ac0

Browse files
committed
Add randomized timing for polling to keep multiple instances of the software from polling the same servers at the same time. Fix tests broken by recent major npm package updates.
1 parent 233ac74 commit 5563ac0

11 files changed

Lines changed: 42 additions & 40 deletions

e2e/tsconfig.e2e.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"outDir": "../out-tsc/e2e",
55
"baseUrl": "./",
66
"module": "commonjs",
7-
"target": "es5",
7+
"target": "es6",
88
"types": [
99
"jasmine",
1010
"jasminewd2",

package-lock.json

Lines changed: 1 addition & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aw-clock",
3-
"version": "1.2.4",
3+
"version": "1.2.5",
44
"license": "MIT",
55
"author": "Kerry Shetline <kerry@shetline.com>",
66
"scripts": {
@@ -58,7 +58,6 @@
5858
"karma-webpack": "^4.0.2",
5959
"less-loader": "^5.0.0",
6060
"node-sass": "^4.13.1",
61-
"null-loader": "^3.0.0",
6261
"postcss-import": "^12.0.1",
6362
"postcss-loader": "^3.0.0",
6463
"postcss-url": "^8.0.0",

protractor.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ exports.config = {
4747
let resolved = false;
4848
let rejected = false;
4949

50-
webpackServerProcess = spawn('webpack-dev-server', ['--port=4200']);
50+
webpackServerProcess = spawn('webpack-dev-server', ['--mode=development', '--port=4200']);
5151
webpackServerProcess.stdout.pipe(process.stdout);
5252

5353
webpackServerProcess.stdout.addListener('data', chunk => {

server/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aw-clock-server",
3-
"version": "1.2.4",
3+
"version": "1.2.5",
44
"license": "MIT",
55
"author": "Kerry Shetline <kerry@shetline.com>",
66
"private": true,

server/src/tai-utc.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const DEFAULT_LEAP_SECOND_URLS = DEFAULT_LEAP_SECOND_HTTPS_URL + ';' + DE
2222
const NTP_BASE = 2208988800; // Seconds before 1970-01-01 epoch for 1900-01-01 epoch
2323
const MILLIS_PER_DAY = 86400000;
2424
const DAYS_BETWEEN_POLLS = 7;
25+
const MAX_RANDOM_LEAP_SECOND_POLL_DELAY = 120000; // Two minutes
2526
const TIMEOUT = 5000;
2627
const TIME_AND_DELTA = /^(\d{10,})\s+(\d{2,4})\s*#\s*1\s+[A-Za-z]{3}\s+\d{4}/;
2728

@@ -30,6 +31,7 @@ function makeError(err: any): Error {
3031
}
3132

3233
export class TaiUtc {
34+
private firstLeapSecondPoll = true;
3335
private lastPollDay = 0;
3436
private lastPollMonth = -1;
3537
private leapSeconds: LeapSecond[] = [];
@@ -86,6 +88,13 @@ export class TaiUtc {
8688
if (this.leapSeconds.length > 1 && this.lastPollDay < day + DAYS_BETWEEN_POLLS && this.lastPollMonth === month)
8789
return;
8890

91+
await new Promise<void>(resolve => {
92+
// Randomly delay polling so that multiple TaiUtc instances don't all poll at the same time every day.
93+
const delay = (this.firstLeapSecondPoll ? 0 : Math.floor(Math.random() * MAX_RANDOM_LEAP_SECOND_POLL_DELAY));
94+
this.firstLeapSecondPoll = false;
95+
setTimeout(() => resolve(), delay);
96+
});
97+
8998
const promises: Promise<string | Error>[] = [];
9099

91100
this.urls.forEach(url => {

spec.bundle.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
const testsContext = require.context('./', true, /\.spec\.ts$/);
2-
testsContext.keys().forEach(key => {
3-
if (key.indexOf('/server/') < 0) { testsContext(key); }
4-
});
1+
const context = require.context('./src/', true, /\.spec\.ts$/);
2+
context.keys().forEach(context);

src/clock.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import * as $ from 'jquery';
2828
const SVG_NAMESPACE = 'http://www.w3.org/2000/svg';
2929

3030
const SECOND_HAND_ANIMATION_TIME = 200;
31+
const MAX_RANDOM_LEAP_SECOND_POLL_DELAY = 120000; // Two minutes
3132
const LEAP_SECOND_RETRY_DELAY = 300000; // 5 minutes
3233

3334
const MILLIS_PER_DAY = 86400000;
@@ -63,6 +64,7 @@ export class Clock {
6364
private lastTick = -1;
6465
private inMinuteOfLeapSecond = false;
6566
private pendingLeapSecondForMonth = 0;
67+
private firstLeapSecondPoll = true;
6668
private lastLeapSecondCheckDay = -1;
6769
private upcomingLeapSecond: CurrentDelta;
6870

@@ -349,15 +351,20 @@ export class Clock {
349351
}
350352

351353
private getLeapSecondInfo(): void {
352-
// noinspection JSIgnoredPromiseFromCall
353-
$.ajax({
354-
url: this.appService.getWeatherServer() + '/tai-utc',
355-
dataType: 'json',
356-
success: (data: CurrentDelta) => this.upcomingLeapSecond = data,
357-
error: () => setTimeout(() => {
358-
this.upcomingLeapSecond = undefined;
359-
this.lastLeapSecondCheckDay = -1;
360-
}, LEAP_SECOND_RETRY_DELAY)
361-
});
354+
setTimeout(() => {
355+
this.firstLeapSecondPoll = false;
356+
357+
// noinspection JSIgnoredPromiseFromCall
358+
$.ajax({
359+
url: this.appService.getWeatherServer() + '/tai-utc',
360+
dataType: 'json',
361+
success: (data: CurrentDelta) => this.upcomingLeapSecond = data,
362+
error: () => setTimeout(() => {
363+
this.upcomingLeapSecond = undefined;
364+
this.lastLeapSecondCheckDay = -1;
365+
}, LEAP_SECOND_RETRY_DELAY)
366+
});
367+
// Randomly delay polling so that multiple clock instances don't all poll at the same time every day.
368+
}, this.firstLeapSecondPoll ? 0 : Math.floor(Math.random() * MAX_RANDOM_LEAP_SECOND_POLL_DELAY));
362369
}
363370
}

src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
</div>
193193
</div>
194194
<div class="dialog-buttons">
195-
<span class="version-number">1.2.4</span>
195+
<span class="version-number">1.2.5</span>
196196
<button id="settings-reload">Reload</button>
197197
<span>&bull;</span>
198198
<button id="settings-cancel">Cancel</button>

0 commit comments

Comments
 (0)