public Validator(IDatabaseConnector <JobDatabaseCredentials> database) { _database = database; RuleFor(x => x.PeerType).IsInEnum(); RuleFor(x => x).CustomAsync(async(command, context, token) => { int queuedJobs = await _database.ExecuteAsync(conn => { return(conn.ExecuteScalarAsync <int>($"SELECT COUNT(1) FROM {JobMap.Table} WHERE {JobMap.UserId} = @{JobMap.UserId} AND {JobMap.State} = {(int)JobState.Queued}", new { command.UserId })); }); if (queuedJobs >= 5) { context.AddFailure("command", "Unable to queue another job"); } bool isAlreadyProccessed = await _database.ExecuteAsync(conn => { return(conn.ExecuteScalarAsync <bool>($@" SELECT 1 FROM {JobMap.Table} WHERE {JobMap.UserId} = @{JobMap.UserId} AND {JobMap.PeerId} = @{JobMap.PeerId} AND {JobMap.PeerType} = @{JobMap.PeerType} AND {JobMap.State} IN ( {(int)JobState.Queued}, {(int)JobState.Fetching} )", new { command.UserId, command.PeerId, command.PeerType })); }); if (isAlreadyProccessed) { context.AddFailure("command", "This peer is in a queue already or is beeing proccessed"); } }); }
public async Task <PaginatedResults <Result> > HandleAsync(Query query) { GridReader reader = await _database.ExecuteAsync(conn => { return(conn.QueryMultipleAsync( $"SELECT COUNT(1) FROM {JobMap.Table} " + $"WHERE {JobMap.UserId} = @{JobMap.UserId}; " + $"SELECT * FROM {JobMap.Table} " + $"WHERE {JobMap.UserId} = @{JobMap.UserId} " + $"ORDER BY {JobMap.Id} DESC {query.PaginationParams.ToSql()}", new { query.UserId })); }); int total = await reader.ReadFirstAsync <int>(); IEnumerable <Job> jobs = await reader.ReadAsync <Job>(); IEnumerable <Result> result = jobs.Select(job => new Result(job)); result.Where(r => r.State == JobState.Fetching).ForEach(r => { RedisValue value = _cache.StringGet($"Messages.{nameof(Job)}.{nameof(Job.Id)}.{nameof(JobState.Fetching)}"); r.Message = value.HasValue ? value.ToString() : "Processing"; }); return(new PaginatedResults <Result>(total, query.PaginationParams, null, result)); }
public async Task ExecuteAsync(Command command) { await _database.ExecuteAsync(conn => { return(conn.InsertAsync(new Job() { PeerId = command.PeerId, PeerType = command.PeerType, UserId = command.UserId, State = JobState.Queued, CreateDateUtc = _clock.UtcNow() })); }); }
public async Task Save(byte[] session) { await _database.ExecuteAsync(conn => { return(conn.ExecuteAsync($@" IF NOT EXISTS ( SELECT 1 FROM {TelegramSessionTable} WHERE {UserId} = @{UserId}) BEGIN INSERT INTO {TelegramSessionTable} ({UserId},{Session}) VALUES (@{UserId},@{Session}) END ELSE BEGIN UPDATE {TelegramSessionTable} SET {Session} = @{Session} WHERE {UserId} = @{UserId} END; ", new { UserId = _userId, Session = session })); }); }