Exemplo n.º 1
0
        public void Execute(CancellationToken cancellationToken)
        {
            foreach (var table in ProcessedTables)
            {
                Logger.DebugFormat("Removing outdated records from table '{0}'...", table);

                int removedCount;

                do
                {
                    using (var storageConnection = (SqlServerConnection)_storage.GetConnection())
                    {
                        removedCount = storageConnection.Connection.Execute(
                            String.Format(@"
set transaction isolation level read committed;
delete top (@count) from HangFire.[{0}] with (tablock) where ExpireAt < @now;", table),
                            new { now = DateTime.UtcNow, count = NumberOfRecordsInSinglePass });
                    }

                    if (removedCount > 0)
                    {
                        Logger.Info(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.º 2
0
        static void Main(string[] args)
        {
            GlobalConfiguration.Configuration
            .UseSqlServerStorage(theConnection);

            var jobStorage = new Hangfire.SqlServer.SqlServerStorage(theConnection, new Hangfire.SqlServer.SqlServerStorageOptions
            {
                PrepareSchemaIfNecessary = false
            });

            //GlobalConfiguration.Configuration.UseAutofacActivator(provider);
            RecurringJobManager manager = new RecurringJobManager(jobStorage);

            List <scheduledReports> theReports = GetReports();

            //*********Code to clear out jobs if needed*********//
            using (var connection = jobStorage.GetConnection())
            {
                foreach (var recurringJob in StorageConnectionExtensions.GetRecurringJobs(connection))
                {
                    RecurringJob.RemoveIfExists(recurringJob.Id);
                }
            }
            int jobid = 0;

            foreach (scheduledReports sr in theReports)
            {
                RecurringJob.AddOrUpdate("reportjob_" + jobid.ToString(), () => sendReport(sr.apiCalls, sr.description, sr.emailRecipients), sr.cron);
                jobid++;
            }

            //foreach (scheduledReports sr in theReports)
            //{
            //    sendReport(sr.apiCalls, sr.description, sr.emailRecipients).Wait();

            //}

            GlobalConfiguration.Configuration.UseSqlServerStorage(theConnection);

            using (var server = new BackgroundJobServer())
            {
                Console.WriteLine("Hangfire Server started. Press any key to exit...");
                Console.ReadKey();
            }
        }
Exemplo n.º 3
0
        public void Execute(CancellationToken cancellationToken)
        {
            using (var storageConnection = (SqlServerConnection)_storage.GetConnection())
            {
                foreach (var table in ProcessedTables)
                {
                    Logger.DebugFormat("Removing outdated records from table '{0}'...", table);

                    storageConnection.Connection.Execute(
                        String.Format(@"
set transaction isolation level read committed;
delete from HangFire.[{0}] with (tablock) where ExpireAt < @now;", table),
                        new { now = DateTime.UtcNow });
                }
            }

            cancellationToken.WaitHandle.WaitOne(_checkInterval);
        }
Exemplo n.º 4
0
        public void Execute(CancellationToken cancellationToken)
        {
            Logger.DebugFormat("Aggregating records in 'Counter' table...");

            int removedCount;

            do
            {
                using (var storageConnection = (SqlServerConnection)_storage.GetConnection())
                {
                    removedCount = storageConnection.Connection.Execute(
                        GetAggregationQuery(),
                        new { now = DateTime.UtcNow, count = NumberOfRecordsInSinglePass });
                }

                if (removedCount >= NumberOfRecordsInSinglePass)
                {
                    cancellationToken.WaitHandle.WaitOne(DelayBetweenPasses);
                    cancellationToken.ThrowIfCancellationRequested();
                }
            } while (removedCount >= NumberOfRecordsInSinglePass);

            cancellationToken.WaitHandle.WaitOne(_interval);
        }