Beispiel #1
0
        private static void Build(BuilderController builderController)
        {
            var dbChunk = new DbChunk(Settings.Current.Building.BuilderConnectionString);
            int?chunkId = null;

            while (true)
            {
                try
                {
                    builderController.RefreshState();
                    if (builderController.Builder.State == BuilderState.Stopping)
                    {
                        break;
                    }

                    if (builderController.Builder.State == BuilderState.Stopped)
                    {
                        break;
                    }

                    if (builderController.Builder.State == BuilderState.Error)
                    {
                        break;
                    }

                    if (builderController.Builder.State == BuilderState.Unknown ||
                        builderController.Builder.State == BuilderState.Idle)
                    {
                        continue;
                    }

                    chunkId = dbChunk.TakeChunk(Settings.Current.Building.Id.Value, Settings.Current.Builder.Id.Value, GetRandomBoolean());
                    if (!chunkId.HasValue)
                    {
                        break;
                    }

                    Console.WriteLine($"ChunkId={chunkId} in progress...");

                    var attempt    = 0;
                    var processing = false;
                    var chunk      = Settings.Current.Building.SourceEngine.GetChunkBuilder(chunkId.Value, CreatePersonBuilder);
                    while (!processing)
                    {
                        try
                        {
                            attempt++;
                            chunk.Process();
                            processing = true;
                        }
                        catch (Exception ex)
                        {
                            if (attempt <= 3)
                            {
                                Logger.Write(chunkId, LogMessageTypes.Warning, "chunk.Process attempt=" + attempt + ") " + Logger.CreateExceptionString(ex));
                                chunk = Settings.Current.Building.SourceEngine.GetChunkBuilder(chunkId.Value, CreatePersonBuilder);
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                    chunkId = null;
                }
                catch (Exception e)
                {
                    if (chunkId.HasValue)
                    {
                        Logger.WriteError(chunkId, e);
                        dbChunk.ChunkFailed(chunkId.Value, Settings.Current.Building.Id.Value);
                    }
                    else
                    {
                        Logger.Write(null, Settings.Current.Building.Id.Value, null, LogMessageTypes.Error,
                                     Logger.CreateExceptionString(e));
                    }


                    builderController.UpdateState(BuilderState.Error);
                }
            }
        }