public void RemoveFromQueue()
 {
     _storage.UseConnection(connection =>
     {
         connection.Execute(string.Format(@"
             delete from [{0}.JobQueue] where Id = @id", _storage.GetSchemaName()),
                            new { id = Id });
     }, true);
 }
Exemplo n.º 2
0
        public void Execute(CancellationToken cancellationToken)
        {
            foreach (var table in ProcessedTables)
            {
                Logger.DebugFormat("Removing outdated records from table '{0}'...", table);

                int removedCount = 0;

                do
                {
                    _storage.UseConnection(connection =>
                    {
                        removedCount = connection.Execute(
                            String.Format(@"
                                delete from [{0}.{1}] where Id in (
                                    select Id from [{0}.{1}]
                                    where ExpireAt < @expireAt
                                    limit @limit)", _storage.GetSchemaName(), table),
                            new { limit = NumberOfRecordsInSinglePass, expireAt = DateTime.UtcNow });
                    }, true);

                    if (removedCount > 0)
                    {
                        Logger.Trace(String.Format("Removed {0} outdated record(s) from '{1}' table.", removedCount, table));

                        cancellationToken.WaitHandle.WaitOne(DelayBetweenPasses);
                        cancellationToken.ThrowIfCancellationRequested();
                    }
                } while (removedCount != 0);
            }

            cancellationToken.WaitHandle.WaitOne(_checkInterval);
        }
Exemplo n.º 3
0
        public IList <ServerDto> Servers()
        {
            return(UseConnection <IList <ServerDto> >(connection =>
            {
                var servers = connection.Query <Entities.Server>(
                    string.Format(@"select * from [{0}.Server]", _storage.GetSchemaName()))
                              .ToList();

                var result = new List <ServerDto>();

                foreach (var server in servers)
                {
                    var data = JobHelper.FromJson <Entities.ServerData>(server.Data);
                    result.Add(new ServerDto
                    {
                        Name = server.Id,
                        Heartbeat = server.LastHeartbeat,
                        Queues = data.Queues,
                        StartedAt = data.StartedAt.HasValue ? data.StartedAt.Value : DateTime.MinValue,
                        WorkersCount = data.WorkerCount
                    });
                }

                return result;
            }));
        }
 public override void ExpireJob(string jobId, TimeSpan expireIn)
 {
     QueueCommand(x => x.Execute(
                      string.Format(@"update [{0}.Job] set ExpireAt = @expireAt where Id = @id", _storage.GetSchemaName()),
                      new { expireAt = DateTime.UtcNow.Add(expireIn), id = jobId }));
 }
Exemplo n.º 5
0
        public override string CreateExpiredJob(
            Job job,
            IDictionary <string, string> parameters,
            DateTime createdAt,
            TimeSpan expireIn)
        {
            if (job == null)
            {
                throw new ArgumentNullException("job");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            string createJobSql = string.Format(@"
insert into [{0}.Job] (InvocationData, Arguments, CreatedAt, ExpireAt)
values (@invocationData, @arguments, @createdAt, @expireAt);
SELECT last_insert_rowid()", _storage.GetSchemaName());

            var invocationData = InvocationData.Serialize(job);

            return(_storage.UseConnection(connection =>
            {
                var jobId = connection.Query <int>(
                    createJobSql,
                    new
                {
                    invocationData = JobHelper.ToJson(invocationData),
                    arguments = invocationData.Arguments,
                    createdAt = createdAt,
                    expireAt = createdAt.Add(expireIn)
                }).Single().ToString();

                if (parameters.Count > 0)
                {
                    var parameterArray = new object[parameters.Count];
                    int parameterIndex = 0;
                    foreach (var parameter in parameters)
                    {
                        parameterArray[parameterIndex++] = new
                        {
                            jobId = jobId,
                            name = parameter.Key,
                            value = parameter.Value
                        };
                    }

                    string insertParameterSql = string.Format(@"
insert into [{0}.JobParameter] (JobId, Name, Value)
values (@jobId, @name, @value)", _storage.GetSchemaName());

                    connection.Execute(insertParameterSql, parameterArray);
                }

                return jobId;
            }, true));
        }