From e36fa601ad79a26773164a86aa8e955221321443 Mon Sep 17 00:00:00 2001 From: Pablo Carranza Velez Date: Tue, 25 Apr 2017 11:25:01 -0700 Subject: [PATCH] Avoid writing target device config to DB if it hasn't changed This helps avoid unnecessary writes to the DB which may cause disk wearout. We also change the error message in this section to show that the error might have happened when fetching the device config as much as when setting it. Change-Type: patch Signed-off-by: Pablo Carranza Velez --- src/application.coffee | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/application.coffee b/src/application.coffee index e58e36a9..6fced1cf 100644 --- a/src/application.coffee +++ b/src/application.coffee @@ -500,7 +500,7 @@ getAndApplyDeviceConfig = -> .then ({ values, targetValues }) -> executeSpecialActionsAndHostConfig(targetValues, values) .tap -> - deviceConfig.set({ values: targetValues }) + deviceConfig.set({ values: targetValues }) if !_.isEqual(values, targetValues) .then (needsReboot) -> if needsReboot logSystemMessage('Rebooting', {}, 'Reboot') @@ -719,11 +719,16 @@ application.update = update = (force, scheduled = false) -> remoteDeviceConfig = {} _.map remoteAppIds, (appId) -> _.merge(remoteDeviceConfig, JSON.parse(remoteApps[appId].config)) - deviceConfig.set({ targetValues: remoteDeviceConfig }) - .then -> - getAndApplyDeviceConfig() + deviceConfig.get() + .then ({ values, targetValues }) -> + # If the new device config is different from the target values we had, or if + # for some reason it hasn't been applied yet (values don't match target), we apply it. + if !_.isEqual(targetValues, remoteDeviceConfig) or !_.isEqual(targetValues, values) + deviceConfig.set({ targetValues: remoteDeviceConfig }) + .then -> + getAndApplyDeviceConfig() .catch (err) -> - logSystemMessage("Error applying device configuration: #{err}", { error: err }, 'Set device configuration error') + logSystemMessage("Error fetching/applying device configuration: #{err}", { error: err }, 'Set device configuration error') .return(allAppIds) .map (appId) -> return if application.localMode