/// <summary> /// Discovers schemas located in the users Zoho CRM instance /// </summary> /// <param name="request"></param> /// <param name="context"></param> /// <returns>Discovered schemas</returns> public override async Task <DiscoverSchemasResponse> DiscoverSchemas(DiscoverSchemasRequest request, ServerCallContext context) { Logger.SetLogPrefix("discover"); Logger.Info("Discovering Schemas..."); var sampleSize = checked ((int)request.SampleSize); DiscoverSchemasResponse discoverSchemasResponse = new DiscoverSchemasResponse(); // only return requested schemas if refresh mode selected if (request.Mode == DiscoverSchemasRequest.Types.Mode.All) { // get all schemas try { var schemas = Discover.GetAllSchemas(_sessionFactory, sampleSize); discoverSchemasResponse.Schemas.AddRange(await schemas.ToListAsync()); Logger.Info($"Schemas returned: {discoverSchemasResponse.Schemas.Count}"); return(discoverSchemasResponse); } catch (Exception e) { Logger.Error(e, e.Message, context); return(new DiscoverSchemasResponse()); } } try { var refreshSchemas = request.ToRefresh; Logger.Info($"Refresh schemas attempted: {refreshSchemas.Count}"); var schemas = Discover.GetRefreshSchemas(_sessionFactory, refreshSchemas, sampleSize); discoverSchemasResponse.Schemas.AddRange(await schemas.ToListAsync()); // return all schemas Logger.Info($"Schemas returned: {discoverSchemasResponse.Schemas.Count}"); return(discoverSchemasResponse); } catch (Exception e) { Logger.Error(e, e.Message, context); return(new DiscoverSchemasResponse()); } }
/// <summary> /// Configures replication writebacks to Cassandra /// </summary> /// <param name="request"></param> /// <param name="context"></param> /// <returns></returns> public override Task <ConfigureReplicationResponse> ConfigureReplication(ConfigureReplicationRequest request, ServerCallContext context) { Logger.SetLogPrefix("configure_replication"); Logger.Info($"Configuring write for schema name {request.Schema.Name}..."); var schemaJson = Replication.GetSchemaJson(); var uiJson = Replication.GetUIJson(); try { var errors = new List <string>(); if (!string.IsNullOrWhiteSpace(request.Form.DataJson)) { // check for config errors var replicationFormData = JsonConvert.DeserializeObject <ConfigureReplicationFormData>(request.Form.DataJson); errors = replicationFormData.ValidateReplicationFormData(); } return(Task.FromResult(new ConfigureReplicationResponse { Form = new ConfigurationFormResponse { DataJson = request.Form.DataJson, Errors = { errors }, SchemaJson = schemaJson, UiJson = uiJson, StateJson = request.Form.StateJson } })); } catch (Exception e) { Logger.Error(e, e.Message, context); return(Task.FromResult(new ConfigureReplicationResponse { Form = new ConfigurationFormResponse { DataJson = request.Form.DataJson, Errors = { e.Message }, SchemaJson = schemaJson, UiJson = uiJson, StateJson = request.Form.StateJson } })); } }
public override async Task ConnectSession(ConnectRequest request, IServerStreamWriter <ConnectResponse> responseStream, ServerCallContext context) { Logger.SetLogPrefix("connect_session"); Logger.Info("Connecting session..."); // create task to wait for disconnect to be called _tcs?.SetResult(true); _tcs = new TaskCompletionSource <bool>(); // call connect method var response = await Connect(request, context); await responseStream.WriteAsync(response); Logger.Info("Session connected."); // wait for disconnect to be called await _tcs.Task; }
/// <summary> /// Prepares writeback settings to write to Cassandra CQL3 /// </summary> /// <param name="request"></param> /// <param name="context"></param> /// <returns></returns> public override async Task <PrepareWriteResponse> PrepareWrite(PrepareWriteRequest request, ServerCallContext context) { // Logger.SetLogLevel(Logger.LogLevel.Debug); Logger.SetLogPrefix(request.DataVersions.JobId); Logger.Info("Preparing write..."); _server.WriteConfigured = false; _server.WriteSettings = new WriteSettings { CommitSLA = request.CommitSlaSeconds, Schema = request.Schema, Replication = request.Replication, DataVersions = request.DataVersions, }; if (_server.WriteSettings.IsReplication()) { // reconcile job Logger.Info($"Starting to reconcile Replication Job {request.DataVersions.JobId}"); try { await Replication.ReconcileReplicationJobAsync(_sessionFactory, request); } catch (Exception e) { Logger.Error(e, e.Message, context); return(new PrepareWriteResponse()); } Logger.Info($"Finished reconciling Replication Job {request.DataVersions.JobId}"); } _server.WriteConfigured = true; Logger.Debug(JsonConvert.SerializeObject(_server.WriteSettings, Formatting.Indented)); Logger.Info("Write prepared."); return(new PrepareWriteResponse()); }
/// <summary> /// Publishes a stream of data for a given schema /// </summary> /// <param name="request"></param> /// <param name="responseStream"></param> /// <param name="context"></param> /// <returns></returns> public override async Task ReadStream(ReadRequest request, IServerStreamWriter <Record> responseStream, ServerCallContext context) { try { var schema = request.Schema; var limit = request.Limit; var limitFlag = request.Limit != 0; var jobId = request.JobId; var recordsCount = 0; Logger.SetLogPrefix(jobId); var records = Read.ReadRecords(_sessionFactory, schema); await foreach (var record in records) { // stop publishing if the limit flag is enabled and the limit has been reached or the server is disconnected if (limitFlag && recordsCount == limit || !_server.Connected) { break; } // publish record await responseStream.WriteAsync(record); recordsCount++; } Logger.Info($"Published {recordsCount} records"); } catch (Exception e) { Logger.Error(e, e.Message, context); } }
/// <summary> /// Establishes a connection with Cassandra. /// </summary> /// <param name="request"></param> /// <param name="context"></param> /// <returns>A message indicating connection success</returns> public override async Task <ConnectResponse> Connect(ConnectRequest request, ServerCallContext context) { Logger.SetLogPrefix("connect"); // validate settings passed in try { _server.Settings = JsonConvert.DeserializeObject <Settings>(request.SettingsJson); _server.Settings.Validate(); } catch (Exception e) { Logger.Error(e, e.Message); return(new ConnectResponse { OauthStateJson = request.OauthStateJson, ConnectionError = "", OauthError = "", SettingsError = e.Message }); } // initialize connection factory try { _sessionFactory.Initialize(_server.Settings); } catch (Exception e) { Logger.Error(e, e.Message); return(new ConnectResponse { OauthStateJson = request.OauthStateJson, ConnectionError = e.Message, OauthError = "", SettingsError = "" }); } // test cluster factory //var conn = _sessionFactory.GetConnection(); var session = Cluster.Builder() .AddContactPoint(_server.Settings.Hostname).WithPort(Int32.Parse(_server.Settings.Port)) .WithCredentials(_server.Settings.Username, _server.Settings.Password) .Build(); try { session.Connect(); } catch (Exception e) { Logger.Error(e, e.Message); return(new ConnectResponse { OauthStateJson = request.OauthStateJson, ConnectionError = e.Message, OauthError = "", SettingsError = "" }); } finally { await session.ShutdownAsync(); } // try // { // await conn.OpenAsync(); // // if (!await conn.PingAsync()) // { // return new ConnectResponse // { // OauthStateJson = request.OauthStateJson, // ConnectionError = "Unable to ping target database.", // OauthError = "", // SettingsError = "" // }; // } // } // catch (Exception e) // { // Logger.Error(e, e.Message, context); // // return new ConnectResponse // { // OauthStateJson = request.OauthStateJson, // ConnectionError = e.Message, // OauthError = "", // SettingsError = "" // }; // } // finally // { // await conn.CloseAsync(); // } _server.Connected = true; return(new ConnectResponse { OauthStateJson = request.OauthStateJson, ConnectionError = "", OauthError = "", SettingsError = "" }); }