# -*- coding: utf-8 -*- """ create by caijinxu on 2019/4/8 """ import etcd import json import time import subprocess import os import re import threading import pymysql import traceback from settings import * import logging __author__ = 'caijinxu' logging.basicConfig(filename='etcdlog.log', format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S %p', level=20) def register_service(): # etcd 中注册服务 while True: client = etcd.Client(host=ETCDHOST) service_key = "/online_work/" + IPADD # print(service_key) try: serv = client.read(service_key) webroot = json.loads(serv.value) webroot.update(WEBROOT) except etcd.EtcdKeyNotFound: webroot = WEBROOT client.write(service_key, json.dumps(webroot), ttl=10) time.sleep(8) def mvfile(srcpath, tagpath): # 移动文件 if os.path.isfile(srcpath): tagdir = os.path.dirname(tagpath) subprocess.check_call("mkdir -p %s" % tagdir, shell=True) subprocess.check_call(["mv", srcpath, tagpath]) # print(time.strftime('%Y-%m-%d %H:%M:%S'), "移动文件:", srcpath, "==>", tagpath) logging.info("移动文件:" + srcpath + " ==> " + tagpath) else: raise Exception("输入文件不存在或者不是一个文件" + srcpath) def updateurlstatus(imgurl, taskstaus, remark): # 更新url状态 bdb_connection = pymysql.connect(host=MYSQLHOST, user=MYSQLUSER, db=MYSQLDB, passwd=MYSQLPASSWD, charset="utf8", init_command='SET NAMES UTF8', cursorclass=pymysql.cursors.DictCursor) b_cursor = bdb_connection.cursor() updatesql = "UPDATE handleimgurl SET updatetime=now(),taskstatus=%s,remark='%s' WHERE imgurl='%s' ORDER BY " \ "`createtime` DESC" % (taskstaus, pymysql.escape_string(remark), imgurl) try: b_cursor.execute(updatesql) bdb_connection.commit() # print(time.strftime('%Y-%m-%d %H:%M:%S'), "更新url状态::", updatesql) logging.info("更新url状态::" + updatesql) except: bdb_connection.rollback() # print(time.strftime('%Y-%m-%d %H:%M:%S'), "更新url状态时出错,dbrollback:", updatesql) logging.error("更新url状态时出错,dbrollback:" + updatesql) bdb_connection.close() def watch_etcd(): # 监控etcd上文件变化 client = etcd.Client(host=ETCDHOST) t = threading.Thread(target=register_service) t.start() for event in client.eternal_watch('/services/', recursive=True): # print(time.strftime('%Y-%m-%d %H:%M:%S'), "获取ETCD事件:", event) logging.info("获取ETCD事件:" + event.__repr__()) for servername in WEBROOT.keys(): if event.value: if re.match(servername, event.value): if "reocerjob" in event.key: # print(time.strftime('%Y-%m-%d %H:%M:%S'), "恢复链接文件事件:", event.value) logging.debug("恢复链接文件事件:" + event.value.__repr__()) urlpath = re.sub(servername, '', event.value) imgsrcpath = WEBROOT[servername][0] + urlpath imgtmppath = WEBROOT[servername][1] + urlpath # print(time.strftime('%Y-%m-%d %H:%M:%S'), "恢复文件原位置:", imgsrcpath, "删除文件存放:", imgtmppath) logging.info("恢复文件原位置:" + imgsrcpath + "删除文件存放:" + imgtmppath) try: mvfile(imgtmppath, imgsrcpath) updateurlstatus(event.value, 1, '') except: updateurlstatus(event.value, 2, traceback.format_exc()) # print(time.strftime('%Y-%m-%d %H:%M:%S'), "移动文件是出错:", traceback.format_exc()) logging.error("移动文件是出错:" + traceback.format_exc()) elif "deletejob" in event.key: # print(time.strftime('%Y-%m-%d %H:%M:%S'), "删除链接文件事件:", event.value) logging.info("删除链接文件事件:" + event.value.__repr__()) urlpath = re.sub(servername, '', event.value) imgsrcpath = WEBROOT[servername][0] + urlpath imgtmppath = WEBROOT[servername][1] + urlpath try: mvfile(imgsrcpath, imgtmppath) updateurlstatus(event.value, 1, '') except Exception as ex: updateurlstatus(event.value, 2, traceback.format_exc()) # print(time.strftime('%Y-%m-%d %H:%M:%S'), "移动文件是出错:", traceback.format_exc()) logging.error("移动文件是出错:" + traceback.format_exc()) break if __name__ == '__main__': watch_etcd()