Skip to content

Commit

Permalink
Run all cron jobs in the same process if no argument provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel15 committed Nov 23, 2015
1 parent 8c93ccb commit 9d2a754
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions Daniel15.Cron/CronRunner.cs
Expand Up @@ -9,46 +9,53 @@ namespace Daniel15.Cron
{
public class Program
{
private readonly IApplicationEnvironment _appEnv;
private readonly IServiceCollection _serviceCollection = new ServiceCollection();
private IServiceProvider _serviceProvider;

public Program(IApplicationEnvironment appEnv)
{
_appEnv = appEnv;
}

public void Main(string[] args)
{
_serviceCollection.AddDaniel15();
var builder = new ConfigurationBuilder()
// This is extremely ugly, but the paths differ in dev vs in prod.
// Need to figure out a nicer way of doing this.
.AddJsonFile("..\\Daniel15.Web\\config.json", optional: true)
.AddJsonFile("..\\Daniel15.Web\\config.Development.json", optional: true)
.AddJsonFile("../../../../../../site/approot/packages/Daniel15.Web/1.0.0/root/config.Production.json", optional: true)
.AddEnvironmentVariables();
_serviceCollection.AddDaniel15Config(builder.Build());
_serviceCollection.AddOptions();
_serviceProvider = _serviceCollection.BuildServiceProvider();

if (args.Length == 0)
{
// No argument, so run everything
RunProjects();
RunSocial();
RunDisqus();
return;
}

var operation = args[0];
switch (operation)
{
case "-disqus":
_serviceProvider.GetRequiredService<IDisqusComments>().Sync();
RunDisqus();
break;

case "-social":
ActivatorUtilities.CreateInstance<SocialShareUpdater>(_serviceProvider).Run();
RunSocial();
break;

case "-projects":
ActivatorUtilities.CreateInstance<ProjectUpdater>(_serviceProvider).Run();
RunProjects();
break;

default:
throw new Exception("Invalid operation '" + operation + "'");
}
}

private void RunDisqus() => _serviceProvider.GetRequiredService<IDisqusComments>().Sync();
private void RunSocial() => ActivatorUtilities.CreateInstance<SocialShareUpdater>(_serviceProvider).Run();
private void RunProjects() => ActivatorUtilities.CreateInstance<ProjectUpdater>(_serviceProvider).Run();
}
}

0 comments on commit 9d2a754

Please sign in to comment.