1
0
mirror of https://github.com/bitwarden/server synced 2026-01-08 11:33:26 +00:00

run jobs crons on API image

This commit is contained in:
Kyle Spearrin
2017-08-17 16:45:27 -04:00
parent d15917d3c9
commit fd35ac45b2
9 changed files with 81 additions and 31 deletions

View File

@@ -8,6 +8,12 @@
<UserSecretsId>bitwarden-Jobs</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<Content Include="crontab">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />

View File

@@ -1,6 +1,9 @@
using Bit.Core.Services;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
@@ -9,28 +12,31 @@ namespace Bit.Jobs
public class Program
{
private static ILicensingService _licensingService;
private static ILogger<Program> _logger;
public static void Main(string[] args)
{
var parameters = ParseParameters(args);
var host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseContentRoot(parameters.ContainsKey("d") ? parameters["d"] : Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServer(new NoopServer())
.Build();
_logger = host.Services.GetRequiredService<ILogger<Program>>();
_licensingService = host.Services.GetRequiredService<ILicensingService>();
MainAsync(args).Wait();
MainAsync(parameters).Wait();
}
private async static Task MainAsync(string[] args)
private async static Task MainAsync(IDictionary<string, string> parameters)
{
if(args.Length == 0)
if(!parameters.ContainsKey("j"))
{
return;
}
switch(args[0])
switch(parameters["j"])
{
case "validate-licenses":
await _licensingService.ValidateOrganizationsAsync();
@@ -38,9 +44,28 @@ namespace Bit.Jobs
case "refresh-licenses":
// TODO
break;
case "hello":
_logger.LogInformation("Hello World!");
break;
default:
break;
}
}
private static IDictionary<string, string> ParseParameters(string[] args)
{
var dict = new Dictionary<string, string>();
for(var i = 0; i < args.Length; i = i + 2)
{
if(!args[i].StartsWith("-"))
{
continue;
}
dict.Add(args[i].Substring(1), args[i + 1]);
}
return dict;
}
}
}

View File

@@ -6,6 +6,7 @@ using Bit.Core;
using Bit.Core.Utilities;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Builder;
using Serilog.Events;
namespace Bit.Jobs
{
@@ -55,7 +56,10 @@ namespace Bit.Jobs
GlobalSettings globalSettings)
{
loggerFactory
.AddSerilog(env, appLifetime, globalSettings)
.AddSerilog(env, appLifetime, globalSettings, (e) =>
{
return e.Level >= LogEventLevel.Information;
})
.AddConsole()
.AddDebug();
}

2
src/Jobs/crontab Normal file
View File

@@ -0,0 +1,2 @@
* * * * * root dotnet /jobs/Jobs.dll -d /jobs -j hello >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.