Skip to content

Commit

Permalink
added quartz and job for scheduled reports
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnetreport committed Mar 21, 2019
1 parent 522be71 commit 672e3ca
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 8 deletions.
4 changes: 3 additions & 1 deletion Global.asax.cs
@@ -1,4 +1,5 @@
using System;
using ReportBuilder.Web.Jobs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
Expand All @@ -12,6 +13,7 @@ public class MvcApplication : System.Web.HttpApplication
protected void Application_Start()
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
JobScheduler.Start();
}
}
}
94 changes: 94 additions & 0 deletions Jobs/DotNetReportJob.cs
@@ -0,0 +1,94 @@
using Newtonsoft.Json;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;

namespace ReportBuilder.Web.Jobs
{
public class ReportSchedule
{
public string Schedule { get; set; }
public string EmailTo { get; set; }
public DateTime? LastRun { get; set; }
public DateTime? NextRun { get; set; }
public string UserId { get; set; }
}
public class ReportWithSchedule
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string DataConnectName { get; set; }
public List<ReportSchedule> Schedules { get; set; }

}
public class JobScheduler
{
public static async void Start()
{
var schedulerFactory = new StdSchedulerFactory();
var scheduler = await schedulerFactory.GetScheduler();
await scheduler.Start();

IJobDetail job = JobBuilder.Create<DotNetReportJob>()
.WithIdentity("DotNetReportJob")
.StoreDurably()
.Build();

ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("DotNetReportJobTrigger")
.StartNow()
.WithSimpleSchedule(s => s.WithIntervalInSeconds(60).RepeatForever())
.Build();

await scheduler.ScheduleJob(job, trigger);

}
}

public class DotNetReportJob : IJob
{
async Task IJob.Execute(IJobExecutionContext context)
{
var apiUrl = ConfigurationManager.AppSettings["dotNetReport.apiUrl"];
var accountApiKey = ConfigurationManager.AppSettings["dotNetReport.accountApiToken"];
var databaseApiKey = ConfigurationManager.AppSettings["dotNetReport.dataconnectApiToken"];
var clientId = ""; // you can specify client id here if needed

// Get all reports with schedule and run the ones that are due
using (var client = new HttpClient())
{
var response = await client.GetAsync($"{apiUrl}/ReportApi/GetScheduledReports?account={accountApiKey}&dataConnect={databaseApiKey}&clientId={clientId}");

response.EnsureSuccessStatusCode();

var content = await response.Content.ReadAsStringAsync();
var reports = JsonConvert.DeserializeObject<List<ReportWithSchedule>>(content);

foreach(var report in reports)
{
foreach(var schedule in report.Schedules)
{
try
{
var chron = new CronExpression(schedule.Schedule);
var nextRun = chron.GetTimeAfter(DateTimeOffset.UtcNow);

schedule.NextRun = null;
}
catch(Exception ex)
{
schedule.NextRun = null;
}
}
}
}
}
}
}
7 changes: 6 additions & 1 deletion ReportBuilder.Web.csproj
Expand Up @@ -13,7 +13,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ReportBuilder.Web</RootNamespace>
<AssemblyName>ReportBuilder.Web</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<UseIISExpress>true</UseIISExpress>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
Expand Down Expand Up @@ -60,8 +60,12 @@
<HintPath>packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="Quartz, Version=3.0.7.0, Culture=neutral, PublicKeyToken=f6b8c98a402cc8a4, processorArchitecture=MSIL">
<HintPath>packages\Quartz.3.0.7\lib\net452\Quartz.dll</HintPath>
</Reference>
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Security" />
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
Expand Down Expand Up @@ -276,6 +280,7 @@
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Jobs\DotNetReportJob.cs" />
<Compile Include="Models\DotNetReportModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
6 changes: 0 additions & 6 deletions ReportBuilder.Web.sln
Expand Up @@ -12,8 +12,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{7ECE70
.nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReportBuilder.ScheduleJob", "..\ReportBuilder.ScheduleJob\ReportBuilder.ScheduleJob.csproj", "{10A52487-7E03-4A11-9D44-8DF7FF1AABE5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -24,10 +22,6 @@ Global
{AFDCDA4C-8F56-46E1-8E07-ABFC39537EDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFDCDA4C-8F56-46E1-8E07-ABFC39537EDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFDCDA4C-8F56-46E1-8E07-ABFC39537EDE}.Release|Any CPU.Build.0 = Release|Any CPU
{10A52487-7E03-4A11-9D44-8DF7FF1AABE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{10A52487-7E03-4A11-9D44-8DF7FF1AABE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{10A52487-7E03-4A11-9D44-8DF7FF1AABE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{10A52487-7E03-4A11-9D44-8DF7FF1AABE5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions packages.config
Expand Up @@ -17,6 +17,7 @@
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Modernizr" version="2.6.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net45" />
<package id="Quartz" version="3.0.7" targetFramework="net461" />
<package id="Select2.js" version="4.0.5" targetFramework="net45" />
<package id="toastr" version="2.1.1" targetFramework="net45" />
</packages>

0 comments on commit 672e3ca

Please sign in to comment.