Commit 89ad1d9e authored by Nicolas Wavrant's avatar Nicolas Wavrant

notifier: write feed in a transactional way, to avoid corrupted feed

parent e212d72c
...@@ -7,6 +7,7 @@ import datetime ...@@ -7,6 +7,7 @@ import datetime
import json import json
import httplib import httplib
import os import os
import shutil
import socket import socket
import subprocess import subprocess
import sys import sys
...@@ -109,17 +110,36 @@ def main(): ...@@ -109,17 +110,36 @@ def main():
print content print content
# Write feed safely
error_message = ""
temp_file = args.logfile[0] + '.tmp'
try:
shutil.copy2(args.logfile[0], temp_file)
except IOError:
error_message = "ERROR ON WRITING FEED"
try:
with open(temp_file, 'a') as file_:
csvfile = csv.writer(file_)
csvfile.writerow([
int(time.time()),
args.title[0],
content,
'slapos:%s' % uuid.uuid4(),
])
os.rename(temp_file, args.logfile[0])
except:
error_message = "ERROR ON WRITING FEED"
finally:
try:
os.remove(temp_file)
except OSError:
pass
with open(args.logfile[0], 'a') as file_: if error_message:
cvsfile = csv.writer(file_) saveStatus(error_message)
cvsfile.writerow([ exit_code = 1
int(time.time()),
args.title[0],
content,
'slapos:%s' % uuid.uuid4(),
])
if exit_code != 0: if exit_code != 0:
sys.exit(exit_code) sys.exit(exit_code)
print 'Fetching %s feed...' % args.feed_url[0] print 'Fetching %s feed...' % args.feed_url[0]
...@@ -143,7 +163,6 @@ def main(): ...@@ -143,7 +163,6 @@ def main():
notification_path += str(transaction_id) notification_path += str(transaction_id)
headers = {'Content-Type': feed.info().getheader('Content-Type')} headers = {'Content-Type': feed.info().getheader('Content-Type')}
error_message = ""
try: try:
notification = httplib.HTTPConnection(notification_url.hostname, notification = httplib.HTTPConnection(notification_url.hostname,
notification_port) notification_port)
...@@ -160,7 +179,7 @@ def main(): ...@@ -160,7 +179,7 @@ def main():
finally: finally:
if error_message: if error_message:
sys.stderr.write(error_message) sys.stderr.write(error_message)
saveStatus(saveStatus('ERROR ON NOTIFYING : %s') % error_message) saveStatus('ERROR ON NOTIFYING : %s') % error_message
if some_notification_failed: if some_notification_failed:
sys.exit(1) sys.exit(1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment