void SyncFiles::Run() { stCpyFile orden; SetId("SyncFiles"); while (IsRunning()) { bool _sincronizando = false; this->sleep(10); while (Get(orden)) { NLOG_DEBUG("FTP. Sincronizando Fichero %s en %s", orden.src.c_str(), orden.ipto.c_str()); _sincronizando = _sincronizando ? true : orden.bRecargar; #ifdef _WIN32 if (LocalConfig::cfg.winSyncFtp()) #endif { FtpClient ftp(orden.ipto, "root", ""); try { /** Copia ficheros en remoto... */ ftp.Login(); ftp.Upload(orden.src, orden.dest); ftp.Close(); NLOG_DEBUG("FTP. Fichero %s Sincronizado.", orden.src.c_str()); } catch(FtpClientException x) { /** Normalmente si salta el error vacio la lista para evitar encadenar errores. */ ordenes.clear(); NLOG_ERROR("FTP EXCEPTION: %s", x.what().c_str()); } } } if (_sincronizando) { /** Avisa Cambio Ficheros... */ HttpClient::http.AvisaCambioConfiguracion(orden.ipto + ":" + LocalConfig::cfg.PuertoEscucha()); } } }
/** * 配置初始化 */ int TestContext::cfg_init(const char* config, ngx_tcp_server_srv_conf_t* tcp_svr_cfg) { test_config_t* testcfg = &this->m_config; CIniFile inifile(config); if(!inifile.ReadFile()){ CONF_ERROR("read config [%s] failed! err: %s", config, strerror(errno)); return NGX_ERROR; } string ip = inifile.GetValue("backend", "ip", "127.0.0.1"); strncpy(testcfg->backend_ip, ip.c_str(), sizeof(testcfg->backend_ip)); testcfg->backend_port = inifile.GetValueI("backend", "port", 2014); mysql_config_read(&inifile, &testcfg->mysql_config, "mysql"); //这一步很重要,当使用异步方式时,timeout必须为0,否则mysql_connect会出错。 if(tcp_svr_cfg->use_async){ testcfg->mysql_config.timeout = 0; } NLOG_DEBUG("mysql: config:%s", testcfg->mysql_config.host); return NGX_OK; }
static ngx_int_t ngx_http_shmtest_get(ngx_http_request_t *r){ ngx_int_t rc = NGX_HTTP_OK; ngx_str_t key = ngx_null_string; ngx_str_t value = ngx_null_string; int32_t ikey = 0; uint8_t value_type = VT_BINARY; uint32_t exptime = 0; uint32_t user_flags = 0; if(ngx_http_arg(r, (u_char*)"key", 3, &key)!=NGX_OK){ NLOG_ERROR("get arg 'key' failed!"); return NGX_HTTP_BAD_REQUEST; } if(key.len > 2 && key.data[0] == '0' && key.data[1] == 'x'){ key.data += 2; key.len -= 2; ikey = ngx_hextoi(key.data, key.len); ngx_str_set_int32(&key, &ikey); NLOG_DEBUG("use int key ikey=%d", ikey); } shmtest_main_conf_t* smcf; smcf = ngx_http_get_module_main_conf(r, ngx_http_shmtest_module); if(smcf == NULL){ NLOG_ERROR("get module ngx_http_shmtest_module's main conf failed!"); return NGX_HTTP_INTERNAL_SERVER_ERROR; } ngx_shm_zone_t* zone = smcf->shmap; u_char* rsp = ngx_pcalloc(r->connection->pool, 256); int rsp_len = 0; rc = ngx_shmap_get(zone, &key, &value, &value_type, &exptime, &user_flags); if(ikey != 0){ if(rc == 0){ rsp_len = ngx_sprintf(rsp, "get(%d)={value=%V,exptime=%d,user_flags=%d}!\n", ikey,&value,exptime,user_flags)-rsp; }else{ rsp_len = ngx_sprintf(rsp, "get(%d) failed!\n", ikey)-rsp; } }else{ if(rc == 0){ rsp_len = ngx_sprintf(rsp, "get(%V)={value=%V,exptime=%d,user_flags=%d}!\n", &key,&value,exptime,user_flags)-rsp; }else{ rsp_len = ngx_sprintf(rsp, "get(%V) failed!\n", &key)-rsp; } } ngx_chain_t* chain = ngx_http_shmtest_resp(r, (char*)rsp, rsp_len); if(chain != NULL){ r->headers_out.content_length_n = rsp_len; }else{ r->headers_out.content_length_n = 0; } rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { }else{ rc = ngx_http_output_filter(r, chain); } return rc; }
static ngx_int_t ngx_http_shmtest_add_or_update(ngx_http_request_t *r,int func){ ngx_int_t rc = NGX_HTTP_OK; ngx_str_t key = ngx_null_string; int32_t ikey = 0; ngx_str_t value = ngx_null_string; char* szFunc = funcs[func]; if(ngx_http_arg(r, (u_char*)"key", 3, &key)!=NGX_OK){ NLOG_ERROR("get arg 'key' failed!"); return NGX_HTTP_BAD_REQUEST; } if(ngx_http_arg(r, (u_char*)"value", 5, &value)!=NGX_OK){ NLOG_ERROR("get arg 'value' failed!"); return NGX_HTTP_BAD_REQUEST; } //如果key开始为0x 表示使用数字的KEY. if(key.len > 2 && key.data[0] == '0' && key.data[1] == 'x'){ key.data += 2; key.len -= 2; ikey = ngx_hextoi(key.data, key.len); ngx_str_set_int32(&key, &ikey); NLOG_DEBUG("use int key ikey=%d", ikey); } uint64_t exptime = 0; ngx_str_t sexptime = ngx_null_string; if(ngx_http_arg(r, (u_char*)"exptime", 7, &sexptime)==NGX_OK){ exptime = ngx_parse_time(&sexptime, 1); } if(ikey != 0){ NLOG_DEBUG("%s(key=%d,value=%V,exptime=%d)", szFunc,ikey,&value,exptime); }else{ NLOG_DEBUG("%s(key=%V,value=%V,exptime=%d)", szFunc,&key,&value,exptime); } shmtest_main_conf_t* smcf; smcf = ngx_http_get_module_main_conf(r, ngx_http_shmtest_module); if(smcf == NULL){ NLOG_ERROR("get module ngx_http_shmtest_module's main conf failed!"); return NGX_HTTP_INTERNAL_SERVER_ERROR; } ngx_shm_zone_t* zone = smcf->shmap; int ret = 0; switch(func){ case FUNC_ADD: ret = ngx_shmap_add(zone, &key,&value,VT_STRING,exptime,0); break; case FUNC_SET: ret = ngx_shmap_set(zone, &key,&value,VT_STRING,exptime,0); break; case FUNC_REPLACE: ret = ngx_shmap_replace(zone, &key,&value,VT_STRING,exptime,0); break; default: NLOG_ERROR("un process type [%d]", func); return NGX_HTTP_BAD_REQUEST; } char* rsp = ngx_pcalloc(r->connection->pool, 256); int rsp_len = 0; if(ret == 0){ rsp_len = sprintf(rsp, "%s success!\n", szFunc); }else{ rsp_len = sprintf(rsp, "%s failed!\n", szFunc); } ngx_chain_t* chain = ngx_http_shmtest_resp(r, rsp, rsp_len); if(chain != NULL){ r->headers_out.content_length_n = rsp_len; }else{ r->headers_out.content_length_n = 0; } rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { }else{ rc = ngx_http_output_filter(r, chain); } return rc; }