/// <summary>
        /// Creates SQLite distributed lock
        /// </summary>
        /// <param name="resource">Lock resource</param>
        /// <param name="timeout">Lock timeout</param>
        /// <param name="database">Lock database</param>
        /// <param name="storageOptions">Database options</param>
        /// <exception cref="DistributedLockTimeoutException">Thrown if lock is not acuired within the timeout</exception>
        public SQLiteDistributedLock(string resource, TimeSpan timeout, HangfireDbContext database,
                                     SQLiteStorageOptions storageOptions)
        {
            _resource       = resource ?? throw new ArgumentNullException(nameof(resource));
            _dbContext      = database ?? throw new ArgumentNullException(nameof(database));
            _storageOptions = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions));

            if (string.IsNullOrEmpty(resource))
            {
                throw new ArgumentException($@"The {nameof(resource)} cannot be empty", nameof(resource));
            }
            if (timeout.TotalSeconds > int.MaxValue)
            {
                throw new ArgumentException($"The timeout specified is too large. Please supply a timeout equal to or less than {int.MaxValue} seconds", nameof(timeout));
            }

            if (!AcquiredLocks.Value.ContainsKey(_resource) || AcquiredLocks.Value[_resource] == 0)
            {
                Cleanup();
                Acquire(timeout);
                AcquiredLocks.Value[_resource] = 1;
                StartHeartBeat();
            }
            else
            {
                AcquiredLocks.Value[_resource]++;
            }
        }
 #pragma warning disable 1591
 public HangfireSQLiteConnection(
     HangfireDbContext database,
     SQLiteStorageOptions storageOptions,
     PersistentJobQueueProviderCollection queueProviders)
 {
     DbContext       = database ?? throw new ArgumentNullException(nameof(database));
     _storageOptions = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions));
     _queueProviders = queueProviders ?? throw new ArgumentNullException(nameof(queueProviders));
 }
        /// <summary>
        /// Constructs Job Storage by database connection string and options
        /// </summary>
        /// <param name="connectionString">LiteDB connection string</param>
        /// <param name="storageOptions">Storage options</param>
        public SQLiteStorage(string connectionString, SQLiteStorageOptions storageOptions)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentNullException(nameof(connectionString));
            }

            _connectionString = connectionString;
            _storageOptions   = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions));

            Connection = HangfireDbContext.Instance(connectionString, storageOptions.Prefix);
            Connection.Init(_storageOptions);

            var defaultQueueProvider = new SQLiteJobQueueProvider(_storageOptions);

            QueueProviders = new PersistentJobQueueProviderCollection(defaultQueueProvider);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="nameOrConnectionString"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IGlobalConfiguration <SQLiteStorage> UseSQLiteStorage(
            [NotNull] this IGlobalConfiguration configuration,
            [NotNull] string nameOrConnectionString,
            SQLiteStorageOptions options = null)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            if (nameOrConnectionString == null)
            {
                throw new ArgumentNullException(nameof(nameOrConnectionString));
            }
            if (options == null)
            {
                options = new SQLiteStorageOptions();
            }

            var storage = new SQLiteStorage(nameOrConnectionString, options);

            return(configuration.UseStorage(storage));
        }
        /// <summary>
        /// Initializes initial tables schema for Hangfire
        /// </summary>
        public void Init(SQLiteStorageOptions storageOptions)
        {
            StorageOptions = storageOptions;

            try
            {
                Database.Execute($@"PRAGMA auto_vacuum = '{(int)storageOptions.AutoVacuumSelected}'");
            }
            catch (Exception ex)
            {
                Logger.Log(LogLevel.Error, () => $"Error set auto vacuum mode. Details: {ex.ToString()}");
            }

            Database.CreateTable <AggregatedCounter>();
            Database.CreateTable <Counter>();
            Database.CreateTable <HangfireJob>();
            Database.CreateTable <HangfireList>();
            Database.CreateTable <Hash>();
            Database.CreateTable <JobParameter>();
            Database.CreateTable <JobQueue>();
            Database.CreateTable <HangfireServer>();
            Database.CreateTable <Set>();
            Database.CreateTable <State>();
            Database.CreateTable <DistributedLock>();

            AggregatedCounterRepository = Database.Table <AggregatedCounter>();
            CounterRepository           = Database.Table <Counter>();
            HangfireJobRepository       = Database.Table <HangfireJob>();
            HangfireListRepository      = Database.Table <HangfireList>();
            HashRepository            = Database.Table <Hash>();
            JobParameterRepository    = Database.Table <JobParameter>();
            JobQueueRepository        = Database.Table <JobQueue>();
            HangfireServerRepository  = Database.Table <HangfireServer>();
            SetRepository             = Database.Table <Set>();
            StateRepository           = Database.Table <State>();
            DistributedLockRepository = Database.Table <DistributedLock>();
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="storageOptions"></param>
 /// <exception cref="ArgumentNullException"></exception>
 public SQLiteJobQueueProvider(SQLiteStorageOptions storageOptions)
 {
     _storageOptions = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions));
 }
示例#7
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="connection"></param>
 /// <param name="storageOptions"></param>
 public SQLiteJobQueue(HangfireDbContext connection, SQLiteStorageOptions storageOptions)
 {
     _storageOptions = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions));
     _dbContext      = connection ?? throw new ArgumentNullException(nameof(connection));
 }