/*复制文件 如果存在则不再复制*/ func copyFile(src, dest string) error { srcFile, e := os.Open(src) if e != nil { logger.Error("copyFile:", e.Error()) return e } defer srcFile.Close() df, e := os.Open(dest) defer df.Close() if e != nil && os.IsNotExist(e) { logger.Info("desIniFile:", dest, "file not exist!") destFile, e := os.Create(dest) if e != nil { logger.Error("copyFile destFile:", e.Error()) return e } defer destFile.Close() io.Copy(destFile, srcFile) logger.Info("copy file from :", src, " to ", dest) } else { logger.Info("desIniFile:", dest, "已存在") } return nil }
/*执行数据同步读取直接写入 single data 单次执行可变的业务数据 */ func (xmlInfo *XmlInfo) RunSingleDataPipe(dbIni *DBini) { logger.Info("All run begin >>>>>>>>>>>>>>>>") logger.Info("dbIni.F_DbType:", dbIni.F_DbType, "dbIni.T_DbType:", dbIni.T_DbType) if !StartFlag { return } defer func() { if err := recover(); err != nil { logger.Error("查询数据库失败", err) } }() f_pwd, _ := Base64Dec(dbIni.F_Password) t_pwd, _ := Base64Dec(dbIni.T_Password) dbIni.F_Db.Db_open(dbIni.F_DbType, dbIni.F_User, f_pwd, dbIni.F_Ip, dbIni.F_Port, dbIni.F_Dbname) dbIni.T_Db.Db_open(dbIni.T_DbType, dbIni.T_User, t_pwd, dbIni.T_Ip, dbIni.T_Port, dbIni.T_Dbname) //关闭数据库 defer dbIni.F_Db.db.Close() defer dbIni.T_Db.db.Close() if dbIni.F_Db.db == nil || dbIni.T_Db.db == nil { logger.Error("无法链接两方数据库") panic("无法链接两方数据库") return } DataSinglePipe(dbIni, xmlInfo) Updater(dbIni, xmlInfo) ExecProc(dbIni, xmlInfo) logger.Info("All run end >>>>>>>>>>>>>>>>") time.Sleep(5 * time.Second) }
/*判断是否需要查询数据*/ func Check(dbIni *DBini, xmlInfo *XmlInfo) { if strings.EqualFold(xmlInfo.IsValData, "true") { xmlInfo.CheckFlag = true return } if len(xmlInfo.CheckRowSQL) > 0 { defer func() { if err := recover(); err != nil { logger.Error("查询数据库失败", err) } }() f_pwd, _ := Base64Dec(dbIni.F_Password) dbIni.F_Db.Db_open(dbIni.F_DbType, dbIni.F_User, f_pwd, dbIni.F_Ip, dbIni.F_Port, dbIni.F_Dbname) defer dbIni.F_Db.db.Close() query, err := dbIni.F_Db.db.Query(strings.Replace(xmlInfo.CheckRowSQL, "@[dbname]", dbIni.F_Dbname, -1)) //查询数据库 defer query.Close() if err != nil { logger.Error("查询数据库失败", err.Error()) xmlInfo.CheckFlag = false panic(err) return } cols, _ := query.Columns() values := make([][]byte, len(cols)) //values是每个列的值 scans := make([]interface{}, len(cols)) //query.Scan的参数 用len(cols)定住当次查询的长度 for i := range values { //让每一行数据都填充到[][]byte里面 scans[i] = &values[i] } dbRows := 0 if query.Next() { //判断是否与上次的数据一致,如果不一致则查询数据库,否则不查询 if err := query.Scan(scans...); err != nil { //query.Scan查询出来的不定长值放到scans[i] = &values[i],也就是每行都放在values里 logger.Error(err) xmlInfo.CheckFlag = false return } for _, v := range values { intV, err := strconv.Atoi(string(v)) if err != nil { xmlInfo.CheckFlag = false } else { dbRows = intV } } if dbRows != 0 && dbRows != xmlInfo.RowNum { //获取reader 如何获取到数据则置为0,如果获取不到了则赋予全局GobalNum xmlInfo.RowNum = dbRows xmlInfo.GobalNum = dbRows xmlInfo.CheckFlag = true } else { xmlInfo.CheckFlag = false } } else { xmlInfo.CheckFlag = true } } else { logger.Info(xmlInfo.Remark, "无需查询系统库行记录是否变化") xmlInfo.CheckFlag = true } }
/*添加待匹配组织机构*/ func insertStdOrgMap(dbIni *DBini, noneOrgMap map[string]string) { stdOrgMapStmt, err := dbIni.T_Db.db.Prepare(StdOrgMapInsertSQL) defer stdOrgMapStmt.Close() if err != nil { logger.Error(err) return } for deptname, _ := range noneOrgMap { if len(deptname) == 0 { continue } stdOrgMapQuery, err := dbIni.T_Db.db.Query(StdOrgMapQuerySQL, deptname) if err != nil { logger.Error("查询数据库失败", err.Error()) return } defer stdOrgMapQuery.Close() if stdOrgMapQuery.Next() { //如果存在 则判断是否需要更新 sysnames cols, _ := stdOrgMapQuery.Columns() colsLength := len(cols) values := make([][]byte, colsLength) scans := make([]interface{}, colsLength) for i := range values { scans[i] = &values[i] } if err := stdOrgMapQuery.Scan(scans...); err != nil { logger.Error(err) return } row := make(map[string]string) //每行数据 for k, v := range values { key := cols[k] row[key] = string(v) } id := row["id"] sysnames := row["sysnames"] if len(sysnames) > 0 && len(dbIni.SysName) > 0 && !strings.Contains(sysnames, dbIni.SysName) { updateStmt, err := dbIni.T_Db.db.Prepare(UpdateSQL) defer updateStmt.Close() if err != nil { logger.Error(err) return } updateScans := make([]interface{}, 2) updateScans[0] = sysnames + "," + dbIni.SysName updateScans[1] = id updateStmt.Exec(updateScans...) } } else { //insert stdOrgScans := make([]interface{}, 4) stdOrgScans[0] = deptname stdOrgScans[1] = dbIni.SysId stdOrgScans[2] = dbIni.SysName stdOrgScans[3] = dbIni.SysName stdOrgMapStmt.Exec(stdOrgScans...) } } }
/*启动组织机构同步服务*/ func (configinfo *ConfigInfo) StartOrgSync() { logger.Info(" StartOrgSync is run... ") var orgVersionUrl = "http://" + configinfo.core_ip + ":" + configinfo.core_port + configinfo.orgversion_url var allOrgUrl = "http://" + configinfo.core_ip + ":" + configinfo.core_port + configinfo.allorg_url logger.Info("orgVersionUrl start >>>>>>>>>>>>>>>>", orgVersionUrl) logger.Info("allOrgUrl start >>>>>>>>>>>>>>>>", allOrgUrl) //发起链接访问,获取返回的json数据 for { var return_orgs []byte var return_version []byte var tmpOrgVersion string = "" if response, err := http.Get(orgVersionUrl); err != nil { logger.Error(" err = ", err) } else { defer response.Body.Close() if return_version, err = ioutil.ReadAll(response.Body); err != nil { logger.Error(" err = ", err) } tmpOrgVersion = string(return_version) } logger.Info(" return_version = ", tmpOrgVersion) if len(OrgVersion) == 0 || (len(tmpOrgVersion) > 0 && !strings.EqualFold(tmpOrgVersion, OrgVersion)) { OrgVersion = tmpOrgVersion //重新加载组织机构 if response, err := http.Get(allOrgUrl); err != nil { logger.Error(" err = ", err) } else { defer response.Body.Close() if return_orgs, err = ioutil.ReadAll(response.Body); err != nil { logger.Error(" err = ", err) } } var orgInfos []OrgInfo err := json.Unmarshal(return_orgs, &orgInfos) if err != nil { logger.Error(" err = ", err) } if len(orgInfos) > 0 { for k, _ := range OrgMap { delete(OrgMap, k) } for k, _ := range OrgNameMap { delete(OrgNameMap, k) } for i := range orgInfos { org := &orgInfos[i] OrgMap[org.Org_path] = org.Id OrgMap[org.Name] = org.Id OrgNameMap[org.Id] = org.Name } } } time.Sleep(Time_interval * time.Second) } }
/*读取xml信息初始化xmlinfo*/ func InitXmlInfo(dirName string) XmlInfo { content, err := ioutil.ReadFile("xml/HostAudit/repeatetimer_主机审计日志.xml") if err != nil { logger.Error(err) } var result XmlInfo err = xml.Unmarshal(content, &result) if err != nil { logger.Error(err) } return result }
/*测试读取xml*/ func TestReadXml() { content, err := ioutil.ReadFile("xml/HostAudit/repeatetimer_主机审计日志.xml") if err != nil { logger.Error(err) } var result XmlInfo err = xml.Unmarshal(content, &result) if err != nil { logger.Error(err) } logger.Debug(result.FileName) }
func ConServer(hostport string) (*net.TCPConn, error) { remoteAddr, err_r := net.ResolveTCPAddr("tcp4", hostport) if err_r != nil { log.Error("Conn error,ResolveTCPAddr:", hostport) return nil, err_r } conn, err := net.DialTCP("tcp4", nil, remoteAddr) if err != nil { log.Error("Conn error,connect to", hostport) return nil, err } return conn, nil }
/*获取数据库连接 支持mysql mssql mongodb*/ func (f *My_db) Db_open(dbtype string, dbusername string, dbpassowrd string, dbhostsip string, dbport string, dbname string) { var connString string if strings.EqualFold(dbtype, "mysql") { //root:@tcp(localhost:3306)/go?charset=utf8 connString = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbusername, dbpassowrd, dbhostsip, dbport, dbname) } else if strings.EqualFold(dbtype, "mssql") { connString = fmt.Sprintf("server=%s;user id=%s;password=%s;port=%s;database=%s;encrypt=disable;", dbhostsip, dbusername, dbpassowrd, dbport, dbname) } Odb, err := sql.Open(dbtype, connString) if err != nil { logger.Error(err) logger.Error("链接失败 >dbusername:%s dbpassowrd:%s dbhostsip:%s dbname:%s", dbusername, dbpassowrd, dbhostsip, dbname) f.db = nil } f.db = Odb }
func (protocol *TransProtocolComm) Decode_pack(data []byte) (*Packet, int) { log.Info("Enter Decode_pack") decodeLen := 0 pack := &Packet{} dataLen := len(data) offset := 0 headLen := binary.Size(pack._header) //1.get packhader //= not cause out of memory so >= is needed for offset = 0; dataLen-offset >= headLen; offset++ { pack._header._ver = int16(binary.BigEndian.Uint16(data[offset : offset+2])) pack._header._seq = int32(binary.BigEndian.Uint32(data[offset+2 : offset+6])) pack._header._type = int16(binary.BigEndian.Uint16(data[offset+6 : offset+8])) pack._header._bodyLen = int32(binary.BigEndian.Uint32(data[offset+8 : offset+12])) //get pack header successful if int16(VER) == pack._header._ver && pack._header._bodyLen < int32(MAX_PACK_SIZE) { break } } //2.check is a complete Packet if dataLen-offset < headLen || dataLen-offset < int(pack._header._bodyLen) { //can't find VER in data Slice or is not a complete pack. log.Error("[PACK_NOT_COMPLETE],pack bodyLen is", pack._header._bodyLen) return nil, 0 } //3.get a complete pack pack._data = make([]byte, pack._header._bodyLen) copy(pack._data, data[offset:]) decodeLen = offset + headLen + int(pack._header._bodyLen) return pack, decodeLen }
/**复制所有ini文件*/ func copyIniFiles() error { var rootName string = "xml_ini" var desRootName string = "xml" var suffix string = ".ini" var rootPath string = "xml_ini//xml_ini.ini" var fileFilter string = "/" s := []string{rootName, fileFilter} var path = strings.Join(s, "") results := make(map[string]string) err := filepath.Walk(path, func(path string, f os.FileInfo, err error) error { if f == nil { return err } if f.IsDir() { iniFile := path + fileFilter + f.Name() + suffix desIniFile := strings.Replace(path, rootName, desRootName, -1) + fileFilter + f.Name() + suffix if !strings.EqualFold(iniFile, rootPath) && strings.HasSuffix(iniFile, suffix) { results[desIniFile] = iniFile copyFile(iniFile, desIniFile) } } return nil }) if err != nil { logger.Error("filepath.Walk() returned %v\n", err) } return err }
func log(i int) { logger.Debug("Debug>>>>>>>>>>>>>>>>>>>>>>" + strconv.Itoa(i)) logger.Info("Info>>>>>>>>>>>>>>>>>>>>>>>>>" + strconv.Itoa(i)) logger.Warn("Warn>>>>>>>>>>>>>>>>>>>>>>>>>" + strconv.Itoa(i)) logger.Error("Error>>>>>>>>>>>>>>>>>>>>>>>>>" + strconv.Itoa(i)) logger.Fatal("Fatal>>>>>>>>>>>>>>>>>>>>>>>>>" + strconv.Itoa(i)) }
/*将数据写入数据库*/ func Writer(dbIni *DBini, xmlInfo *XmlInfo) { logger.Info("***************************") logger.Info("Writer begin >>>>>>>>>>>>>>>>") if len(xmlInfo.Datas) == 0 { logger.Info("Writer end >>>>>>>>>>>>>>>>") return } stmt, err := dbIni.T_Db.db.Prepare(xmlInfo.InsertSQL) defer stmt.Close() if err != nil { logger.Error(err) return } colsLength := len(xmlInfo.Columns) c := 0 arrIds := make([]string, len(xmlInfo.Datas)) for _, v := range xmlInfo.Datas { scans := make([]interface{}, colsLength+2) for i := range xmlInfo.Columns { scans[i] = v[xmlInfo.Columns[i]] } scans[colsLength] = dbIni.SysId scans[colsLength+1] = dbIni.SysName arrIds[c] = v[xmlInfo.Columns[0]] stmt.Exec(scans...) c++ } xmlInfo.Datas = nil xmlInfo.Ids = arrIds logger.Info("Writer end >>>>>>>>>>>>>>>>") }
/** *记录数据日志 *------ flag 1 成功 0 失败 *------jobname dataname syscode sysname platform producer beginTime flag endTime errmsg */ func DataLog(dbIni *DBini, xmlInfo *XmlInfo, begtime string, err error) { stmt, err := dbIni.T_Db.db.Prepare(LogInsertSQL) defer stmt.Close() if err != nil { logger.Error(err) return } scans := make([]interface{}, 10) var flag string = "1" var msg string if err != nil { flag = "0" msg = string(err.Error()) } scans[0] = xmlInfo.Remark scans[1] = xmlInfo.Remark scans[2] = dbIni.SysId scans[3] = dbIni.SysName scans[4] = dbIni.Platform scans[5] = dbIni.Company scans[6] = begtime scans[7] = flag scans[8] = NowTime() scans[9] = msg stmt.Exec(scans...) }
/*已匹配组织机构信息*/ func (sys *SysIni) StdOrgSync() { logger.Info("StdOrgSync start >>>>>>>>>>>>>>>>") defer func() { if err := recover(); err != nil { logger.Error("查询数据库失败", err) } }() for { dbIni := &sys.dbini t_pwd, _ := Base64Dec(dbIni.T_Password) dbIni.T_Db.Db_open(dbIni.T_DbType, dbIni.T_User, t_pwd, dbIni.T_Ip, dbIni.T_Port, dbIni.T_Dbname) defer dbIni.T_Db.db.Close() query, err := dbIni.T_Db.db.Query(MapQuerySQL) //查询数据库 if err != nil { logger.Error("查询数据库失败", err.Error()) panic(err) return } defer query.Close() for k, _ := range StdOrgMap { delete(StdOrgMap, k) } cols, _ := query.Columns() colsLength := len(cols) values := make([][]byte, colsLength) scans := make([]interface{}, colsLength) for i := range values { scans[i] = &values[i] } for query.Next() { //循环 if err := query.Scan(scans...); err != nil { logger.Error(err) return } row := make(map[string]string) //每行数据 for k, v := range values { key := cols[k] row[key] = string(v) } StdOrgMap[row["map_deptname"]] = row["std_deptid"] } time.Sleep(Time_interval * time.Second) } }
func (p *Workshop) Run() { LOOP: for { select { case cmd := <-p.upCtrlChan: if cmd == CMD_EXIT { close(p.msgChan) // will send an empty msg to its receivers p.closeSubRoutines(p.PRCtrlChan, p.PRReportChan, p.processorNum) p.closeSubRoutines(p.CLCtrlChan, p.CLReportChan, p.collectorNum) break LOOP } else if cmd == CMD_TICK { // logger.Debug("run_routine tick") p.sendCtrlInfo(p.PRCtrlChan, cmd, p.processorNum) p.sendCtrlInfo(p.CLCtrlChan, cmd, p.collectorNum) p.cronTick++ if p.cronTick > p.cronTickInterval { p.Tick() p.cronTick = 0 } } else { logger.Error(" WorkShop receive a un-expected cmd ", cmd) } default: messages, err := p.provider.GetNextMsg() if err != nil { logger.Error("src provider get msg err occur with ", len(messages), " msgs, err: ", err) } if messages == nil || len(messages) == 0 { continue } for _, v := range messages { if len(v) > 0 { logger.Info("emit!") p.msgChan <- string(v) } } logger.Debug("main routine receive ", len(messages), " msgs from srcProvider") p.processedLine += len(messages) p.provider.Ack() } } p.provider.Destory() p.upReportChan <- RET_EXIT_SUCCESS }
/* *注册组织机构信息 */ func (sys *SysIni) RegisterSys() { if !StartFlag { return } dbIni := &sys.dbini t_pwd, _ := Base64Dec(dbIni.T_Password) dbIni.T_Db.Db_open(dbIni.T_DbType, dbIni.T_User, t_pwd, dbIni.T_Ip, dbIni.T_Port, dbIni.T_Dbname) defer dbIni.T_Db.db.Close() query, err := dbIni.T_Db.db.Query(RegisterQuerySQL, dbIni.SysName) //查询数据库 if err != nil { logger.Error("查询数据库失败", err.Error()) return } defer query.Close() stmt, err := dbIni.T_Db.db.Prepare(RegisterInsertSQL) defer stmt.Close() if err != nil { logger.Error(err) return } if !query.Next() { scans := make([]interface{}, 6) scans[0] = dbIni.SysId scans[1] = dbIni.SysName scans[2] = dbIni.SysName scans[3] = dbIni.Platform scans[4] = dbIni.Company scans[5] = dbIni.SysType stmt.Exec(scans...) } //调用http服务进行系统注册 var configInfo ConfigInfo = InitConfigInfo() var rstUrl = "http://" + configInfo.core_ip + ":" + configInfo.core_port + configInfo.register_url if response, err := http.Get(rstUrl); err != nil { logger.Error(" err = ", err) } else { defer response.Body.Close() if _, err := ioutil.ReadAll(response.Body); err != nil { logger.Error(" err = ", err) } } }
/*读取xml信息 初始化xmlinfo*/ func InitXmlInfoes(dirName string, filter string) []XmlInfo { fileNames := GetXmlFileNames(dirName, filter) var values = make([]XmlInfo, len(fileNames)) var i = 0 for _, v := range fileNames { content, err := ioutil.ReadFile(v) if err != nil { logger.Error(err) } var result XmlInfo err = xml.Unmarshal(content, &result) if err != nil { logger.Error(err) } values[i] = result values[i].CheckFlag = true i++ } return values }
/*主入口*/ func main() { runtime.GOMAXPROCS(runtime.NumCPU()) //指定是否控制台打印,默认为true logger.SetConsole(true) //指定日志级别 ALL,DEBUG,INFO,WARN,ERROR,FATAL,OFF 级别由低到高 logger.SetLevel(logger.INFO) logger.SetRollingFile("./log", "datapostlog.log", 100, 50, logger.MB) if beego.AppConfig.String("runmode") == "dev" { orm.Debug = true } //支持异构版本升级 pipe.Upgrade() time.Sleep(3 * time.Second) //配置文件初始化 err := pipe.InitFile() if err != nil { logger.Error("InitFile():", err) } //注册运行服务 ServiceHandle(cSvcName, cSvcName, cSvcName, func() { //初始化运行参数 pipe.InitConfigMap() //运行参数管理UI服务 beego.Router("/", &pipe.IndexInfo{}, "*:Index") beego.Router("/index/list", &pipe.IndexInfo{}, "*:List") beego.Router("/index/edit", &pipe.IndexInfo{}, "*:Edit") beego.Router("/index/test", &pipe.IndexInfo{}, "*:Test") beego.Router("/index/help", &pipe.IndexInfo{}, "*:Help") beego.Router("/index/treedata", &pipe.IndexInfo{}, "*:TreeData") //启动数据同步 go pipe.StartAllSys() //启动https服务 go pipe.Https_start() //启动参数管理UI 服务 beego.Run() logger.Info("DaemonName=", cSvcName, "Daemon started.") for { time.Sleep(time.Hour) } }, func() { Stop() logger.Info("DaemonName=", cSvcName, "Daemon stoped.") }) }
func (sock *TcpSocket) to_write() { var pack *packet.Packet for ESTABLISHED == sock._socketState { pack = <-sock._writerChannel if pack == nil { log.Error("get pack error") continue } data := pack.Serialize() n, err := sock._conn.Write(data) if err != nil { if io.ErrShortWrite != err { log.Error("write err close--", n) sock.closeSock() break } } log.Info("send data success") } }
//测试连接 func (this *IndexInfo) Test() { var ( config = new(IndexInfo) err error ret Ret db = new(My_db) dbtype string = "mysql" dbusername string dbpassowrd string dbhostsip string dbport string dbname string ) if err = json.Unmarshal([]byte(this.GetString("json")), &config); err == nil { if strings.EqualFold(config.Systype, "f") { dbusername = config.F_username dbpassowrd, _ = Base64Dec(config.F_password) dbhostsip = config.F_ip dbport = config.F_port dbname = "mid" } else { dbusername = config.T_username dbpassowrd, _ = Base64Dec(config.T_password) dbhostsip = config.T_ip dbport = config.T_port dbname = "rptdb" } db.Db_open(dbtype, dbusername, dbpassowrd, dbhostsip, dbport, dbname) } if err != nil { logger.Error(err) ret.Code, ret.Msg = 1, err.Error() } else { if db.db != nil { defer db.db.Close() query, err := db.db.Query("select 1") if err != nil { ret.Code = 1 } else { if query.Next() { ret.Code = 0 } else { ret.Code = 1 } defer query.Close() } } else { ret.Code = 1 } } this.Data["json"] = &ret this.ServeJson() }
/*访问数据库获取数据*/ func Reader(dbIni *DBini, xmlInfo *XmlInfo) { logger.Info("***************************") logger.Info("Reader begin >>>>>>>>>>>>>>>>") query, err := dbIni.F_Db.db.Query(xmlInfo.DataSQL) //查询数据库 if err != nil { logger.Error("查询数据库失败", err.Error()) return } defer query.Close() cols, _ := query.Columns() values := make([][]byte, len(cols)) //values是每个列的值 scans := make([]interface{}, len(cols)) //query.Scan的参数 用len(cols)定住当次查询的长度 for i := range values { //让每一行数据都填充到[][]byte里面 scans[i] = &values[i] } results := make(map[int]map[string]string) //最后得到的map i := 0 for query.Next() { //循环 if err := query.Scan(scans...); err != nil { //query.Scan查询出来的不定长值放到scans[i] = &values[i],也就是每行都放在values里 logger.Error(err) return } row := make(map[string]string) //每行数据 for k, v := range values { key := cols[k] row[key] = string(v) } results[i] = row //装入结果集中 i++ } if i == 0 { xmlInfo.RowNum = xmlInfo.GobalNum } else { xmlInfo.RowNum = 0 } xmlInfo.Datas = results xmlInfo.Columns = cols logger.Info("Reader end >>>>>>>>>>>>>>>>") }
func (sock *TcpSocket) to_read() { for sock._socketState == ESTABLISHED { //接收前必须重新调整接收缓冲 sock._recvBuff.Reset() //if recv too big pack which is bigger than MAX_PACK_SIZE if sock._recvBuff.RemainLen() <= 0 { log.Error("Invalid pack is too big,close the session") sock.closeSock() } recvLen, err := sock._conn.Read(sock._recvBuff.WritePos()) if err != nil { if err == syscall.EINTR || err == syscall.EAGAIN { log.Error("read error--") continue } else if neterr, ok := err.(net.Error); ok && neterr.Timeout() { //meas read Timeout log.Error("read time out-", err, "neterr--", neterr) } else { log.Error("read erro-close--", err) sock.closeSock() break } } if recvLen > 0 { sock._recvBuff.WriteOffsetAdd(recvLen) pack, decodeLen := sock._tprotocol.Decode_pack(sock._recvBuff.ReadPos()) if pack == nil { continue } sock._recvBuff.ReadOffsetAdd(decodeLen) //触发消息处理 sock._readChannel <- pack } } //for log.Error("exit read data") }
/*执行数据处理*/ func ExecProc(dbIni *DBini, xmlInfo *XmlInfo) { proname := xmlInfo.Proname if strings.EqualFold(xmlInfo.Isexecpro, "true") && len(proname) > 0 { var buffer bytes.Buffer buffer.WriteString("call ") buffer.WriteString(proname) buffer.WriteString("()") defer func() { if err := recover(); err != nil { logger.Error("查询数据库失败", err) } }() logger.Info("执行存储过程 >>>>>>>>>>>>>>>>", buffer.String()) stmt, err := dbIni.T_Db.db.Prepare(buffer.String()) defer stmt.Close() if err != nil { logger.Error(err) panic(err) return } stmt.Exec() } }
/*判断是否需要添加标记位,如需要添加标记位*/ func Alter(dbIni *DBini, xmlInfo *XmlInfo) { if len(xmlInfo.CheckColumnSQL) > 0 { defer func() { if err := recover(); err != nil { logger.Error("数据库执行失败", err) } }() f_pwd, _ := Base64Dec(dbIni.F_Password) dbIni.F_Db.Db_open(dbIni.F_DbType, dbIni.F_User, f_pwd, dbIni.F_Ip, dbIni.F_Port, dbIni.F_Dbname) defer dbIni.F_Db.db.Close() query, err := dbIni.F_Db.db.Query(strings.Replace(xmlInfo.CheckColumnSQL, "@[dbname]", dbIni.F_Dbname, -1)) //查询数据库 defer query.Close() if err != nil { logger.Error("查询数据库失败", err.Error()) panic(err) return } testQuery, err := dbIni.F_Db.db.Query("select 1") if err != nil { logger.Error("数据库连接异常,无法查询测试数据") panic(err) return } else { if !testQuery.Next() { logger.Error("数据库连接异常,无法遍历测试数据") panic(err) return } defer testQuery.Close() } if query.Next() { logger.Info(xmlInfo.Remark, "经检测,标记位已添加") xmlInfo.AlterFlag = true } else { stmt, err := dbIni.F_Db.db.Prepare(xmlInfo.AlterSQL) defer stmt.Close() if err != nil { logger.Error(err) return } stmt.Exec() logger.Info(xmlInfo.Remark, "在源数据库中添加成功") updateStmt, err := dbIni.F_Db.db.Prepare(xmlInfo.UpdateFlagSQL) defer updateStmt.Close() if err != nil { logger.Error(err) return } updateStmt.Exec() logger.Info(xmlInfo.Remark, "在源数据库中更新成功") xmlInfo.AlterFlag = true } } else { logger.Info(xmlInfo.Remark, "无需检测标记位") xmlInfo.AlterFlag = true } }
func main() { if len(os.Args) < 3 { fmt.Fprintf(os.Stderr, "Usage: $0 -c config_file [-t]\n") os.Exit(1) } flag.StringVar(&configFile_, "c", "conf.json", "config file in json") flag.BoolVar(&isTest, "t", false, "test mode") flag.Parse() // SIGHUP to reload configFile, SIGTERM/SIGINT to exit the process sigChan := make(chan os.Signal, 10) signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT) // start worshop for the first time if err := StartNewWorkShop(isTest); err != nil { fmt.Fprintf(os.Stderr, "workshop init fail [%+v]\n", err) os.Exit(1) } if isTest { os.Exit(0) } // loop. wait for signals MAIN_LOOP: for { select { case sig := <-sigChan: fmt.Fprintf(os.Stderr, "receive sig [%+v]\n", sig) // logger.Error("receive sig ", sig) if sig == syscall.SIGHUP { // wait for workshop to do some clean-up StopWorkShop() ticker_.Stop() } else if sig == syscall.SIGTERM || sig == syscall.SIGINT { StopWorkShop() ticker_.Stop() break MAIN_LOOP } else { logger.Error("main routine receive unexpected signal [", sig, "], this should NEVER happen") os.Exit(1) } case <-ticker_.C: WSChan_ <- CMD_TICK logger.Debug("main_routine tick") } } logger.Info("the qtask application exit") }
/*回写标记位*/ func Updater(dbIni *DBini, xmlInfo *XmlInfo) { logger.Info("***************************") logger.Info("Updater begin >>>>>>>>>>>>>>>>") defer func() { if err := recover(); err != nil { logger.Error("数据库执行失败", err) } }() if len(xmlInfo.SuccessSQL) > 0 { var buffer bytes.Buffer idsLength := len(xmlInfo.Ids) idsLength-- for i := range xmlInfo.Ids { if i == idsLength { buffer.WriteString("'") buffer.WriteString(xmlInfo.Ids[i]) buffer.WriteString("'") } else { buffer.WriteString("'") buffer.WriteString(xmlInfo.Ids[i]) buffer.WriteString("',") } } if len(buffer.String()) > 0 { stmt, err := dbIni.F_Db.db.Prepare(strings.Replace(xmlInfo.SuccessSQL, "@[id]", buffer.String(), -1)) defer stmt.Close() if err != nil { logger.Error(err) panic(err) return } stmt.Exec() } } logger.Info("Updater end >>>>>>>>>>>>>>>>") }
/**合并文件*/ func joinFiles() { //判断密码是否以enc`开头,去掉enc` bakXmlInies := getXmlNames("xml_bak") for _, bakPath := range bakXmlInies { newIniPath := strings.Replace(bakPath, "xml_bak", "xml_ini", 1) bakXmlIniCfg, bakErr := goconfig.LoadConfigFile(bakPath) newXmlIniCfg, newErr := goconfig.LoadConfigFile(newIniPath) if bakXmlIniCfg != nil && newXmlIniCfg != nil && bakErr == nil && newErr == nil { //替换文件 joinToIni(bakXmlIniCfg, newXmlIniCfg) retErr := goconfig.SaveConfig(newXmlIniCfg, newIniPath) if retErr != nil { logger.Error("retErr=", retErr) } } } }
func (sock *TcpSocket) Write(data []byte) error { if len(data) <= 0 { return errors.New("data is error") } if ESTABLISHED != sock._socketState { log.Error("socket 's state is not ESTABLISHED") return errors.New("socket is error") } //1.encode pack pack := sock._tprotocol.Encode_pack(data) //2.write notice select { case sock._writerChannel <- pack: return nil default: return errors.New("WriteChannel has full") } }
/*执行数据同步*/ func (xmlInfo *XmlInfo) Run(dbIni *DBini) { logger.Info("All run begin >>>>>>>>>>>>>>>>") f_pwd, _ := Base64Dec(dbIni.F_Password) t_pwd, _ := Base64Dec(dbIni.T_Password) dbIni.F_Db.Db_open(dbIni.F_DbType, dbIni.F_User, f_pwd, dbIni.F_Ip, dbIni.F_Port, dbIni.F_Dbname) dbIni.T_Db.Db_open(dbIni.T_DbType, dbIni.T_User, t_pwd, dbIni.T_Ip, dbIni.T_Port, dbIni.T_Dbname) //关闭数据库 defer dbIni.F_Db.db.Close() defer dbIni.T_Db.db.Close() if dbIni.F_Db.db == nil || dbIni.T_Db.db == nil { logger.Error("无法链接两方数据库") return } Reader(dbIni, xmlInfo) Writer(dbIni, xmlInfo) Updater(dbIni, xmlInfo) logger.Info("All run end >>>>>>>>>>>>>>>>") time.Sleep(5 * time.Second) }