public virtual async Task EnsureLoadedAsync(bool silent = false) { if (isLoaded) { return; } if (silent) { await ReadAsync(); } else { var logContext = (id : uniqueId.ToString(), name : GetType().Name); using (log.MeasureInformation(logContext, (ctx, w) => w .WriteProperty("action", "ActivateDomainObject") .WriteProperty("domainObjectType", ctx.name) .WriteProperty("domainObjectKey", ctx.id))) { await ReadAsync(); } } isLoaded = true; }
private void AddContentQueries(DomainId schemaId, string schemaType, string schemaName, IGraphType contentType, int pageSize) { AddField(new FieldType { Name = $"query{schemaType}Contents", Arguments = CreateContentQueryArguments(pageSize), ResolvedType = new ListGraphType(new NonNullGraphType(contentType)), Resolver = ResolveAsync((c, e) => { var contentQuery = BuildODataQuery(c); return(e.QueryContentsAsync(schemaId.ToString(), contentQuery)); }), Description = $"Query {schemaName} content items." }); AddField(new FieldType { Name = $"query{schemaType}ContentsWithTotal", Arguments = CreateContentQueryArguments(pageSize), ResolvedType = new ContentsResultGraphType(schemaType, schemaName, contentType), Resolver = ResolveAsync((c, e) => { var contentQuery = BuildODataQuery(c); return(e.QueryContentsAsync(schemaId.ToString(), contentQuery)); }), Description = $"Query {schemaName} content items with total count." }); }
public async Task Should_return_single_content_when_updating_content() { var query = @" mutation { updateMySchemaContent(id: ""<ID>"", data: <DATA>, expectedVersion: 10) { <FIELDS> } }".Replace("<ID>", contentId.ToString()).Replace("<DATA>", GetDataString()).Replace("<FIELDS>", TestContent.AllFields); commandContext.Complete(content); var result = await sut.QueryAsync(requestContext, new GraphQLQuery { Query = query }); var expected = new { data = new { updateMySchemaContent = TestContent.Response(content) } }; AssertResult(expected, result); A.CallTo(() => commandBus.PublishAsync( A <UpdateContent> .That.Matches(x => x.ContentId == content.Id && x.ExpectedVersion == 10 && x.Data.Equals(content.Data)))) .MustHaveHappened(); }
public async Task <IEnrichedContentEntity> FindAsync(Context context, string schemaIdOrName, DomainId id, long version = -1) { Guard.NotNull(context, nameof(context)); if (id == default) { throw new DomainObjectNotFoundException(id.ToString()); } var schema = await GetSchemaOrThrowAsync(context, schemaIdOrName); CheckPermission(context, schema); using (Profiler.TraceMethod <ContentQueryService>()) { IContentEntity?content; if (version > EtagVersion.Empty) { content = await FindByVersionAsync(context, id, version); } else { content = await FindCoreAsync(context, id, schema); } if (content == null || content.SchemaId.Id != schema.Id) { throw new DomainObjectNotFoundException(id.ToString()); } return(await TransformAsync(context, content)); } }
public async Task Should_invoke_grain_for_comments_command() { var command = CreateCommentsCommand(new CreateComment()); var context = CreateContextForCommand(command); var grain = A.Fake <ICommentsGrain>(); var result = "Completed"; A.CallTo(() => grainFactory.GetGrain <ICommentsGrain>(commentsId.ToString(), null)) .Returns(grain); A.CallTo(() => grain.ExecuteAsync(A <J <CommentsCommand> > .That.Matches(x => x.Value == command))) .Returns(new J <object>(result)); var isNextCalled = false; await sut.HandleAsync(context, c => { isNextCalled = true; return(Task.CompletedTask); }); Assert.True(isNextCalled); A.CallTo(() => grain.ExecuteAsync(A <J <CommentsCommand> > .That.Matches(x => x.Value == command))) .Returns(new J <object>(12)); }
public async Task Should_add_error_if_value_has_invalid_discriminator_schema() { var(_, sut, components) = Field(new ComponentFieldProperties { SchemaId = schemaId2 }); await sut.ValidateAsync(CreateValue(schemaId1.ToString(), "field", 1), errors, components : components); errors.Should().BeEquivalentTo( new[] { "Invalid component. Cannot find schema." }); }
public async Task Should_call_grain_to_start_backup() { var grain = A.Fake <IBackupGrain>(); A.CallTo(() => grainFactory.GetGrain <IBackupGrain>(appId.ToString(), null)) .Returns(grain); await sut.StartBackupAsync(appId, actor); A.CallTo(() => grain.BackupAsync(actor)) .MustHaveHappened(); }
public Task <DomainId?> GetRunningRuleIdAsync(DomainId appId, CancellationToken ct = default) { var grain = grainFactory.GetGrain <IRuleRunnerGrain>(appId.ToString()); return(grain.GetRunningRuleIdAsync()); }
public Task LogAsync(DomainId appId, RequestLog request) { if (!requestLogStore.IsEnabled) { return(Task.CompletedTask); } var storedRequest = new Request { Key = appId.ToString(), Properties = new Dictionary <string, string> { [FieldCosts] = request.Costs.ToString(CultureInfo.InvariantCulture) }, Timestamp = request.Timestamp }; Append(storedRequest, FieldAuthClientId, request.UserClientId); Append(storedRequest, FieldAuthUserId, request.UserId); Append(storedRequest, FieldBytes, request.Bytes); Append(storedRequest, FieldCosts, request.Costs); Append(storedRequest, FieldRequestElapsedMs, request.ElapsedMs); Append(storedRequest, FieldRequestMethod, request.RequestMethod); Append(storedRequest, FieldRequestPath, request.RequestPath); return(requestLogStore.LogAsync(storedRequest)); }
public Task ReadAsync(long expectedVersion = -2) { if (applyEvent != null) { foreach (var @event in events) { if (!applyEvent(@event)) { break; } } } if (expectedVersion > EtagVersion.Any && expectedVersion != Version) { if (Version == EtagVersion.Empty) { throw new DomainObjectNotFoundException(ownerKey.ToString() !); } else { throw new InconsistentStateException(Version, expectedVersion); } } return(Task.CompletedTask); }
public async Task <IEnrichedContentEntity?> FindAsync(Context context, string schemaIdOrName, DomainId id, long version = EtagVersion.Any, CancellationToken ct = default) { Guard.NotNull(context); using (Telemetry.Activities.StartActivity("ContentQueryService/FindAsync")) { var schema = await GetSchemaOrThrowAsync(context, schemaIdOrName, ct); IContentEntity?content; if (id.ToString().Equals(SingletonId, StringComparison.Ordinal)) { id = schema.Id; } if (version > EtagVersion.Empty) { content = await contentLoader.GetAsync(context.App.Id, id, version); } else { content = await FindCoreAsync(context, id, schema, ct); } if (content == null || content.SchemaId.Id != schema.Id) { return(null); } return(await TransformAsync(context, content, ct)); } }
private string CreateQuery(string query) { query = query .Replace("<ID>", contentId.ToString(), StringComparison.Ordinal) .Replace("'", "\"", StringComparison.Ordinal) .Replace("`", "\"", StringComparison.Ordinal) .Replace("<FIELDS>", TestContent.AllFields, StringComparison.Ordinal); if (query.Contains("<DATA>", StringComparison.Ordinal)) { var data = TestContent.Input(content, TestSchemas.Ref1.Id, TestSchemas.Ref2.Id); var dataJson = JsonConvert.SerializeObject(data, Formatting.Indented); // Use Properties without quotes. dataJson = Regex.Replace(dataJson, "\"([^\"]+)\":", x => x.Groups[1].Value + ":"); // Use pure integer numbers. dataJson = dataJson.Replace(".0", string.Empty, StringComparison.Ordinal); // Use enum values whithout quotes. dataJson = dataJson.Replace("\"EnumA\"", "EnumA", StringComparison.Ordinal); dataJson = dataJson.Replace("\"EnumB\"", "EnumB", StringComparison.Ordinal); query = query.Replace("<DATA>", dataJson, StringComparison.Ordinal); } return(query); }
private static void CheckWorkflowExists(Workflows workflows, DomainId id) { if (!workflows.ContainsKey(id)) { throw new DomainObjectNotFoundException(id.ToString()); } }
public virtual async Task <IEnrichedContentEntity?> FindContentAsync(DomainId schemaId, DomainId id) { var content = cachedContents.GetOrDefault(id); if (content == null) { await maxRequests.WaitAsync(); try { content = await contentQuery.FindAsync(context, schemaId.ToString(), id); } finally { maxRequests.Release(); } if (content != null) { cachedContents[content.Id] = content; } } return(content); }
public DefaultAppImageStoreTests() { fileNameDefault = appId.ToString(); fileNameFolder = $"{appId}/thumbnail"; sut = new DefaultAppImageStore(assetStore, Options.Create(options)); }
public async Task DeleteAsync(DomainId id) { var job = state.Value.Jobs.FirstOrDefault(x => x.Id == id); if (job == null) { throw new DomainObjectNotFoundException(id.ToString()); } if (currentJob == job) { try { currentJobToken?.Cancel(); } catch (ObjectDisposedException) { return; } } else { await RemoveAsync(job); } }
public async Task <IIndex> AcquireAsync(DomainId ownerId) { IndexHolder?indexHolder; try { await lockObject.WaitAsync(); if (indices.TryGetValue(ownerId, out indexHolder)) { log.LogWarning(w => w .WriteProperty("message", "Unreleased index found.") .WriteProperty("ownerId", ownerId.ToString())); await CommitInternalAsync(indexHolder, true); } var directory = await indexStorage.CreateDirectoryAsync(ownerId); indexHolder = new IndexHolder(ownerId, directory); indices[ownerId] = indexHolder; } finally { lockObject.Release(); } return(indexHolder); }
public Task RunAsync(DomainId appId, DomainId ruleId, bool fromSnapshots = false, CancellationToken ct = default) { var grain = grainFactory.GetGrain <IRuleRunnerGrain>(appId.ToString()); return(grain.RunAsync(ruleId, fromSnapshots)); }
private void CheckPermissions(DomainId userId) { if (!string.Equals(userId.ToString(), User.OpenIdSubject())) { throw new DomainForbiddenException(T.Get("comments.noPermissions")); } }
public Task LogAsync(DomainId appId, RequestLog request, CancellationToken ct = default) { if (!requestLogStore.IsEnabled) { return(Task.CompletedTask); } var storedRequest = new Request { Key = appId.ToString(), Timestamp = request.Timestamp }; Append(storedRequest, FieldAuthClientId, request.UserClientId); Append(storedRequest, FieldAuthUserId, request.UserId); Append(storedRequest, FieldBytes, request.Bytes); Append(storedRequest, FieldCacheHits, request.CacheHits); Append(storedRequest, FieldCacheServer, request.CacheServer); Append(storedRequest, FieldCacheStatus, request.CacheStatus); Append(storedRequest, FieldCacheTTL, request.CacheTTL); Append(storedRequest, FieldCosts, request.Costs); Append(storedRequest, FieldRequestElapsedMs, request.ElapsedMs); Append(storedRequest, FieldRequestMethod, request.RequestMethod); Append(storedRequest, FieldRequestPath, request.RequestPath); Append(storedRequest, FieldStatusCode, request.StatusCode); return(requestLogStore.LogAsync(storedRequest, ct)); }
public async Task <(Instant Create, string Hash)> GetCurrentHashAsync(DomainId appId) { var entity = await Collection.Find(x => x.AppId == appId.ToString()).FirstOrDefaultAsync(); if (entity == null) { return(default, string.Empty);
public async Task <IBackupJob?> GetBackupAsync(DomainId appId, DomainId backupId) { var grain = grainFactory.GetGrain <IBackupGrain>(appId.ToString()); var state = await grain.GetStateAsync(); return(state.Value.Find(x => x.Id == backupId)); }
public async Task <List <IBackupJob> > GetBackupsAsync(DomainId appId) { var grain = grainFactory.GetGrain <IBackupGrain>(appId.ToString()); var state = await grain.GetStateAsync(); return(state.Value); }
private static string CreateQuery(string query, DomainId id = default) { return(query .Replace("'", "\"") .Replace("<ID>", id.ToString()) .Replace("<FIELDS_ASSET>", TestAsset.AllFields) .Replace("<FIELDS_CONTENT>", TestContent.AllFields)); }
private static string CreateQuery(string query, DomainId id = default) { return(query .Replace("'", "\"", StringComparison.Ordinal) .Replace("<ID>", id.ToString(), StringComparison.Ordinal) .Replace("<FIELDS_ASSET>", TestAsset.AllFields, StringComparison.Ordinal) .Replace("<FIELDS_CONTENT>", TestContent.AllFields, StringComparison.Ordinal) .Replace("<FIELDS_CONTENT_FLAT>", TestContent.AllFlatFields, StringComparison.Ordinal)); }
public async Task <ITextIndex> CreateAsync(DomainId schemaId) { var indexManager = new IndexManager(storage, A.Fake <ISemanticLog>()); grain = new LuceneTextIndexGrain(indexManager); await grain.ActivateAsync(schemaId.ToString()); return(new LuceneTextIndex(grainFactory, indexManager)); }
public static IFieldResolver Query(DomainId schemaId) { var schemaIdValue = schemaId.ToString(); return(new FuncFieldResolver <object?>(c => { var query = c.BuildODataQuery(); return ((GraphQLExecutionContext)c.UserContext).QueryContentsAsync(schemaIdValue, query); })); }
private async Task <ISchemaEntity?> GetSchemaCoreAsync(DomainId id, bool allowDeleted = false) { var schema = (await grainFactory.GetGrain <ISchemaGrain>(id.ToString()).GetStateAsync()).Value; if (schema.Version <= EtagVersion.Empty || (schema.IsDeleted && !allowDeleted)) { return(null); } return(schema); }
private async Task <IRuleEntity?> GetRuleCoreAsync(DomainId id) { var rule = (await grainFactory.GetGrain <IRuleGrain>(id.ToString()).GetStateAsync()).Value; if (rule.Version <= EtagVersion.Empty || rule.IsDeleted) { return(null); } return(rule); }
private async Task <IAppEntity?> GetAppCoreAsync(DomainId id) { var app = (await grainFactory.GetGrain <IAppGrain>(id.ToString()).GetStateAsync()).Value; if (app.Version <= EtagVersion.Empty || app.IsArchived) { return(null); } return(app); }