1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
...
#action du thread ( start )
def run(self):
#Signale que l'on met en file le fichier
self.logger.info("%s -- INFO -- Attente du thread -- %s"% (strftime('%c',gmtime()), self.file[1]) )
#aquisition d'un jeton ( semaphore ) ou attente d'une libération
self.sem.acquire()
#Changement d'état en base => 2 upload en cours
self.sql.execute("UPDATE "+str(self.conf.get("DDB","TBL_ETAT"))+" SET "+str(self.conf.get("DDB","CHAMP_ETAT"))+" = 2 WHERE "+str(self.conf.get("DDB","CHAMP_ID"))+" = "+str(self.file[0]))
try:
#jeton acquis , signalement du lancement de l'upload du fichier
self.logger.info("%s -- INFO -- Execution du thread -- %s"% (strftime('%c',gmtime()), self.file[1]) )
#envoie du fichier au module FTP
cret = self._send_file()
#test du code retour 0 = OK
if(cret != 1):
#Changement d'état en base => 3 upload terminé si tout est ok
self.sql.execute("UPDATE "+str(self.conf.get("DDB","TBL_ETAT"))+" SET "+str(self.conf.get("DDB","CHAMP_ETAT"))+" = 3 WHERE "+str(self.conf.get("DDB","CHAMP_ID"))+" = "+str(self.file[0]))
#notification
self.notify_by_mail('data_newfilenotify')
self.logger.info("%s -- INFO -- Notify new file -- %s"% (strftime('%c',gmtime()), self.file[1]) )
else:
#remet l'etat du fichier à 0 pour reesayer
self.sql.execute("UPDATE "+str(self.conf.get("DDB","TBL_ETAT"))+" SET "+str(self.conf.get("DDB","CHAMP_ETAT"))+" = 0 WHERE "+str(self.conf.get("DDB","CHAMP_ID"))+" = "+str(self.file[0]))
#notification
self.notify_by_mail('data_retryfilenotify')
self.logger.info("%s -- ERR -- Notify retry file -- %s"% (strftime('%c',gmtime()), self.file[1]) )
finally:
#signalement de la fin de l'upload donc du thread
self.logger.info("%s -- INFO -- Fin du thread -- %s"% (strftime('%c',gmtime()), self.file[1]) )
#libération du jeton pour laisser la place à un autre
self.sem.release()
#Méthode _send_file gére les transactions avec le serveur FTP
def _send_file(self):
try:
#ouverture du fichier data
f = open(self.file[3]+self.file[1],'rb')
except IOError:
#erreur à l'ouverture du fichier
self.logger.info('%s -- ERR -- sendFile error opening [%s] -- etat devient 404' % (strftime('%c',gmtime()), self.file[1]) )
#Changement d'état en base => 404 Fichier introuvable
self.sql.execute("UPDATE "+str(self.conf.get("DDB","TBL_ETAT"))+" SET "+str(self.conf.get("DDB","CHAMP_ETAT"))+" = 404 WHERE "+str(self.conf.get("DDB","CHAMP_ID"))+" = "+str(self.file[0]))
#notification erreur
self.notify_by_mail('data_emergencynotify')
self.logger.info("%s -- INFO -- Notify error -- %s"% (strftime('%c',gmtime()), self.file[1]) )
#quit la fonction
return 1
try:
#connection au serveur FTP
ftp = FTP( self.conf.get("FTP", "HOST") )
#Login avec user <-> password
ftp.login( self.conf.get("FTP", "USER"), self.conf.get("FTP", "PASSWORD"))
try:
#creation du repertoire destination
self.logger.info("%s -- INFO -- Creation repertoire -- %s"% (strftime('%c',gmtime()), self.file[4]) )
ftp.mkd(self.file[4])
except error_perm, resp:
#si le repertoire existe déjà .. on signale et on passe
self.logger.info("%s -- WARN -- Repertoire deja existant -- %s"% (strftime('%c',gmtime()), self.file[1]) )
finally:
#on se déplace dans le repertoire finale
ftp.cwd(self.file[4])
#info du lancement d'upload du fichier
self.logger.info("%s -- INFO -- Depot du fichier -- %s"% (strftime('%c',gmtime()), self.file[1]) )
try:
#Lancement de l'upload proprement dit#
ftp.storbinary('STOR %s' %self.file[1], f)
except all_errors, resp:
#remet l'etat du fichier à 0 pour reesayer
self.sql.execute("UPDATE "+str(self.conf.get("DDB","TBL_ETAT"))+" SET "+str(self.conf.get("DDB","CHAMP_ETAT"))+" = 0 WHERE "+str(self.conf.get("DDB","CHAMP_ID"))+" = "+str(self.file[0]))
self.logger.info("%s -- ERR -- %s etat devient 0 Erreur transfert du fichier -- %s"% (strftime('%c',gmtime()), resp, self.file[1]) )
#Retour erreur
return 1
#code retour
return 0
except error_perm, resp:
#Changement d'état en base => 500 Probleme de connection ou d'ecriture
self.sql.execute("UPDATE "+str(self.conf.get("DDB","TBL_ETAT"))+" SET "+str(self.conf.get("DDB","CHAMP_ETAT"))+" = 500 WHERE "+str(self.conf.get("DDB","CHAMP_ID"))+" = "+str(self.file[0]))
self.logger.info("%s -- ERR -- %s etat devient 500 -- %s"% (strftime('%c',gmtime()), resp, self.file[1]) )
#notification erreur
self.logger.info("%s -- INFO -- Notify error -- %s"% (strftime('%c',gmtime()), self.file[1]) )
self.notify_by_mail('data_emergencynotify')
return 1
except all_errors, resp:
#Changement d'état en base => 500 Probleme de connection ou d'ecriture
self.sql.execute("UPDATE "+str(self.conf.get("DDB","TBL_ETAT"))+" SET "+str(self.conf.get("DDB","CHAMP_ETAT"))+" = 500 WHERE "+str(self.conf.get("DDB","CHAMP_ID"))+" = "+str(self.file[0]))
self.logger.info("%s -- ERR -- %s etat devient 500 -- %s"% (strftime('%c',gmtime()), resp, self.file[1]) )
#notification erreur
self.logger.info("%s -- INFO -- Notify error -- %s"% (strftime('%c',gmtime()), self.file[1]) )
self.notify_by_mail('data_emergencynotify')
return 1
finally:
#fermeture du fichier
self.logger.info("%s -- INFO -- Fermeture du fichier -- %s"% (strftime('%c',gmtime()), self.file[1]) )
f.close()
#cloture de la connection FTP
self.logger.info("%s -- INFO -- Deconnection du FTP -- %s"% (strftime('%c',gmtime()), self.file[1]) )
ftp.quit()
... |
Partager