private void OnImportInstance(string msg, string reply, string subject) { Logger.Debug(Strings.OnImportInstanceDebugLogMessage, ServiceDescription(), msg, reply); object[] credentials = new object[0]; credentials = JsonConvertibleObject.DeserializeFromJsonArray(msg); ProvisionedServicePlanType plan = (ProvisionedServicePlanType)Enum.Parse(typeof(ProvisionedServicePlanType), JsonConvertibleObject.ObjectToValue <string>(credentials[0])); ServiceCredentials prov_cred = new ServiceCredentials(); ServiceCredentials binding_creds = new ServiceCredentials(); prov_cred.FromJsonIntermediateObject(credentials[1]); binding_creds.FromJsonIntermediateObject(credentials[2]); prov_cred.ServiceWorkFactory.StartNew( () => { try { string instance = prov_cred.Name; string file_path = this.GetMigrationFolder(instance); bool result = this.ImportInstance(prov_cred, binding_creds, file_path, plan); NodeNats.Publish(reply, null, result.ToString()); } catch (Exception ex) { Logger.Warning(ex.ToString()); } }); }
private void SendNodeAnnouncement(string msg = null, string reply = null) { try { if (!this.NodeReady()) { Logger.Debug(Strings.SendNodeAnnouncementNotReadyDebugLogMessage, ServiceDescription()); return; } Logger.Debug(Strings.SendNodeAnnouncementDebugLogMessage, ServiceDescription(), reply != null ? reply : "everyone"); DiscoverMessage discoverMessage = new DiscoverMessage(); if (msg != null) { discoverMessage.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg)); } if (string.IsNullOrEmpty(discoverMessage.Plan) || discoverMessage.Plan == this.plan) { Announcement a = this.AnnouncementDetails; a.Id = this.nodeId; a.Plan = this.plan; NodeNats.Publish(reply != null ? reply : string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectAnnounce, ServiceName()), null, a.SerializeToJson()); } } catch (Exception ex) { Logger.Warning(ex.ToString()); } }
private void OnPurgeOrphan(string msg, string reply, string subject) { // This may take a long time (it can unbind many services), so we run it on a different thread; ThreadPool.QueueUserWorkItem( (data) => { Logger.Debug(Strings.OnPurgeOrphanDebugLogMessage, ServiceDescription()); SimpleResponse response = new SimpleResponse(); try { PurgeOrphanRequest request = new PurgeOrphanRequest(); request.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg)); bool result = this.PurgeOrphan(request.OrphanInsList, request.OrphanBindingList); if (result) { NodeNats.Publish(reply, null, EncodeSuccess(response)); } else { NodeNats.Publish(reply, null, EncodeFailure(response)); } } catch (Exception ex) { Logger.Warning(ex.ToString()); NodeNats.Publish(reply, null, EncodeFailure(response, ex)); } }); }
/// <summary> /// This is called after the node is connected to NATS. /// </summary> protected override void OnConnectNode() { Logger.Debug(Strings.ConnectedLogMessage, ServiceDescription()); NodeNats.Subscribe(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectProvision, this.ServiceName(), this.nodeId), new SubscribeCallback(this.OnProvision)); NodeNats.Subscribe(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectUnprovision, this.ServiceName(), this.nodeId), new SubscribeCallback(this.OnUnprovision)); NodeNats.Subscribe(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectBind, this.ServiceName(), this.nodeId), new SubscribeCallback(this.OnBind)); NodeNats.Subscribe(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectUnbind, this.ServiceName(), this.nodeId), new SubscribeCallback(this.OnUnbind)); NodeNats.Subscribe(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectRestore, this.ServiceName(), this.nodeId), new SubscribeCallback(this.OnRestore)); NodeNats.Subscribe(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectDiscover, this.ServiceName()), new SubscribeCallback(this.OnDiscover)); NodeNats.Subscribe(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectDisableInstance, this.ServiceName(), this.nodeId), new SubscribeCallback(this.OnDisableInstance)); NodeNats.Subscribe(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectEnableInstance, this.ServiceName(), this.nodeId), new SubscribeCallback(this.OnEnableInstance)); NodeNats.Subscribe(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectImportInstance, this.ServiceName(), this.nodeId), new SubscribeCallback(this.OnImportInstance)); NodeNats.Subscribe(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectCleanupNfs, this.ServiceName(), this.nodeId), new SubscribeCallback(this.OnCleanupNfs)); NodeNats.Subscribe(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectCheckOrphan, this.ServiceName()), new SubscribeCallback(this.OnCheckOrphan)); NodeNats.Subscribe(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectPurgeOrphan, this.ServiceName(), this.nodeId), new SubscribeCallback(this.OnPurgeOrphan)); SendNodeAnnouncement(); TimerHelper.RecurringCall( 30000, delegate { SendNodeAnnouncement(); }); }
private void OnCleanupNfs(string msg, string reply, string subject) { Logger.Debug(Strings.CleanupNfsLogMessage, ServiceDescription(), msg, reply); object[] request = new object[0]; request = JsonConvertibleObject.DeserializeFromJsonArray(msg); ServiceCredentials prov_cred = new ServiceCredentials(); ServiceCredentials binding_creds = new ServiceCredentials(); prov_cred.FromJsonIntermediateObject(request[0]); binding_creds.FromJsonIntermediateObject(request[1]); prov_cred.ServiceWorkFactory.StartNew( () => { try { string instance = prov_cred.Name; Directory.Delete(this.GetMigrationFolder(instance), true); NodeNats.Publish(reply, null, "true"); } catch (Exception ex) { Logger.Warning(ex.ToString()); } }); }
private void OnCheckOrphan(string msg, string reply, string subject) { Logger.Debug(Strings.CheckOrphanLogMessage, ServiceDescription()); CheckOrphanResponse response = new CheckOrphanResponse(); try { CheckOrphanRequest request = new CheckOrphanRequest(); request.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg)); // TODO: vladi: investigate further if this needs to be parallelized; seems that this would take a very short time, and it's not tied to a specific instance, so it could run on a new thread this.CheckOrphan(request.Handles); response.OrphanInstances = OrphanInstancesHash; response.OrphanBindings = OrphanBindingHash; response.Success = true; } catch (Exception ex) { Logger.Warning(Strings.CheckOrphanExceptionLogMessage, ex.ToString()); response.Success = false; response.Error = new Dictionary <string, object>() { { "message", ex.Message }, { "stack", ex.StackTrace } }; } finally { NodeNats.Publish(string.Format(CultureInfo.InvariantCulture, Strings.NatsSubjectOrphanResult, ServiceName()), null, response.SerializeToJson()); } }
private void OnRestore(string msg, string reply, string subject) { Logger.Debug(Strings.OnRestoreDebugLogMessage, ServiceDescription(), msg, reply); SimpleResponse response = new SimpleResponse(); try { RestoreRequest restore_message = new RestoreRequest(); restore_message.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg)); string instance_id = restore_message.InstanceId; string backup_path = restore_message.BackupPath; // TODO: vladi: need to make this parallel when we implement the actual restore for mssql bool result = this.Restore(instance_id, backup_path); if (result) { NodeNats.Publish(reply, null, EncodeSuccess(response)); } else { NodeNats.Publish(reply, null, EncodeFailure(response)); } } catch (Exception ex) { Logger.Warning(ex.ToString()); NodeNats.Publish(reply, null, EncodeFailure(response, ex)); } }
private void OnEnableInstance(string msg, string reply, string subject) { Logger.Debug(Strings.OnEnableInstanceDebugMessage, ServiceDescription(), msg, reply); object[] credentials = new object[0]; credentials = JsonConvertibleObject.DeserializeFromJsonArray(msg); ServiceCredentials prov_cred = new ServiceCredentials(); Dictionary <string, object> binding_creds_hash = new Dictionary <string, object>(); prov_cred.FromJsonIntermediateObject(credentials[0]); binding_creds_hash = JsonConvertibleObject.ObjectToValue <Dictionary <string, object> >(credentials[1]); prov_cred.ServiceWorkFactory.StartNew( () => { try { this.EnableInstance(ref prov_cred, ref binding_creds_hash); // Update node_id in provision credentials.. prov_cred.NodeId = this.nodeId; credentials[0] = prov_cred; credentials[1] = binding_creds_hash; NodeNats.Publish(reply, null, JsonConvertibleObject.SerializeToJson(credentials)); } catch (Exception ex) { Logger.Warning(ex.ToString()); } }); }
private void OnUnbind(string msg, string reply, string subject) { Logger.Debug(Strings.UnbindRequestDebugLogMessage, ServiceDescription(), msg, reply); SimpleResponse response = new SimpleResponse(); UnbindRequest unbind_req = new UnbindRequest(); unbind_req.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg)); unbind_req.Credentials.ServiceWorkFactory.StartNew( () => { try { bool result = this.Unbind(unbind_req.Credentials); if (result) { this.NodeNats.Publish(reply, null, EncodeSuccess(response)); } else { this.NodeNats.Publish(reply, null, EncodeFailure(response)); } } catch (Exception ex) { Logger.Warning(ex.ToString()); NodeNats.Publish(reply, null, EncodeFailure(response, ex)); } }); }
private void OnBind(string msg, string reply, string subject) { Logger.Debug(Strings.BindRequestLogMessage, ServiceDescription(), msg, reply); BindResponse response = new BindResponse(); BindRequest bind_message = new BindRequest(); bind_message.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg)); string name = bind_message.Name; // Create a service credentials object with a name set, so we can run this operation in parallel for different service instances. ServiceCredentials nameCredentials = new ServiceCredentials(); nameCredentials.Name = name; nameCredentials.ServiceWorkFactory.StartNew( () => { try { Dictionary <string, object> bind_opts = bind_message.BindOptions; ServiceCredentials credentials = bind_message.Credentials; response.Credentials = this.Bind(name, bind_opts, credentials); NodeNats.Publish(reply, null, EncodeSuccess(response)); } catch (Exception ex) { Logger.Warning(ex.ToString()); NodeNats.Publish(reply, null, EncodeFailure(response, ex)); } }); }
private void OnProvision(string msg, string reply, string subject) { Logger.Debug(Strings.OnProvisionRequestDebugLogMessage, ServiceDescription(), msg, reply); ProvisionResponse response = new ProvisionResponse(); ProvisionRequest provision_req = new ProvisionRequest(); provision_req.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg)); ProvisionedServicePlanType plan = provision_req.Plan; ServiceCredentials credentials = provision_req.Credentials; if (credentials == null) { credentials = GenerateCredentials(); } credentials.ServiceWorkFactory.StartNew( () => { try { ServiceCredentials credential = Provision(plan, credentials); credential.NodeId = this.nodeId; response.Credentials = credential; Logger.Debug( Strings.OnProvisionSuccessDebugLogMessage, ServiceDescription(), msg, response.SerializeToJson()); NodeNats.Publish(reply, null, EncodeSuccess(response)); } catch (Exception ex) { Logger.Warning(ex.ToString()); NodeNats.Publish(reply, null, EncodeFailure(response)); } }); }
private void OnUnprovision(string msg, string reply, string subject) { Logger.Debug(Strings.UnprovisionRequestDebugLogMessage, ServiceDescription(), msg); SimpleResponse response = new SimpleResponse(); UnprovisionRequest unprovision_req = new UnprovisionRequest(); unprovision_req.FromJsonIntermediateObject(JsonConvertibleObject.DeserializeFromJson(msg)); string name = unprovision_req.Name; // Create a service credentials object with a name set, so we can run this operation in parallel for different service instances. ServiceCredentials credentials = new ServiceCredentials(); credentials.Name = name; credentials.ServiceWorkFactory.StartNew( () => { try { ServiceCredentials[] bindings = unprovision_req.Bindings; bool result = this.Unprovision(name, bindings); if (result) { this.NodeNats.Publish(reply, null, EncodeSuccess(response)); } else { this.NodeNats.Publish(reply, null, EncodeFailure(response)); } } catch (Exception ex) { Logger.Warning(ex.ToString()); NodeNats.Publish(reply, null, EncodeFailure(response, ex)); } }); }
private void OnDisableInstance(string msg, string reply, string subject) { Logger.Debug(Strings.OnDisableInstanceDebugLogMessage, ServiceDescription(), msg, reply); object[] credentials = new object[0]; credentials = JsonConvertibleObject.DeserializeFromJsonArray(msg); ServiceCredentials prov_cred = new ServiceCredentials(); ServiceCredentials binding_creds = new ServiceCredentials(); prov_cred.FromJsonIntermediateObject(credentials[0]); binding_creds.FromJsonIntermediateObject(credentials[1]); prov_cred.ServiceWorkFactory.StartNew( () => { try { string instance = prov_cred.Name; string file_path = this.GetMigrationFolder(instance); Directory.CreateDirectory(file_path); bool result = this.DisableInstance(prov_cred, binding_creds); if (result) { result = this.DumpInstance(prov_cred, binding_creds, file_path); } NodeNats.Publish(reply, null, result.ToString()); } catch (Exception ex) { Logger.Warning(ex.ToString()); } }); }