private static HangfireContext CreateContext(DbContextOptionsBuilder <HangfireContext> builder) { var context = new HangfireContext(builder.Options, string.Empty); context.Database.EnsureCreated(); return(context); }
private static void AddJobWithStateToContext( HangfireContext context, string stateName, IDictionary <string, string> data = null) { data ??= new Dictionary <string, string>(); var state = new HangfireState { CreatedAt = DateTime.UtcNow, Name = stateName, Data = SerializationHelper.Serialize(data), }; var job = new HangfireJob { CreatedAt = DateTime.UtcNow, InvocationData = CreateInvocationData(() => SampleMethod(null)), States = new[] { state, }, }; context.Add(job); context.SaveChanges(); job.State = state; job.StateName = state.Name; context.SaveChanges(); }
public static void ConfigureHangfire(IAppBuilder app) { var db = new HangfireContext(); GlobalConfiguration.Configuration.UseSqlServerStorage(db.Database.Connection.ConnectionString); // Copy connection string app.UseHangfireDashboard(); app.UseHangfireServer(); }
public void HangfireContext_Verbose_Expect_True() { var mockArgs = new Mock <IArgs>(); mockArgs.Setup(o => o.ShowConsole).Returns(true); var hangfireContext = new HangfireContext(mockArgs.Object); Assert.IsTrue(hangfireContext.Verbose); }
public void HangfireServer_Program_ConnectionString_Exists() { var args = new Args { ShowConsole = true, }; var implConfig = SetupHangfireImplementationConfigs(out var mockWriter, out var mockPauseHelper, out var mockExecutionLogPublisher); var dbSource = new DbSource { ConnectionString = "connectionString", }; var compressedExecuteMessage = new CompressedExecuteMessage(); var serializeToJsonString = dbSource.SerializeToJsonString(new DefaultSerializationBinder()); compressedExecuteMessage.SetMessage(serializeToJsonString); var mockSerializer = new Mock <IBuilderSerializer>(); var mockFile = new Mock <IFile>(); var mockDirectory = new Mock <IDirectory>(); var persistenceSettings = new PersistenceSettings("some path", mockFile.Object, mockDirectory.Object) { Enable = false, PersistenceDataSource = new NamedGuidWithEncryptedPayload { Name = "Data Source", Value = Guid.Empty, Payload = serializeToJsonString }, }; mockSerializer.Setup(o => o.Deserialize <DbSource>(persistenceSettings.PersistenceDataSource.Payload)).Returns(dbSource).Verifiable(); var mockContext = new HangfireContext(args); var mockEventWaitHandle = new Mock <IEventWaitHandle>(); mockEventWaitHandle.Setup(o => o.New()).Verifiable(); mockEventWaitHandle.Setup(o => o.WaitOne()).Verifiable(); var item = new Implementation(mockContext, implConfig, persistenceSettings, mockSerializer.Object, mockEventWaitHandle.Object); item.Run(); mockWriter.Verify(o => o.WriteLine("Starting Hangfire server..."), Times.Once); mockExecutionLogPublisher.Verify(o => o.Info("Starting Hangfire server..."), Times.Once); mockSerializer.Verify(o => o.Deserialize <DbSource>(persistenceSettings.PersistenceDataSource.Payload), Times.Once); mockWriter.Verify(o => o.WriteLine("Hangfire dashboard started..."), Times.Once); mockExecutionLogPublisher.Verify(o => o.Info("Hangfire dashboard started..."), Times.Once); mockWriter.Verify(o => o.WriteLine("Hangfire server started..."), Times.Once); mockExecutionLogPublisher.Verify(o => o.Info("Hangfire server started..."), Times.Once); mockPauseHelper.Verify(o => o.Pause(), Times.Never); mockEventWaitHandle.Verify(o => o.New(), Times.Never); mockEventWaitHandle.Verify(o => o.WaitOne(), Times.Never); }
public static void AddJobProcessing(this IServiceCollection services, ConnectionStrings connectionStrings) { services.AddHangfireServer(options => { options.WorkerCount = Math.Max(Environment.ProcessorCount - 1, 2); options.Queues = JobQueueNames.All; }); HangfireContext.EnsureCreated(connectionStrings.HangfirePgsql); var hangfireConfiguration = ConfigureHangfire(connectionStrings); services.AddHangfire(hangfireConfiguration); }
private static void AddJobWithQueueItemToContext(HangfireContext context, string queue) { var job = new HangfireJob { CreatedAt = DateTime.UtcNow, InvocationData = CreateInvocationData(() => SampleMethod(null)), }; var queueItem = new HangfireQueuedJob { Job = job, Queue = queue, }; context.Add(job); context.Add(queueItem); }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { AddOptions <EmailOptions>(services); AddOptions <SessionOptions>(services); AddOptions <FileStorageOptions>(services); FFMpegOptions.Configure(new FFMpegOptions { RootDirectory = "" }); services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo { Title = "Memester API", Version = "v1" })); HangfireContext.EnsureCreated(_configuration.GetConnectionString("HangfirePgsql")); ConfigureHangfire(GlobalConfiguration.Configuration); services.AddHangfire(ConfigureHangfire); services.AddHangfireServer(options => { options.WorkerCount = Math.Min(Environment.ProcessorCount * 2 - 1, 16); options.Queues = JobQueues.All; }); services.AddMvc().AddJsonOptions(options => ConfigureJsonSerializer(options.JsonSerializerOptions)); services.AddDbContext <DatabaseContext>(options => options.UseNpgsql(_configuration.GetConnectionString("Pgsql"), b => b.MigrationsAssembly(typeof(DatabaseContext).Assembly.FullName))); services.AddScoped(typeof(OperationContext), _ => new OperationContext()); services.AddScoped(typeof(IHttpSessionService), typeof(CookieSessionService)); services.AddScoped(typeof(ScrapingService)); services.AddScoped(typeof(IndexingService)); services.AddScoped(typeof(FileStorageService)); services.AddScoped(typeof(AuthenticationService)); services.AddSingleton(typeof(IEmailService), typeof(MailkitEmailService)); services.AddSingleton(typeof(SessionService)); services.AddSingleton(typeof(Random)); services.AddTransient <IAsyncInitialized, FileStorageService>(); // services.AddTransient<IAsyncInitialized, DatabaseContext>(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, HangfireContext context) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseHangfireServer(); app.UseHangfireDashboard(); Jobs.Configure(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public void Setup() { _connectionHangfire = new SqliteConnection("DataSource=:memory:"); _connectionHangfire.Open(); var options = new DbContextOptionsBuilder <HangfireContext>() .UseSqlite(_connectionHangfire) .Options; _hangfireContext = new HangfireContext(options); _connectionEventStore = new SqliteConnection("DataSource=:memory:"); _connectionEventStore.Open(); var optionsEventStore = new DbContextOptionsBuilder <EventStoreContext>() .UseSqlite(_connectionEventStore) .Options; _eventStoreContext = new EventStoreContext(optionsEventStore); _hangfireContext.Database.EnsureCreated(); _eventStoreContext.Database.EnsureCreated(); }
public void HangfireServer_Program_ConnectionString_Payload_Empty_WaitOne() { var args = new Args { ShowConsole = false, }; var implConfig = SetupHangfireImplementationConfigs(out var mockWriter, out var mockPauseHelper, out var mockExecutionLogPublisher); var mockFile = new Mock <IFile>(); var mockDirectory = new Mock <IDirectory>(); var persistenceSettings = new PersistenceSettings("", mockFile.Object, mockDirectory.Object); var mockContext = new HangfireContext(args); var mockEventWaitHandle = new Mock <IEventWaitHandle>(); mockEventWaitHandle.Setup(o => o.New()).Verifiable(); mockEventWaitHandle.Setup(o => o.WaitOne()).Verifiable(); var item = new Implementation(mockContext, implConfig, persistenceSettings, new Mock <IBuilderSerializer>().Object, mockEventWaitHandle.Object); item.Run(); item.WaitForExit(); mockWriter.Verify(o => o.WriteLine("Starting Hangfire server..."), Times.Once); mockExecutionLogPublisher.Verify(o => o.Info("Starting Hangfire server..."), Times.Once); mockWriter.Verify(o => o.WriteLine("Fatal Error: Could not find persistence config file. Hangfire server is unable to start."), Times.Once); mockExecutionLogPublisher.Verify(o => o.Error("Fatal Error: Could not find persistence config file. Hangfire server is unable to start."), Times.Once); mockWriter.Verify(o => o.Write("Press any key to exit..."), Times.Once); mockPauseHelper.Verify(o => o.Pause(), Times.Never); mockWriter.Verify(o => o.WriteLine("Hangfire dashboard started..."), Times.Never); mockExecutionLogPublisher.Verify(o => o.Info("Hangfire dashboard started..."), Times.Never); mockWriter.Verify(o => o.WriteLine("Hangfire server started..."), Times.Never); mockExecutionLogPublisher.Verify(o => o.Info("Hangfire server started..."), Times.Never); mockEventWaitHandle.Verify(o => o.New(), Times.Never); mockEventWaitHandle.Verify(o => o.WaitOne(), Times.Once); }
public void Ctor_CreatesInstance(string schema) { var builder = new DbContextOptionsBuilder <HangfireContext>(); OptionsAction(builder); DbContextOptions options = builder.Options; using var context = new HangfireContext(options, schema); Assert.Same(schema, context.Schema); var model = context.Model; Assert.NotNull(model); var actualSchema = context.Model #if NETCOREAPP3_1 || NET5_0 .GetDefaultSchema() #else .Relational() .DefaultSchema #endif ?? string.Empty; Assert.Equal(schema, actualSchema); }