Last active
August 31, 2023 14:35
-
-
Save alkampfergit/b46cb5e87fc9353f414fb00cd3eca981 to your computer and use it in GitHub Desktop.
Bug in hangfire Mongodb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Hangfire; | |
using Hangfire.MemoryStorage; | |
using Hangfire.Mongo; | |
using Hangfire.Mongo.Migration.Strategies; | |
using Hangfire.Mongo.Migration.Strategies.Backup; | |
namespace ConsoleApp4 | |
{ | |
internal class Program | |
{ | |
private static BackgroundJobClient _client; | |
private static string Id = Guid.NewGuid().ToString(); | |
private static string[] _queues = new[] { "alpha", "beta", "gamma" }; | |
private static long _counter = 0; | |
private static long _queueCounter = 0; | |
private static string GetQueue() | |
{ | |
_queueCounter = (_queueCounter + 1) % _queues.Length; | |
return _queues[_queueCounter]; | |
} | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("Hello, World!"); | |
GlobalConfiguration.Configuration | |
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180) | |
.UseColouredConsoleLogProvider() | |
.UseSimpleAssemblyNameTypeSerializer() | |
.UseRecommendedSerializerSettings() | |
.UseMongoStorage( | |
"mongodb://admin:123456##@mongo01.codewrecks.com/testhangfire?authSource=admin&compressors=snappy", | |
new MongoStorageOptions() | |
{ | |
MigrationOptions = new MongoMigrationOptions | |
{ | |
MigrationStrategy = new MigrateMongoMigrationStrategy(), | |
BackupStrategy = new CollectionMongoBackupStrategy() | |
}, | |
Prefix = "hangfire.mongo", | |
CheckConnection = true, | |
CheckQueuedJobsStrategy = CheckQueuedJobsStrategy.Poll, | |
QueuePollInterval = TimeSpan.FromSeconds(1) | |
}); | |
var options = new BackgroundJobServerOptions() | |
{ | |
SchedulePollingInterval = TimeSpan.FromSeconds(1), | |
Queues = _queues, | |
}; | |
using (var server = new BackgroundJobServer(options)) | |
{ | |
var newSchedulingQueue = GetQueue(); | |
BackgroundJob.Schedule(newSchedulingQueue, () => DoSomething(newSchedulingQueue, Id), TimeSpan.FromSeconds(1)); | |
Console.WriteLine("Press a key to exit"); | |
Console.ReadKey(); | |
} | |
} | |
public static void DoSomething(string queue, string id) | |
{ | |
if (id == Id) | |
{ | |
var newSchedulingQueue = GetQueue(); | |
Interlocked.Increment(ref _counter); | |
Console.WriteLine("Cnt: {0:0000} Queued on {1} - timestampe {2}", _counter, queue, DateTime.UtcNow); | |
BackgroundJob.Schedule(newSchedulingQueue, () => DoSomething(newSchedulingQueue,Id), TimeSpan.FromSeconds(_counter)); | |
} | |
else | |
{ | |
Console.WriteLine("OLDJOB - Cnt: {0:0000} - {1}", _counter, DateTime.UtcNow); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment