Skip to content

Commit

Permalink
Use atomic write for gauge-data.txt
Browse files Browse the repository at this point in the history
Addresses issue #10
  • Loading branch information
gjr80 committed Jan 20, 2018
1 parent 61e0a54 commit a011849
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions bin/user/rtgd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
# You should have received a copy of the GNU General Public License along with
# this program. If not, see http://www.gnu.org/licenses/.
#
# Version: 0.3.2 Date: 20 January 2018
# Version: 0.3.3 Date: ?? January 2018
#
# Revision History
# ?? January 2018 v0.3.3
# - implemented atomic write when writing gauge-data.txt to file
# 20 January 2018 v0.3.2
# - modified rtgdthread queue management to fix 100% CPU usage issue
# 3 December 2017 v0.3.1
Expand Down Expand Up @@ -388,6 +390,7 @@
import httplib
import json
import math
import os
import os.path
import socket
import syslog
Expand Down Expand Up @@ -692,6 +695,7 @@ def __init__(self, control_queue, result_queue, config_dict, manager_dict,
self.rtgd_path_file = os.path.join(self.rtgd_path,
rtgd_config_dict.get('rtgd_file_name',
'gauge-data.txt'))
self.rtgd_path_file_tmp = self.rtgd_path_file + '.tmp'

# get the remote server URL if it exists, if it doesn't set it to None
self.remote_server_url = rtgd_config_dict.get('remote_server_url', None)
Expand Down Expand Up @@ -1148,8 +1152,9 @@ def write_data(self, data):
Takes dictionary of data elements, converts them to JSON format and
writes them to file. JSON output is sorted by key and any non-critical
whitespace removed before being written to file. Destination directory
is created if it does not exist.
whitespace removed before being written to file. An atomic write to
file is used to lessen chance of rtgd/web server file access conflict.
Destination directory is created if it does not exist.
Inputs:
data: dictionary of gauge-data.txt data elements
Expand All @@ -1163,9 +1168,11 @@ def write_data(self, data):
# raise if the error is anything other than the dir already exists
if error.errno != errno.EEXIST:
raise
# now write to file
with open(self.rtgd_path_file, 'w') as f:
# now write to temporary file
with open(self.rtgd_path_file_tmp, 'w') as f:
json.dump(data, f, separators=(',', ':'), sort_keys=True)
# and copy the temporary file to our destination
os.rename(self.rtgd_path_file_tmp, self.rtgd_path_file)

def get_scroller_text(self):
"""Obtain the text string to be used in the scroller.
Expand Down

0 comments on commit a011849

Please sign in to comment.