Quartz.net daily jobs keeps getting executed every minute - scheduling

I have the code below. I would expect it to run daily at 17:00 if the config setting was not set otherwise the config setting will be used. So far no issue, the variable is set correctly. However: Instead of daily the job gets executed every minute and I cant figure out why. Is the scheduler not set up correctly?
TimeSpan timeOfExecution;
if (!TimeSpan.TryParse(ConfigurationManager.AppSettings["TimeOfExecution"], out timeOfExecution))
{
timeOfExecution = new TimeSpan(17, 0, 0);
}
var job = JobBuilder.Create<DailyReportJob>()
.WithIdentity("DailyReportJob")
.Build();
var trigger = TriggerBuilder.Create()
.WithIdentity("DailyReportTrigger")
.WithDailyTimeIntervalSchedule(s => s.OnEveryDay().StartingDailyAt(new TimeOfDay(timeOfExecution.Hours, timeOfExecution.Minutes)))
.Build();
Scheduler.ScheduleJob(job, trigger);
Scheduler.ListenerManager.AddJobListener(AutofacJobListener);
Scheduler.Start();

The default time for this trigger is every minute, since you haven't specified otherwise.
You can check all the intervals using this code:
var dt = trigger.GetNextFireTimeUtc();
for (int i = 0; i < 10; i++)
{
if (dt == null)
break;
Console.WriteLine(dt.Value.ToLocalTime());
dt = trigger.GetFireTimeAfter(dt);
}
If you want to schedule your job to run once a day at 5pm, you can change your code adding a 24 hour interval:
var trigger = TriggerBuilder.Create()
.WithIdentity("DailyReportTrigger")
.WithDailyTimeIntervalSchedule(s => s.OnEveryDay().StartingDailyAt(new TimeOfDay(timeOfExecution.Hours, timeOfExecution.Minutes)))
.WithIntervalInHours(24)
.Build();

Related

Alarm Manager stops trigger PendingIntent after few moments

I try to create reminder app. My aim is to trigger notification every hour. I have used Alarm Manager to achieve it. Everything works correctly for first 2-3 hours. After that time notifications stop being deliveried. If I reopen app I get missing notification imediatelly.
Alarm Manager:
val intent = Intent(context, AlarmReceiver::class.java).apply {
action = context.getString(R.string.alarm_pending_action)
}
val alarmIntent = PendingIntent.getBroadcast(context, 0, intent, FLAG_UPDATE_CURRENT)
val datetimeToAlarm = Calendar.getInstance()
.apply {
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
datetimeToAlarm.timeInMillis,
60 * 60 * 1000,
alarmIntent
)
AlarmReceiver:
override fun onReceive(context: Context, intent: Intent) {
if (intent.action != null) {
if (intent.action!!.equals(context.getString(R.string.alarm_pending_action), ignoreCase = true)) {
NotificationHelper.createNotification(context)
}
}
}
AndroidManifest
<receiver android:name=".alert.AlarmReceiver"/>
Thank you for your help
I solved my problem. Solution is to replace setRepeating by setAlarmClock and it works well.

How do I restrict hours of operation in a rest-based API function?

I have an API integration for our web store running on AWS Lambda to return live delivery quotes based on customer address, and then create the delivery order to a third party delivery as a service provider when the invoice is completed (paid).
I was able to add a time restriction for Monday-Saturday but Sunday has different hours and is not working. Here is the relevant code:
'use strict'
/**
* This function is use to generate qoutes to client 1st warehouse
*/
exports.handler = function (event, context, callback) {
console.log('-------------------EVENT OBJECT--------------------------')
// console.log(event.body.shipping_address)
console.log(event)
try {
const app = require('./app')
const EventEmitter = require('events').EventEmitter
const _bus = new EventEmitter()
let date = new Date()
if (date.getDay() == 0) {
if (!(date.getHours() >= 17 && date.getHours() <= 22) || !(date.getHours() < 3)) {
callback(null, {
message: 'The store is closed'
})
}
} else {
if (date.getHours() >= 3 && date.getHours() <= 15) {
callback(null, {
message: 'The store is closed'
})
}
}
let _shipmentReturn = []
let _shipmentReturnError = []
}
catch(e) {
}
}
Be very careful when using NOT logic.
Your 'normal' days have the store closed from 3am to 4pm. (Yes, 4pm. That's because you only check hours, so 3:59pm is still an 'hour' of 3, so it would be closed.)
On Sunday, the store is closed from midnight to 4:59pm, and also 10pm to midnight.
Take a look at this line:
if (!(date.getHours() >= 17 && date.getHours() <= 22) || !(date.getHours() < 3)) {
Let's pick a time of 2am. It equates to:
if (!(FALSE) || !(TRUE))
This equals TRUE, so the store is closed.
Same for 4am: if (!(FALSE) || !(FALSE)) also equals TRUE
You possibly want an AND rather than an OR in those logic statements.
I would also recommend that you convert the UTC times into your "local" times, which would make it easier for you to write the logic. This will avoid errors where UTC Sunday does not actually align to your 'local' Sunday. For example, if you are UTC-6, then 2am UTC Sunday is not Sunday in your timezone.

Sending High Volume of Emails with Coldfusion

This question could probably be related to doing anything high volume, but in this case I am trying to send emails.
I have setup the sending process in a new thread so user doesn't wait, and have overridden the request timeout to an hour.
The problem is that once the process get's up to about 2000 emails sent (looped over the below code about 2000 times) the server runs out of memory, stops responding, and needs a reboot.
Reading other topics on this, CF should be able to handle this volume of emails fine.
One thing I have considered is changing all object calls to straight DB Queries and using the cfmail tag to (I guess) remove all objects from being created and building up on reach request (which I guess is what is happening), but I'm not sure if that would make a difference, and really want to avoid that approach if possible. Something else I considered was splitting it across 3 or 4 seperate threads, but again, not sure if that would solve the problem.
Has anyone faced this problem, and what did you find worked to allow processing to continue without the ram slowly filling up and killing the server?
thread name="sendBroadcastEmail" rc="#rc#" prc="#prc#" filters="#rc.filters#" email="#email#" emailSignature="#emailSignature#"{
createObject( "java", "coldfusion.tagext.lang.SettingTag" ).setRequestTimeout(javaCast( "double", 3600 ));
//get profiles that it will be sent to
var sendToProfiles = profileService.getWithFilters(rc.filters);
var mailService = getPlugin("MailService");
var emailSent = false;
var sentCount = 0;
var failedCount = 0;
//send the email (and log in profile events)
if (listFind(attributes.rc.email.action,'send')){
for ( i=1; i<=arrayLen(sendToProfiles);i++){
var profile = sendToProfiles[i];
try{
if (len(trim(profile.getPrimaryEmail()))){
var emailBody = profile.processDynamicPlaceholders(attributes.rc.email.body);
var emailBody = attributes.emailSignature.getHeader() & emailBody & attributes.emailSignature.getFooter();
var sendEmail = mailService.newMail(
from = attributes.emailSignature.getEmailAddress(),
//to = profile.getPrimaryEmail(),
to = Application.settings.testemail,
subject = attributes.rc.email.subject,
body = emailBody,
type="html");
sendEmail.addMailParam(disposition='attachment', file=attributes.email.getAttachmentWithPath());
mailService.send(sendEmail);
//log profile event
profile.saveEvent(eventType = 3,
title="Broadcast Email: #attributes.rc.email.subject#",
description="Broadcast Email Sent: Subject: <br> #attributes.rc.email.subject#",
sentContent=emailBody,
ref2=1);
sentCount++;
}
}
catch (any exception){
//log profile event
profile.saveEvent(eventType = 3,
title="FAILED Broadcast Email",
description="<br>Subject: #attributes.email.subject#<br>This email should have been sent to this profile, but the attempted send failed. The likely cause is a malformed email address.",
sentContent=emailBody,
ref2=0);
failedCount++;
}
}
emailSent = true;
}
//persist email object
if (listFind(attributes.rc.email.action,'save')){
email.setTstamp(attributes.prc.now);
email.setSent(emailSent);
email.setStatsSent(sentCount);
email.save();
}
}//end thread
One approach would be to generate the emails in timed batches to spread the load evenly. The batch size and delay between batches can be adjusted to suit your environment.
thread name="sendBroadcastEmail" rc="#rc#" prc="#prc#" filters="#rc.filters#" email="#email#" emailSignature="#emailSignature#"{
createObject( "java", "coldfusion.tagext.lang.SettingTag" ).setRequestTimeout(javaCast( "double", 3600 ));
// set thread to a lowish prority
var currentThread = CreateObject( "java","java.lang.Thread" ).currentThread();
var priority = currentThread.getPriority();
currentThread.setPriority( 3 );
//get profiles that it will be sent to
var sendToProfiles = profileService.getWithFilters(rc.filters);
var mailService = getPlugin("MailService");
var emailSent = false;
var sentCount = 0;
var failedCount = 0;
//send the email (and log in profile events)
if (listFind(attributes.rc.email.action,'send')){
var emailsPerBatch = 1000; // divide into batches, set size here
var batchcount = Ceiling( ArrayLen( sendToProfiles ) / emailsPerBatch ); // number of batches
var batchdelay = 120000; // set delay between batches (ms)
// initialise first batch
var firstitem = 1;
var lastitem = emailsPerBatch;
for( var batch=1; batch<=batchcount; batch++ ) {
if( batch > 1 ){
// delay sending next batch and give way to other threads
currentThread.yield();
currentThread.sleep( batchdelay );
}
for ( var i=firstitem; i<=lastitem;i++ ){
var profile = sendToProfiles[i];
// generate emails ...
}
// initialise next batch
firstitem = lastitem++;
lastitem += emailsPerBatch;
if( lastitem > ArrayLen( sendToProfiles ) ) {
// last batch
lastitem = ArrayLen( sendToProfiles );
}
}
emailSent = true;
}
currentThread.setPriority( priority ); // reset thread priority
}//end thread

ColdFusion - Get next scheduled task due to run

This thread was useful in finding out the next run-time for a scheduled task.
How do I find out the next run time for a Scheduled Task?
But, is there also a way to simply get the next scheduled task due to run?
If I can get the date and name of the next task due to run, I can plug that date into a jQuery countdown timer, which will display a countdown to the next scheduled task, something like:
TaskABC due to run in:
12 03 20
hrs min sec
. This is for an admin interface in case you're wondering how geeky can people get:-)
EDIT
I had the same thought as Bill. But was curious if there was another way.
I poked around and apparently the internal Scheduler class maintains a list of upcoming tasks. The list is private, but you can use the same reflection technique to access it. Interestingly the list also includes system tasks like the mail spooler, session/application trackers, watchers, etecetera. So you must iterate through it until you find a "scheduled task" ie CronTabEntry
Below is a very lightly tested function that seems to do the trick in CF9. (Note, includes the CreateTimeStruct function from http://www.cflib.org).
Rules:
Returns a structure containing the name and time remaining until the next task. If no tasks were found, result.task is an empty string.
Excludes paused tasks
Usage:
result = new TaskUtil().getNextTask();
WriteDump(result);
CFC
component {
public struct function getNextTask() {
// get list of upcoming tasks from factory (UNDOCUMENTED)
local.scheduler = createObject("java", "coldfusion.server.ServiceFactory").getSchedulerService();
local.taskField = local.scheduler.getClass().getDeclaredField("_tasks");
local.taskField.setAccessible( true );
local.taskList = local.taskField.get(local.scheduler);
// taskList contains system jobs too, so we must iterate
// through the tasks to find the next "scheduled task"
local.nextTask = "";
local.tasks = local.taskList.iterator();
while ( local.tasks.hasNext() ) {
local.currTask = local.tasks.next();
local.className = local.currTask.getRunnable().getClass().name;
// exit as soon as we find a scheduled task that is NOT paused
if (local.className eq "coldfusion.scheduling.CronTabEntry"
&& !local.currTask.getRunnable().paused) {
local.nextTask = local.currTask;
break;
}
}
// if we found a task, calculate how many days, hours, etcetera
// until its next run time
local.details = { task="", remaining={} };
if ( isObject(local.nextTask) ) {
local.secondsToGo = (local.nextTask.getWhen() - now().getTime()) / 1000;
local.details.task = local.nextTask.getRunnable().task;
local.details.remaining = createTimeStruct(local.secondsToGo);
local.details.nextDate = dateAdd("s", local.nextTask.getWhen() / 1000
, "January 1 1970 00:00:00" );
}
return local.details;
}
/**
* Abbreviated version of CreateTimeStruct by Dave Pomerance
* See http://www.cflib.org/index.cfm?event=page.udfbyid&udfid=421
*
* #param timespan The timespan to convert.
* #return Returns a structure.
* #author Dave Pomerance
* #version 1, January 7, 2002
*/
public struct function CreateTimeStruct(required numeric timespan) {
var timestruct = StructNew();
var mask = "s";
// only 4 allowed values for mask - if not one of those, return blank struct
if (ListFind("d,h,m,s", mask)) {
// compute seconds
if (mask eq "s") {
timestruct.s = (timespan mod 60) + (timespan - Int(timespan));
timespan = int(timespan/60);
mask = "m";
} else timestruct.s = 0;
// compute minutes
if (mask eq "m") {
timestruct.m = timespan mod 60;
timespan = int(timespan/60);
mask = "h";
} else timestruct.m = 0;
// compute hours, days
if (mask eq "h") {
timestruct.h = timespan mod 24;
timestruct.d = int(timespan/24);
} else {
timestruct.h = 0;
timestruct.d = timespan;
}
}
return timestruct;
}
}
My first thought is to iterate Leigh's getNextRunTime(string taskName) function over the collection of tasks. You can get an array of structs containing the details of all scheduled tasks using taskArray = createobject("java","coldfusion.server.ServiceFactory").getCronService().listAll();
The key in the struct containing the task name is "task". So you can extract all the task names as an array for example, run Leigh's function on each element and determine which one will run next.

How to create service which restarts on crash

I am creating a service using CreateService. The service will run again fine if it happens to crash and I would like to have Windows restart the service if it crashes. I know it is possible to set this up from the services msc see below.
How can I programatically configure the service to always restart if it happens to crash.
Used Deltanine's approach, but modified it a bit to be able to control each failure action:
SERVICE_FAILURE_ACTIONS servFailActions;
SC_ACTION failActions[3];
failActions[0].Type = SC_ACTION_RESTART; //Failure action: Restart Service
failActions[0].Delay = 120000; //number of milliseconds to wait before performing failure action = 2minutes
failActions[1].Type = SC_ACTION_RESTART;
failActions[1].Delay = 120000;
failActions[2].Type = SC_ACTION_NONE;
failActions[2].Delay = 120000;
servFailActions.dwResetPeriod = 86400; // Reset Failures Counter, in Seconds = 1day
servFailActions.lpCommand = NULL; //Command to perform due to service failure, not used
servFailActions.lpRebootMsg = NULL; //Message during rebooting computer due to service failure, not used
servFailActions.cActions = 3; // Number of failure action to manage
servFailActions.lpsaActions = failActions;
ChangeServiceConfig2(sc_service, SERVICE_CONFIG_FAILURE_ACTIONS, &servFailActions); //Apply above settings
You want to call ChangeServiceConfig2 after you've installed the service. Set the second parameter to SERVICE_CONFIG_FAILURE_ACTIONS and pass in an instance of SERVICE_FAILURE_ACTIONS as the third parameter, something like this:
int numBytes = sizeof(SERVICE_FAILURE_ACTIONS) + sizeof(SC_ACTION);
std::vector<char> buffer(numBytes);
SERVICE_FAILURE_ACTIONS *sfa = reinterpret_cast<SERVICE_FAILURE_ACTIONS *>(&buffer[0]);
sfa.dwResetPeriod = INFINITE;
sfa.cActions = 1;
sfa.lpsaActions[0].Type = SC_ACTION_RESTART;
sfa.lpsaActions[0].Delay = 5000; // wait 5 seconds before restarting
ChangeServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, sfa);
The answer above will give you the gist... but it wont compile.
try:
SERVICE_FAILURE_ACTIONS sfa;
SC_ACTION actions;
sfa.dwResetPeriod = INFINITE;
sfa.lpCommand = NULL;
sfa.lpRebootMsg = NULL;
sfa.cActions = 1;
sfa.lpsaActions = &actions;
sfa.lpsaActions[0].Type = SC_ACTION_RESTART;
sfa.lpsaActions[0].Delay = 5000;
ChangeServiceConfig2(hService, SERVICE_CONFIG_FAILURE_ACTIONS, &sfa)