Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Settings listeners not receiving overwritten values from env vars #25448

Merged
merged 10 commits into from
May 12, 2022
2 changes: 1 addition & 1 deletion apps/meteor/app/cors/server/cors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { WebApp, WebAppInternals } from 'meteor/webapp';
import _ from 'underscore';

import { settings } from '../../settings/server';
import { Logger } from '../../logger';
import { Logger } from '../../logger/server';

const logger = new Logger('CORS');

Expand Down
10 changes: 4 additions & 6 deletions apps/meteor/app/federation/server/startup/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ settingsRegistry.addGroup('Federation', function () {
const updateSettings = async function (): Promise<void> {
// Get the key pair

if (getFederationDiscoveryMethod() === 'hub' && !Promise.await(isRegisteringOrEnabled())) {
if (getFederationDiscoveryMethod() === 'hub' && !(await isRegisteringOrEnabled())) {
// Register with hub
try {
await updateStatus(STATUS_REGISTERING);
Expand All @@ -89,20 +89,18 @@ const updateSettings = async function (): Promise<void> {
};

// Add settings listeners
settings.watch('FEDERATION_Enabled', function enableOrDisable(value) {
settings.watch('FEDERATION_Enabled', async function enableOrDisable(value) {
setupLogger.info(`Federation is ${value ? 'enabled' : 'disabled'}`);

if (value) {
Promise.await(updateSettings());
await updateSettings();

enableCallbacks();
} else {
Promise.await(updateStatus(STATUS_DISABLED));
await updateStatus(STATUS_DISABLED);

disableCallbacks();
}

value && updateSettings();
});

settings.watchMultiple(['FEDERATION_Discovery_Method', 'FEDERATION_Domain'], updateSettings);
8 changes: 0 additions & 8 deletions apps/meteor/app/lib/server/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import './startup/email';
import './startup/oAuthServicesUpdate';
import './startup/rateLimiter';
import './startup/robots';
import './startup/settings';
import './startup/settingsOnLoadCdnPrefix';
import './startup/settingsOnLoadDirectReply';
import './startup/settingsOnLoadSMTP';
import '../lib/MessageTypes';
import './lib/bugsnag';
import './lib/debug';
Expand Down
8 changes: 8 additions & 0 deletions apps/meteor/app/lib/server/startup/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import './email';
import './oAuthServicesUpdate';
import './rateLimiter';
import './robots';
import './settings';
import './settingsOnLoadCdnPrefix';
import './settingsOnLoadDirectReply';
import './settingsOnLoadSMTP';
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const getSecondsSinceLastAgentResponse = async (room, agentLastMessage) => {

callbacks.add(
'livechat.closeRoom',
async (room) => {
(room) => {
const closedByAgent = room.closer !== 'visitor';
const wasTheLastMessageSentByAgent = room.lastMessage && !room.lastMessage.token;
if (!closedByAgent || !wasTheLastMessageSentByAgent) {
Expand All @@ -68,8 +68,10 @@ callbacks.add(
if (!agentLastMessage) {
return;
}
const secondsSinceLastAgentResponse = await getSecondsSinceLastAgentResponse(room, agentLastMessage);
const secondsSinceLastAgentResponse = Promise.await(getSecondsSinceLastAgentResponse(room, agentLastMessage));
LivechatRooms.setVisitorInactivityInSecondsById(room._id, secondsSinceLastAgentResponse);

return room;
},
callbacks.priority.HIGH,
'process-room-abandonment',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RoutingManager } from '../lib/RoutingManager';

callbacks.add(
'afterSaveMessage',
async (message, room) => {
(message, room) => {
if (!isOmnichannelRoom(room)) {
return message;
}
Expand All @@ -26,7 +26,7 @@ callbacks.add(
return message;
}

await LivechatInquiry.setLastMessageByRoomId(room._id, message);
Promise.await(LivechatInquiry.setLastMessageByRoomId(room._id, message));

return message;
},
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/models/server/models/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _ from 'underscore';

import { Base } from './_Base';
import Rooms from './Rooms';
import { settings } from '../../../settings/server/functions/settings';
import { settings } from '../../../settings/server';
import { otrSystemMessages } from '../../../otr/lib/constants';

export class Messages extends Base {
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { escapeRegExp } from '@rocket.chat/string-helpers';

import { Base } from './_Base';
import Subscriptions from './Subscriptions';
import { settings } from '../../../settings/server/functions/settings';
import { settings } from '../../../settings/server';

const queryStatusAgentOnline = (extraFilters = {}) => ({
statusLivechat: 'available',
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/app/settings/server/SettingsRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export class SettingsRegistry {
}),
},
);

return;
}

Expand Down
63 changes: 63 additions & 0 deletions apps/meteor/app/settings/server/applyMiddlewares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Meteor } from 'meteor/meteor';

import { use } from './Middleware';
import { settings } from './cached';

const getProcessingTimeInMS = (time: [number, number]): number => time[0] * 1000 + time[1] / 1e6;

settings.watch = use(settings.watch, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});

if (process.env.DEBUG_SETTINGS === 'true') {
settings.watch = use(settings.watch, function watch(context, next) {
const [_id, callback, options] = context;
return next(
_id,
(...args) => {
const start = process.hrtime();
callback(...args);
const elapsed = process.hrtime(start);
console.log(`settings.watch: ${_id} ${getProcessingTimeInMS(elapsed)}ms`);
},
options,
);
});
}
settings.watchMultiple = use(settings.watchMultiple, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});
settings.watchOnce = use(settings.watchOnce, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});

settings.watchByRegex = use(settings.watchByRegex, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});

settings.change = use(settings.change, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});
settings.changeMultiple = use(settings.changeMultiple, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});
settings.changeOnce = use(settings.changeOnce, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});

settings.changeByRegex = use(settings.changeByRegex, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});

settings.onReady = use(settings.onReady, (context, next) => {
const [callback, ...args] = context;
return next(Meteor.bindEnvironment(callback), ...args);
});
3 changes: 3 additions & 0 deletions apps/meteor/app/settings/server/cached.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { CachedSettings } from './CachedSettings';

export const settings = new CachedSettings();
14 changes: 0 additions & 14 deletions apps/meteor/app/settings/server/functions/settings.ts

This file was deleted.

15 changes: 12 additions & 3 deletions apps/meteor/app/settings/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { settings, settingsRegistry } from './functions/settings';
import { SettingsEvents } from './SettingsRegistry';
import SettingsModel from '../../models/server/models/Settings';
import { SettingsRegistry } from './SettingsRegistry';
import { initializeSettings } from './startup';
import { settings } from './cached';
import './applyMiddlewares';

export { settings, settingsRegistry, SettingsEvents };
export { SettingsEvents } from './SettingsRegistry';

export { settings };

export const settingsRegistry = new SettingsRegistry({ store: settings, model: SettingsModel });

initializeSettings({ SettingsModel, settings });
70 changes: 11 additions & 59 deletions apps/meteor/app/settings/server/startup.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,15 @@
import { Meteor } from 'meteor/meteor';
import type { ISetting } from '@rocket.chat/core-typings';

import { use } from './Middleware';
import { settings } from './functions/settings';
import { Settings } from '../../models/server/models/Settings';
import { ICachedSettings } from './CachedSettings';

const getProcessingTimeInMS = (time: [number, number]): number => time[0] * 1000 + time[1] / 1e6;

settings.watch = use(settings.watch, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});

if (process.env.DEBUG_SETTINGS === 'true') {
settings.watch = use(settings.watch, function watch(context, next) {
const [_id, callback, options] = context;
return next(
_id,
(...args) => {
const start = process.hrtime();
callback(...args);
const elapsed = process.hrtime(start);
console.log(`settings.watch: ${_id} ${getProcessingTimeInMS(elapsed)}ms`);
},
options,
);
export function initializeSettings({ SettingsModel, settings }: { SettingsModel: Settings; settings: ICachedSettings }): void {
SettingsModel.find().forEach((record: ISetting) => {
if (record._id.startsWith('Prometheus')) {
console.log('store cache', record);
}
settings.set(record);
});
}
settings.watchMultiple = use(settings.watchMultiple, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});
settings.watchOnce = use(settings.watchOnce, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});

settings.watchByRegex = use(settings.watchByRegex, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});

settings.change = use(settings.change, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});
settings.changeMultiple = use(settings.changeMultiple, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});
settings.changeOnce = use(settings.changeOnce, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});

settings.changeByRegex = use(settings.changeByRegex, (context, next) => {
const [_id, callback, ...args] = context;
return next(_id, Meteor.bindEnvironment(callback), ...args);
});

settings.onReady = use(settings.onReady, (context, next) => {
const [callback, ...args] = context;
return next(Meteor.bindEnvironment(callback), ...args);
});
settings.initilized();
}
6 changes: 3 additions & 3 deletions apps/meteor/app/videobridge/server/methods/jitsiSetTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ Meteor.methods({
}

return jitsiTimeout || nextTimeOut;
} catch (error) {
SystemLogger.error('Error starting video call:', error.message);
} catch (err) {
SystemLogger.error({ msg: 'Error starting video call:', err });

throw new Meteor.Error('error-starting-video-call', error.message, {
throw new Meteor.Error('error-starting-video-call', err.message, {
method: 'jitsi:updateTimeout',
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IMessage, IRoom } from '@rocket.chat/core-typings';

import { AutoTransferChatScheduler } from '../lib/AutoTransferChatScheduler';
import { callbacks } from '../../../../../lib/callbacks';
import { settings } from '../../../../../app/settings/server';
Expand Down Expand Up @@ -30,7 +32,7 @@ const handleAfterTakeInquiryCallback = async (inquiry: any = {}): Promise<any> =
return inquiry;
};

const handleAfterSaveMessage = async (message: any = {}, room: any = {}): Promise<any> => {
const handleAfterSaveMessage = (message: any = {}, room: any = {}): IMessage => {
const { _id: rid, t, autoTransferredAt, autoTransferOngoing } = room;
const { token } = message;

Expand All @@ -50,11 +52,11 @@ const handleAfterSaveMessage = async (message: any = {}, room: any = {}): Promis
return message;
}

await AutoTransferChatScheduler.unscheduleRoom(rid);
Promise.await(AutoTransferChatScheduler.unscheduleRoom(rid));
return message;
};

const handleAfterCloseRoom = async (room: any = {}): Promise<any> => {
const handleAfterCloseRoom = (room: any = {}): IRoom => {
const { _id: rid, autoTransferredAt, autoTransferOngoing } = room;

if (!autoTransferTimeout || autoTransferTimeout <= 0) {
Expand All @@ -69,7 +71,7 @@ const handleAfterCloseRoom = async (room: any = {}): Promise<any> => {
return room;
}

await AutoTransferChatScheduler.unscheduleRoom(rid);
Promise.await(AutoTransferChatScheduler.unscheduleRoom(rid));
return room;
};

Expand Down
3 changes: 1 addition & 2 deletions apps/meteor/ee/app/settings/server/settings.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Meteor } from 'meteor/meteor';
import { ISetting, SettingValue } from '@rocket.chat/core-typings';

import { settings } from '../../../../app/settings/server/functions/settings';
import { isEnterprise, hasLicense, onValidateLicenses } from '../../license/server/license';
import SettingsModel from '../../../../app/models/server/models/Settings';
import { use } from '../../../../app/settings/server/Middleware';
import { SettingsEvents } from '../../../../app/settings/server';
import { settings, SettingsEvents } from '../../../../app/settings/server';

export function changeSettingValue(record: ISetting): SettingValue {
if (!record.enterprise) {
Expand Down
3 changes: 1 addition & 2 deletions apps/meteor/server/importPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import '../app/integrations/server';
import '../app/irc';
import '../app/issuelinks/server';
import '../app/katex/server';
import '../app/lib';
import '../app/lib/server';
import '../app/livestream/server';
import '../app/logger';
import '../app/token-login/server';
Expand Down Expand Up @@ -101,7 +101,6 @@ import '../app/bigbluebutton/server';
import '../app/mail-messages/server';
import '../app/user-status';
import '../app/utils';
import '../app/settings';
import '../app/models';
import '../app/metrics';
import '../app/notifications';
Expand Down
6 changes: 3 additions & 3 deletions apps/meteor/server/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import '../app/settings/server/startup';
import './services/startup';
import '../app/settings/server';
import '../lib/oauthRedirectUri';
import './overrides/http';
import './lib/logger/startup';
import './importPackages';
import '../imports/startup/server';

import './services/startup';
import '../app/lib/server/startup';

import '../ee/server';
import './lib/pushConfig';
Expand Down
Loading