Exemple #1
0
        public async Task <bool> AddProjectAsync(Project project)
        {
            CheckDisposed();

            using (await _lock.WriterLockAsync())
            {
                Engine engine = project.IsShared
                                        ? await _engines.GetByLanguageTagAsync(project.SourceLanguageTag, project.TargetLanguageTag)
                                        : null;

                try
                {
                    if (engine == null)
                    {
                        // no existing shared engine or a project-specific engine
                        engine = new Engine
                        {
                            Projects          = { project.Id },
                            IsShared          = project.IsShared,
                            SourceLanguageTag = project.SourceLanguageTag,
                            TargetLanguageTag = project.TargetLanguageTag
                        };
                        await _engines.InsertAsync(engine);

                        EngineRuntime runtime = CreateRuntime(engine.Id);
                        await runtime.InitNewAsync();
                    }
                    else
                    {
                        // found an existing shared engine
                        if (engine.Projects.Contains(project.Id))
                        {
                            return(false);
                        }
                        engine = await _engines.ConcurrentUpdateAsync(engine, e => e.Projects.Add(project.Id));
                    }
                }
                catch (KeyAlreadyExistsException)
                {
                    // a project with the same id already exists
                    return(false);
                }

                project.EngineRef = engine.Id;
                await _projects.InsertAsync(project);

                return(true);
            }
        }
        public async Task <Project> AddProjectAsync(string projectId, string sourceLanguageTag, string targetLanguageTag,
                                                    string sourceSegmentType, string targetSegmentType, bool isShared)
        {
            CheckDisposed();

            using (await _lock.WriterLockAsync())
            {
                Engine engine = isShared
                                        ? await _engineRepo.GetByLanguageTagAsync(sourceLanguageTag, targetLanguageTag)
                                        : null;

                try
                {
                    EngineRunner runner;
                    if (engine == null)
                    {
                        // no existing shared engine or a project-specific engine
                        engine = new Engine
                        {
                            Projects          = { projectId },
                            IsShared          = isShared,
                            SourceLanguageTag = sourceLanguageTag,
                            TargetLanguageTag = targetLanguageTag
                        };
                        await _engineRepo.InsertAsync(engine);

                        runner = CreateRunner(engine.Id);
                        await runner.InitNewAsync();
                    }
                    else
                    {
                        // found an existing shared engine
                        if (engine.Projects.Contains(projectId))
                        {
                            return(null);
                        }
                        engine = await _engineRepo.ConcurrentUpdateAsync(engine, e => e.Projects.Add(projectId));

                        runner = GetOrCreateRunner(engine.Id);
                    }
                    await runner.StartBuildAsync(engine);
                }
                catch (KeyAlreadyExistsException)
                {
                    // a project with the same id already exists
                    return(null);
                }

                var project = new Project
                {
                    Id = projectId,
                    SourceLanguageTag = sourceLanguageTag,
                    TargetLanguageTag = targetLanguageTag,
                    SourceSegmentType = sourceSegmentType,
                    TargetSegmentType = targetSegmentType,
                    IsShared          = isShared,
                    Engine            = engine.Id
                };
                await _projectRepo.InsertAsync(project);

                return(project);
            }
        }