示例#1
0
        private static async Task RunAsync(IWorkerOptions options)
        {
            if (string.IsNullOrEmpty(options.LogFileName))
            {
                options.LogFileName = Path.Combine(Environment.CurrentDirectory, options.WorkerName ?? WorkerType + ".log");
            }

            Log.Logger = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .MinimumLevel.Debug()
                         .WriteTo.Console()
                         .WriteTo.File(options.LogFileName)
                         .CreateLogger();

            Log.Debug($"Opened logfile {options.LogFileName}");

            var connectionParameters = new ConnectionParameters
            {
                EnableProtocolLoggingAtStartup = true,
                ProtocolLogging = new ProtocolLoggingParameters
                {
                    LogPrefix = Path.ChangeExtension(options.LogFileName, string.Empty) + "-protocol"
                },
                WorkerType             = WorkerType,
                DefaultComponentVtable = new ComponentVtable()
            };

            using (var connection = await WorkerConnection.ConnectAsync(options, connectionParameters).ConfigureAwait(false))
            {
                connection.StartSendingMetrics();

                foreach (var opList in connection.GetOpLists())
                {
                    ProcessOpList(opList);
                }
            }

            Log.Information("Disconnected from SpatialOS");
        }
示例#2
0
        private static async Task RunAsync(IOptions options)
        {
            if (string.IsNullOrEmpty(options.LogFileName))
            {
                options.LogFileName = Path.Combine(Environment.CurrentDirectory, options.WorkerName + ".log");
            }

            Log.Logger = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .MinimumLevel.Debug()
                         .WriteTo.Console()
                         .WriteTo.File(options.LogFileName)
                         .CreateLogger();

            Log.Debug($"Opened logfile {options.LogFileName}");

            var connectionParameters = new ConnectionParameters
            {
                EnableProtocolLoggingAtStartup = true,
                ProtocolLogging = new ProtocolLoggingParameters
                {
                    LogPrefix = Path.ChangeExtension(options.LogFileName, string.Empty) + "-protocol"
                },
                WorkerType             = WorkerType,
                DefaultComponentVtable = new ComponentVtable()
            };


            using (var connection = await WorkerConnection.ConnectAsync(options, connectionParameters))
            {
                var postgresOptions             = new PostgresOptions(GetPostgresFlags(options, connection));
                DatabaseSyncLogic databaseLogic = null;

                var tableName = connection.GetWorkerFlag("postgres_tablename") ?? "postgres";

                var databaseService = Task.Run(async() =>
                {
                    using (var response = await connection.SendEntityQueryRequest(new EntityQuery {
                        Constraint = new ComponentConstraint(DatabaseSyncService.ComponentId), ResultType = new SnapshotResultType()
                    }))
                    {
                        if (response.ResultCount == 0)
                        {
                            throw new ServiceNotFoundException(nameof(DatabaseSyncService));
                        }

                        databaseLogic = new DatabaseSyncLogic(postgresOptions, tableName, connection, response.Results.First().Key, DatabaseSyncService.CreateFromSnapshot(response.Results.First().Value));
                        connection.StartSendingMetrics(databaseLogic.UpdateMetrics);
                    }
                });

                using (var databaseChanges = new DatabaseChanges <DatabaseSyncItem.DatabaseChangeNotification>(postgresOptions, tableName))
                {
                    foreach (var opList in connection.GetOpLists(TimeSpan.FromMilliseconds(16)))
                    {
                        var changes = databaseChanges.GetChanges();

                        if (!changes.IsEmpty)
                        {
                            databaseLogic?.ProcessDatabaseSyncChanges(changes);
                        }

                        ProcessOpList(opList);

                        if (options.PostgresFromWorkerFlags)
                        {
                            postgresOptions.ProcessOpList(opList);
                        }

                        connection.ProcessOpList(opList);
                        databaseLogic?.ProcessOpList(opList);

                        // Propagate exceptions.
                        databaseService.Wait(TimeSpan.FromTicks(1));
                    }
                }
            }

            Log.Information("Disconnected from SpatialOS");
        }