def sendUpdatesWrite (proxy_id): """ Sends the updates for a specific soft-proxy to the main server according to the list of updates. This is the version for Request Write with updates only :param proxy_id: :return: tuple boolean/string (true/false, list of updates for logging/errors) """ # each meta is listed with its time of latest modification updateslist = [] updatespath = os.path.join(conf.baseproxypath,proxy_id,"next") for cupdate in os.listdir(updatespath): updateslist.append((cupdate, os.path.getmtime(os.path.join(updatespath, cupdate)))) locker = proxy_lock.ProxyLocker (retries=3, wait=5) meta_dict = {} for meta_id, timestamp in updateslist: meta_dict [meta_id] = locker.performLocked(proxy_core.assembleMetaJson, proxy_id, meta_id) metadelstring = conf.MAINSERVER_LOC+"/broker/clear?token="+proxy_id+"&meta="+meta_id urllib2.urlopen(metadelstring) template = TemplatesModels.model_request_write customfields = { "data": { "upserts" : meta_dict, "delete" : [] } } # /broker/clear?token=???&meta=??? requestmsg = Common.Components.createMessageFromTemplate(template, token=proxy_id, **customfields) send_success = sendMessageToServer(json.dumps(requestmsg), conf.URL_WRITEREQUEST, 'POST', TemplatesModels.model_response_write, TemplatesModels.model_error_error) if send_success: #proceed to clean up the /next directory from meta that has not received other modifications, log the success with the list of metas sent for cupdate in os.listdir(updatespath): # we remove only the entries that have not received additional updates from the time of sending; we check if the tuple filename/updatetime is in the updates list we created at the start of the process if (cupdate, os.path.getmtime(os.path.join(updatespath, cupdate))) in updateslist: try: os.remove(os.path.join(updatespath,cupdate)) except: logEvent ("Failed to remove meta %s from the updates list of proxy %s" % (cupdate, proxy_id), True) logEvent ("Sent updates for proxy %s to main server", False) else: #leave the /next listing as is and log the failure as issue logEvent ("Failed to send updates for proxy %s (meta/timestamp list: %s)" % (proxy_id, updateslist), True)
def sendProxyManifestRaw (jsonmanifest): """ Sends the manifest of a given soft proxy to the main server and returns the response :param jsonmanifest: :return: """ print "Expected replies for manifest send:\n%s\n%s" % (TemplatesModels.model_response_manifest_success, TemplatesModels.model_response_manifest_fail) try: return sendMessageToServer(jsonmanifest, conf.URL_WRITEMANIFEST, 'POST', TemplatesModels.model_response_manifest_success, TemplatesModels.model_response_manifest_fail) except Exception as ex: return False, "Error while sending manifest to %s: %s" % (conf.URL_WRITEMANIFEST, str(ex))