getNextFireTime of my existing job - quartz-scheduler

I tried Quartz.com documentation & googled for couple for hours...but could not find single good article on how to get Next Job (which is supposr to fire in future).
I am using CronTrigger Expression to Schedule jobs, using VB.net (winforms). jobs works fine...however I would like my users to see when will it FIRE next. I am storing CronExpression in my database, Can I use that Expression to show next Fire Date/Time to my end users? or if there is any other way possible please advise (with a simple samply).
Thanks
Edit
Following Code Returns next job will fire after 12 minutes instead of 20 minute
Dim exp As CronExpression = New CronExpression("0 0/20 * * * ?")
Dim nextFire As String = exp.GetNextValidTimeAfter(DateTime.Now)
MsgBox(nextFire)

You can create a new JobKey
JobKey jobKey = new JobKey(jobName, groupName);
and use the key to fetch the job detail:
var detail = scheduler.GetJobDetail(jobKey);
this is a simple function which does what you're looking for:
private DateTime getNextFireTimeForJob(IScheduler scheduler, string jobName, string groupName = "")
{
JobKey jobKey = new JobKey(jobName, groupName);
DateTime nextFireTime = DateTime.MinValue;
bool isJobExisting = Scheduler.CheckExists(jobKey);
if (isJobExisting)
{
var detail = scheduler.GetJobDetail(jobKey);
var triggers = scheduler.GetTriggersOfJob(jobKey);
if (triggers.Count > 0)
{
var nextFireTimeUtc = triggers[0].GetNextFireTimeUtc();
nextFireTime = TimeZone.CurrentTimeZone.ToLocalTime(nextFireTimeUtc.Value.DateTime);
}
}
return (nextFireTime);
}
It works only if you have one trigger per job.
If there's more than one trigger in your job you can loop through them:
foreach (ITrigger trigger in triggers)
{
Console.WriteLine(jobKey.Name);
Console.WriteLine(detail.Description);
Console.WriteLine(trigger.Key.Name);
Console.WriteLine(trigger.Key.Group);
Console.WriteLine(trigger.GetType().Name);
Console.WriteLine(scheduler.GetTriggerState(trigger.Key));
DateTimeOffset? nextFireTime = trigger.GetNextFireTimeUtc();
if (nextFireTime.HasValue)
{
Console.WriteLine(TimeZone.CurrentTimeZone.ToLocalTime(nextFireTime.Value.DateTime).ToString());
}
}
or using Linq (System.Linq) :
var myTrigger = triggers.Where(f => f.Key.Name == "[trigger name]").SingleOrDefault();

If you already know the cronExpression then you can call GetNextValidTimeAfter , eg
CronExpression exp = new CronExpression("0 0 0/1 1/1 * ? *");
var nextFire = exp.GetNextValidTimeAfter(DateTime.Now);
Console.WriteLine(nextFire);
and if you want more fire times, then
for(int i=0 ; i< 9; i++)
{
if (nextFire.HasValue)
{
nextFire = exp.GetNextValidTimeAfter(nextFire.Value);
Console.WriteLine(nextFire);
}
}
If you are looking for a more general way of showing next fire times for existing jobs, then check out the answer to this question which works even if you aren't using cron expressions. This is for Quartz Version 2
The Quartz Version 1 way of getting job and trigger information is something like the method below.
public void GetListOfJobs(IScheduler scheduler)
{
var query =
(from groupName in scheduler.JobGroupNames
from jobName in scheduler.GetJobNames(groupName)
let triggers = scheduler.GetTriggersOfJob(jobName, groupName)
select new
{
groupName,
jobName,
triggerInfo = (from trigger in triggers select trigger)
}
);
foreach (var r in query)
{
if (r.triggerInfo.Count() == 0)
{
var details = String.Format("No triggers found for : Group {0} Job {1}", r.groupName, r.jobName);
Console.WriteLine(details);
}
foreach (var t in r.triggerInfo)
{
var details = String.Format("{0,-50} {9} Next Due {1,30:r} Last Run {2,30:r} Group {3,-30}, Trigger {4,-50} {5,-50} Scheduled from {6:r} Until {7,30:r} {8,30:r} ",
t.JobName,
t.GetNextFireTimeUtc().ToLocalTime(),
t.GetPreviousFireTimeUtc().ToLocalTime(),
t.JobGroup,
t.Name,
t.Description,
t.StartTimeUtc.ToLocalTime(),
t.EndTimeUtc.ToLocalTime(),
t.FinalFireTimeUtc.ToLocalTime(),
((t.GetNextFireTimeUtc() > DateTime.UtcNow) ? "Active " : "InActive")
);
Console.WriteLine(details);
}
}
}

I had problem where i am getting next execution time even for the date which are past the end time.
Following is my implementation which eventually returning the correct result in Quartz 3.0.4
internal static DateTime? GetNextFireTimeForJob(IJobExecutionContext context)
{
JobKey jobKey = context.JobDetail.Key;
DateTime? nextFireTime = null;
var isJobExisting = MyQuartzScheduler.CheckExists(jobKey);
if (isJobExisting.Result)
{
var triggers = MyQuartzScheduler.GetTriggersOfJob(jobKey);
if (triggers.Result.Count > 0)
{
var nextFireTimeUtc = triggers.Result.First().GetNextFireTimeUtc();
if (nextFireTimeUtc.HasValue)
{
nextFireTime = TimeZone.CurrentTimeZone.ToLocalTime(nextFireTimeUtc.Value.DateTime);
}
}
}
return nextFireTime;
}

Related

How to stop NUnit ITestRunner?

Using nunit.engine 3.10.0, I can't stop an asynchronously running ITestRunner. The TestPackage is set up to be executed locally, i.e. InProcess and in the current AppDomain. No more tests are started after the second test as expected, but the while loop never ends.
public static void Main(string[] args)
{
// 2 assemblies x 2 TestFixtures each x 2 Tests each = 8 test cases
string[] testAssemblyFileNames = { TestAssemblyFileName1, TestAssemblyFileName2 };
string assemblyDirectory = Path.GetDirectoryName(Uri.UnescapeDataString(
new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path));
// Nunit 3.10.0
var minVersion = new Version("3.4");
ITestEngine testEngine = TestEngineActivator.CreateInstance(minVersion);
// configure a test package that executes
// in the current process and in the current domain
var testPackage = new TestPackage(testAssemblyFileNames);
testPackage.AddSetting(EnginePackageSettings.ProcessModel, "InProcess");
testPackage.AddSetting(EnginePackageSettings.DomainUsage, "None");
testPackage.AddSetting(EnginePackageSettings.DisposeRunners, "True");
testPackage.AddSetting(EnginePackageSettings.WorkDirectory, assemblyDirectory);
ITestRunner testRunner = testEngine.GetRunner(testPackage);
// prepare a listener that stops the test runner
// when the second test has been started
const bool StopAfterSecondTest = true;
int testStartedCount = 0;
var listener = new MyTestEventListener();
listener.TestStarted += (sender, eventArgs) =>
{
testStartedCount++;
if ( StopAfterSecondTest && testStartedCount == 2 )
{
testRunner.StopRun(force: true);
}
};
var testFilterBuilder = new TestFilterBuilder();
TestFilter testFilter = testFilterBuilder.GetFilter();
ITestRun testRun = testRunner.RunAsync(listener, testFilter);
bool keepRunning;
int loopCount = 0;
do
{
bool completed = testRun.Wait(500);
bool running = testRunner.IsTestRunning;
keepRunning = !completed && running;
loopCount++;
} while ( keepRunning );
Console.WriteLine($"Loop count: {loopCount}");
XmlNode resultNode = testRun.Result;
Console.WriteLine(resultNode.InnerText);
Console.ReadKey();
}
private class MyTestEventListener : ITestEventListener
{
private const string TestCaseStartPrefix = "<start-test";
private const string TestMethodTypeAttribute = " type=\"TestMethod\"";
public event EventHandler<EventArgs> TestStarted;
public void OnTestEvent(string report)
{
if ( report.StartsWith(TestCaseStartPrefix) &&
report.Contains(TestMethodTypeAttribute) )
{
TestStarted?.Invoke(this, new EventArgs());
}
}
}
If I skip waiting and try to get the test result, I get an InvalidOperationException: 'Cannot retrieve Result from an incomplete or cancelled TestRun.'
How can I stop the test runner and get the results of the tests that were completed before the stopping?
You can't do it from inside a test. Your listener is executed in the context of the test itself. For that reason, listeners are specifically forbidden from trying to change the outcome of a test. Additionally, the event is buffered and may not even be received in this case until after the test run is complete.
StopRun is intended to be called by the main runner itself, generally as triggered by some user input.
You should also take note of this issue: https://github.com/nunit/nunit/issues/3276 which prevents StopRun(true) from working under any circumstances. It was fixed in PR https://github.com/nunit/nunit/pull/3281 but is not yet in any release of the framework. You will have to either use a recent dev build of the framework or switch to StopRun(false).
Based on the answer by #Charlie, this is how to modify the code in order to stop all threads:
public static void Main(string[] args)
{
// 2 assemblies x 2 TestFixtures each x 2 Tests each = 8 test cases
// each test case includes a 200 ms delay
string[] testAssemblyFileNames = { TestAssemblyFileName1, TestAssemblyFileName2 };
string assemblyDirectory = Path.GetDirectoryName(Uri.UnescapeDataString(
new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path));
// Nunit 3.10.0
var minVersion = new Version("3.4");
ITestEngine testEngine = TestEngineActivator.CreateInstance(minVersion);
// configure a test package that executes
// in the current process and in the current domain
var testPackage = new TestPackage(testAssemblyFileNames);
testPackage.AddSetting(EnginePackageSettings.ProcessModel, "InProcess");
testPackage.AddSetting(EnginePackageSettings.DomainUsage, "None");
testPackage.AddSetting(EnginePackageSettings.DisposeRunners, "True");
testPackage.AddSetting(EnginePackageSettings.WorkDirectory, assemblyDirectory);
ITestRunner testRunner = testEngine.GetRunner(testPackage);
var listener = new TestStartListener();
var testFilterBuilder = new TestFilterBuilder();
TestFilter testFilter = testFilterBuilder.GetFilter();
ITestRun testRun = testRunner.RunAsync(listener, testFilter);
// wait until the first test case has been started
while ( listener.Count < 1 )
{
Thread.Sleep(50);
}
bool keepRunning = true;
while ( keepRunning )
{
int testStartedCount = listener.Count;
testRunner.StopRun(force: false);
Writer.WriteLine($"{GetTimeStamp()}, Stop requested after {testStartedCount} test cases.");
// wait for less time than a single test needs to complete
bool completed = testRun.Wait(100);
bool running = testRunner.IsTestRunning;
Writer.WriteLine($"{GetTimeStamp()} Completed: {completed}, running: {running}");
keepRunning = !completed && running;
}
listener.WriteReportsTo(Writer);
XmlNode resultNode = testRun.Result;
Writer.WriteLine("Test result:");
resultNode.WriteContentTo(ResultWriter);
Console.ReadKey();
}
private class TestStartListener : List<string>, ITestEventListener
{
private const string TestCaseStartPrefix = "<start-test";
private const string TestMethodTypeAttribute = " type=\"TestMethod\"";
public event EventHandler<EventArgs> TestStarted;
public void OnTestEvent(string report)
{
if ( report.StartsWith(TestCaseStartPrefix) &&
report.Contains(TestMethodTypeAttribute) )
{
Add($"{GetTimeStamp()}, {report}");
TestStarted?.Invoke(this, new EventArgs());
}
}
public void WriteReportsTo(TextWriter writer)
{
Writer.WriteLine($"Listener was called {Count} times.");
foreach ( var report in this )
{
Writer.WriteLine(report);
}
}
}
The two test assemblies get executed in the runner's process, in a single domain and on two threads, one for each test assembly. In total, two test methods get executed and pass; one for each of the two test assemblies. Other test methods do not get executed and not reported. Other test fixtures (classes) do not get executed and get reported with result="Failed" label="Cancelled".
Note that testRunner.StopRun(force: false) is called repeatedly. If only called once, the other thread will run to completion.

Writing Test class for Currency Conversion Trigger

I need little assistance in writing a test class for this trigger that converts the record currency to org currency. Can anyone please assist/guide? Please.
trigger convertToEuro on CustomObject(before update) {
List<CurrencyType> currencyTypeList = [select id,IsoCode,ConversionRate from CurrencyType where isActive = true] ;
Map<String , Decimal> isoWithRateMap = new Map<String, Decimal>();
for(CurrencyType c : currencyTypeList) {
isoWithRateMap.put(c.IsoCode , c.ConversionRate) ;
}
for(CustomObject ce: trigger.new){
if(ce.CurrencyIsoCode != 'EUR' && isoWithRateMap.containsKey(ce.CurrencyIsoCode)){
ce.Amount_Converted__c = ce.ffps_iv__Amount__c/ isoWithRateMap.get(ce.CurrencyIsoCode);
}
}
}
#isTest(seealldata=false)
public class testConvertToEuro{
#testSetup
static void setupTest(){
List<CurrencyType> liCT = New List<CurrencyType>();
List<CustomObject> liCO = New List<CustomObject>();
Integer iCounter = 0;
liCT.add(new CurrencyType(IsoCode='EUR',ConversionRate=1,isActive=TRUE));
liCT.add(new CurrencyType(IsoCode='USD',ConversionRate=2,isActive=TRUE));
liCT.add(new CurrencyType(IsoCode='XXX',ConversionRate=3,isActive=TRUE));
liCT.add(new CurrencyType(IsoCode='YYY',ConversionRate=4,isActive=TRUE));
liCT.add(new CurrencyType(IsoCode='ZZZ',ConversionRate=5,isActive=TRUE));
for(Integer i=0; i<251; i++){
CustomObject co = New CustomObject(
CurrencyIsoCode = liCT[iCounter].IsoCode;
ffps_iv__Amount__c = liCT[iCounter].ConversionRate + .01;
);
if(iCounter < liCT.size() -1){
iCounter += 1;
}
else{
iCounter = 0;
}
}
insert liCT;
insert liCO;
}
static testmethod void runTest(){
setupTest();
List<CustomObject> liCustomObjectsToUpdate = New List<CustomObject>();
for(CustomObject co : [SELECT Id, ffps_iv__Amount__c, Amount_Converted__c from CustomObject]){
co.ffps_iv__Amount__c -= .01;
liCustomObjectsToUpdate.add(co);
}
test.startTest();
Update liCustomObjectsToUpdate;
test.stopTest();
System.assertEquals([SELECT Id, Amount_Converted__c FROM CustomObject LIMIT 1][0].Amount_Converted__c, 1);
}
}
I can't test this code without adding your custom object so it might have some errors. Please let me know what errors you are getting. If you want help debugging, you should provide the exact code being used and the exact error message.
It follows the basic pattern for testing update triggers:
Insert test data
Retrieve test data
Update records
Assert that the trigger generated the desired result

Hangfire , EF context used in the Task is not updated, the query inside the task always gives the old values

The context used inside the Hangfire task , always gives the old database values, it seems like the context is not updating inside the task. How to get the updated info context data inside hangfire task.
calling the task
BLL.Extraction NewExtractionCls = new BLL.Extraction();
var jobId = BackgroundJob.Enqueue(() => NewExtractionCls.SearchEngineExtraction("SearchURL", "1","1", null));
This is implementation
[Authorize]
public void SearchEngineExtraction(string SearchURL, int PageLimit, int SearchEngineID, PerformContext context)
{
WebClient wc = new WebClient();
#region Main Table - SearchEngineTbl
var NewExtraction = db.SearchEngineTbls.Where(x => x.SearchEngineID == SearchEngineID).FirstOrDefault();
var JobID = context.BackgroundJob.Id;
NewExtraction.JobID = Convert.ToInt32(JobID);
NewExtraction.SeachEngineURL = SearchURL;
NewExtraction.Status = "Processing";
db.SaveChanges();
var LinkCollectionRefined = ExtractLinkFromThisPage(i, SearchURL, wc).Distinct().ToList();//.Skip(10);
foreach (var Link in LinkCollectionRefined)
{
using (Entities dbRefreshed = new Entities())
{
// I get the same old value here, even if I update the table manually, when I rerun, everything is fine.
var CurrentStatusOfExtraction = db.SearchEngineTbls.Where(x => x.SearchEngineID == NewExtraction.SearchEngineID).FirstOrDefault();
if (CurrentStatusOfExtraction.IsActive == false)
{
return;
}
}
}
#endregion
}

quartz.net simple example as per the time given

I am new to quartz.net. I want to build a simple window based application which scheduled the task. Suppose I have 4 task ans it start and end time
Example
Breakfast ; 8:00;8:30
Lunch;13:00;13:30
dinner;19:30;20:00
Now I want when I click on button at 8:00 AM a message box should appear with a text "breakfast started!!!" at 8:30 AM again a message box should appear with a text as "Breakfast end!!!" and so on.
I had gone through tutorial. But confuse how to proceed. Can any one help me out?
EDIT
Is it possible to use one job and one triggers for this?
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
for (int i = 0; i < listBox1.Items.Count; i++)
{
string[] strArr = Regex.Split(listBox1.Items[i].ToString(), #";", RegexOptions.Multiline);
// define the job and tie it to our HelloJob class
IJobDetail jobStart = JobBuilder.Create<HelloJob>()
.WithIdentity("job" + i, "group1") // name "myJob", group "group1"
.StoreDurably()
.UsingJobData("jobSays", strArr[0].ToUpper().Trim() + " " + "starts")
.Build();
string[] ArrStart = strArr[1].Trim().Split(':');
ITrigger triggerstart = TriggerBuilder.Create()
.WithIdentity("trigger" + i, "group1")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(Convert.ToInt32(ArrStart[0]), Convert.ToInt32(ArrStart[1])))
// .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()
.ForJob(jobStart)
.Build();
// .WithSchedule(CronScheduleBuilder.CronSchedule("0 4 06 1/1 * ? *"))
// Tell quartz to schedule the job using our trigger
scheduler.ScheduleJob(jobStart, triggerstart);
scheduler.Start();
string[] Arrend = strArr[2].Trim().Split(':');
IJobDetail jobend = JobBuilder.Create<HelloJob>()
.WithIdentity("job1" + i, "group1")
.StoreDurably()
.UsingJobData("jobSays", strArr[0].ToUpper().Trim() + " " + "end")
.Build();
ITrigger triggerend = TriggerBuilder.Create()
.WithIdentity("trigger1" + i, "group1")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(Convert.ToInt32(Arrend[0]), Convert.ToInt32(Arrend[1])))
// .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionIgnoreMisfires()
.ForJob(jobend)
.Build();
scheduler.ScheduleJob(jobend, triggerend);
scheduler.Start();
}
}
catch (SchedulerException se)
{
Console.WriteLine("Scheduler Exception : " + se);
}
}
public class HelloJob : IJob
{
public void Execute(IJobExecutionContext context)
{
JobKey key = context.JobDetail.Key;
JobDataMap dataMap = context.JobDetail.JobDataMap;
string jobSays = dataMap.GetString("jobSays");
MessageBox.Show(jobSays);
}
}
You can schedule a job as follows and use this simple example:
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
IJobDetail job = JobBuilder.Create<MealAlertJob>()
.WithIdentity("mealJob", "mealJobGroup")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("breakfastStartTrigger", "mealTriggerGroup")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(8, 0))
.ForJob(job)
.Build();
ITrigger trigger1 = TriggerBuilder.Create()
.WithIdentity("breakfastEndTrigger", "mealTriggerGroup")
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(8, 30))
.ForJob(job)
.Build();
scheduler.ScheduleJob(job, trigger);
scheduler.ScheduleJob(trigger1);
scheduler.Start();
You should implement your logic (let's say showing a message box) in your job class's "Execute" method.
Defining multiple triggers for your job results in multiple execution. In the job class for example "MealAlertJob" you may check the time and show an alert based on it.
In the above example you can add more triggers for the other four times (lunch start/end and dinner start/end).
public class MealAlertJob : IJob
{
public void Execute(IJobExecutionContext context)
{
var now = DateTime.Now;
var hour = now.Hour;
var minute = now.Minute;
if (hour == 8 && minute == 0)
System.Windows.Forms.MessageBox.Show("Breakfast started!!!");
//and so on....
}
}
or something like this.

CRM 2011, Stopping custom workflow programmatically

i've been trying to stop a workflow programmatically.
I've read both in various posts and in the msdn that this can be done by updating
the Asyncoperation status via update request.
However everytime i update the request. the workflow get stuck on a mid stage such as cancelling or pausing and dosen't reach a final state.
any ideas?
protected void ExecutePostAccountUpdate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
string fetchXML = "<fetch mapping='logical' count='50' version='1.0'>" +
"<entity name='asyncoperation'>" +
"<filter>" +
"<condition attribute='regardingobjectid' operator='eq' value='" +
localContext.PluginExecutionContext.PrimaryEntityId + "' />" +
"</filter>" +
"</entity>" +
"</fetch>";
EntityCollection col = localContext.OrganizationService.RetrieveMultiple(new FetchExpression(fetchXML));
if (col.Entities.Count > 0)
{
AsyncOperation a = (AsyncOperation)col[0];
a.StateCode = AsyncOperationState.Completed;
a.StatusCode = new OptionSetValue(32);
localContext.OrganizationService.Update(a);
}
}
Have a look at my blog: How to Cancel Workflow Programmatically using C#
Make sure the user have permissions to Cancel System Jobs.
QueryExpression queryExpression = new QueryExpression("asyncoperation") { ColumnSet = new ColumnSet("statuscode") };
queryExpression.Criteria.AddCondition("name", ConditionOperator.Equal, Name of Workflow);
queryExpression.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, regardingobjectId);
var asyncOperations = organizationService.RetrieveMultiple(queryExpression);
foreach (var asyncOperation in asyncOperations.Entities)
{
if (((OptionSetValue)asyncOperation["statuscode"]).Value == 10 || // Waiting
((OptionSetValue)asyncOperation["statuscode"]).Value == 20 || // In Process
((OptionSetValue)asyncOperation["statuscode"]).Value == 0)
{
Entity operation = new Entity("asyncoperation")
{
Id = asyncOperation.Id,
["statecode"] = new OptionSetValue(3),
["statuscode"] = new OptionSetValue(32)
};
organizationService.Update(operation);
}
}
Make sure the user have permissions to Cancel System Jobs.
It seems you can un-publish a workflow via code, according to this post.
NOTE: This does not necessarily halt an in-progress workflow, but it will prevent any new workflows of that type from being started.
const int WorkflowStatusDraft = 1;
const int WorkflowStatusPublished = 2;
public void PublishWorkflow(Guid workflowId)
{
SetStateWorkflowRequest publishRequest = new SetStateWorkflowRequest();
publishRequest.EntityId = workflowId;
publishRequest.WorkflowState = WorkflowState.Published;
publishRequest.WorkflowStatus = WorkflowStatusPublished;
this.CrmService.Execute(publishRequest);
}
public void UnpublishWorkflow(Guid workflowId)
{
SetStateWorkflowRequest unpublishRequest = new SetStateWorkflowRequest();
unpublishRequest.EntityId = workflowId;
unpublishRequest.WorkflowState = WorkflowState.Draft;
unpublishRequest.WorkflowStatus = WorkflowStatusDraft;
this.CrmService.Execute(unpublishRequest);
}