How to set APScheduler timezone and execute task through an hour range? - python-3.x

scheduler = BlockingScheduler(timezone='US/Eastern')
scheduler.add_job(get_scores, 'cron', day_of_week='mon-fri', hour='8-18', minute='*/15')
scheduler.start()
I need this code to execute every 15 minutes from 9am - 5pm M-F EST. But for some reason it is doing:
Next wakeup is due at 2021-07-22 08:00:00-04:00 (in 47626.905794 seconds)
Thanks in advance!

timezone needs to be in the add_job, like:
scheduler.add_job(get_si_data, trigger='cron', day_of_week='mon-fri', hour='8-18', minute='*/15', timezone='US/Eastern')

Related

Is there any simpler way to write the cron job for every 5 minutes starting from 4th min then 9th min and so on

I am trying to write cron job for every 5 minutes, starting from 4th minute, 9th, 14th, and so on...
const cron = require("node-cron");
cron.schedule(`4,9,14,19,24,29,34,39,44,49,54,59 * * * *`, async function() {
console.log('scheduleCron ',new Date());
// my function call
})
It is executing perfectly, but I want to know is this the only method to do like this or any other method?
Thanks in advance :)
“At every 5th minute from 4 through 59.”
4/5 * * * *
Crontab guru
Warning: Non standard! May not work with every cron.

Execute a function every midnight in a timezone (CST) different than server timezone (UTC)

I have a time zone (timerTimeZone): For e.g. "America/Chicago".
let timerTimeZone = "America/Chicago"
Our server local time is in UTC.
I want to execute a function every night at 12.00 AM in the time zone which is stored in the timerTimeZone variable.
Let's say the program is running at 6.00 PM UTC/1.00 PM CST. So the first execution should be after 11 hours (12.00 AM CST) and next subsequent executions every 24 hours.
I have been trying to use moment and moment-timezone but not able to find any way.
I would suggest using the excellent Cron module.
This allows you to schedule tasks according to a cron expression, and also lets you specify an IANA timezone to use.
I'm also logging here the next 5 dates the job will run, both in the specified timezone and UTC.
const CronJob = require("cron").CronJob;
// Run at midnight every night
const cronExpression = "00 00 * * *";
const timeZone = "America/Chicago";
const cronJob = new CronJob(
cronExpression,
cronFunction,
null,
true,
timeZone
);
function cronFunction() {
console.log("cronFunction: Running....");
/* Do whatever you wish here... */
}
// Get the next N dates the job will fire on...
const nextDates = cronJob.nextDates(5);
console.log(`Next times (${timeZone}) the job will run on:`, nextDates.map(d => d.tz(timeZone).format("YYYY-MM-DD HH:mm")));
console.log("Next times (UTC) the job will run on:", nextDates.map(d => d.utc().format("YYYY-MM-DD HH:mm")));

Execute python script every 15 mins starting from 9:30:42 till 15:00:42 every day from Mon-Fri

I need a way to execute my python script every 15 mins starting from 9:30:42 till 15:00:42 every day from Mon-Fri.
I have explored APScheduler with cron syntax but can't figure out how to code the above condition. I tried below but doesn't work (execute is my function name)
sched.add_cron_job(execute, day_of_week='mon-fri', hour='9:30:42-15:00:42', minute='*/15')
Any pointer is deeply appreciated.
As far as I can tell you won't be able to do what you want with a single job.
This is the closest I could get with one:30-59/15 9-14 * * 1-5 which equates to Every 15 minutes, minutes 30 through 59 past the hour, between 09:00 AM and 02:59 PM, Monday through Friday.
Although it isn't exactly what you wanted I hope this helps as a base.
I wrote custom code to solve my problem. Posting here in case it helps someone. Any optimisations suggestions are welcome.
The first infinite loop starts the job when the start time is hit. The 2nd infinite loop wakes up every x minutes to check if next run time has approached. If yes, it executes else goes back to sleep. If the end time for execution has reached, then it breaks out
def execute_schedule_custom():
start_time_of_day = datetime.combine(date.today(), time(9, 30, 42))
next_run_time = start_time_of_day
end_time_of_day = datetime.combine(date.today(), time(15, 0, 42))
interval = 15
sleep_secs = 60 * 5 #sleep for 5 mins
while True:
if datetime.now() >= start_time_of_day:
execute()
next_run_time = start_time_of_day + timedelta(minutes=interval)
break
while True:
if datetime.now() >= end_time_of_day:
break
elif datetime.now() >= next_run_time:
execute()
next_run_time = next_run_time + timedelta(minutes=interval)
t.sleep(sleep_secs)

Python Aps Scheduler run in interval between two weekdays with time and schedule two job

I am trying to run a job between monday hour 0 to friday 19 and add second job scheduled in friday 20. It's not working, Not sure when combining multiple trigger for cron & interval works this way.
from apscheduler.triggers.combining import AndTrigger,OrTrigger
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.triggers.cron import CronTrigger
from apscheduler.schedulers.blocking import BlockingScheduler
scheduler = BlockingScheduler()
def job():
print('job started in mon 0, will be executed each minute until fri 19')
def job_report():
print('end job & report mail to team in fri 20')
trigger1 = OrTrigger([IntervalTrigger(minutes=1),
CronTrigger(day_of_week='mon', hour=0),
CronTrigger(day_of_week='fri',hour=19)])
trigger2 = OrTrigger([CronTrigger(day_of_week='fri',hour=20)])
scheduler.add_job(job, trigger1)
scheduler.add_job(job_report, trigger2)
scheduler.start()
Or should I use standard cron from aps? For example-
sched.add_job(job_function, CronTrigger.from_crontab('0 0 1-15 may-aug *'))
Any help will be appreciated.
trigger1 is executed monday to fri from 0:00 to 19:00 every day (not executed from 19:00 to 23:59)
trigger1 = AndTrigger([IntervalTrigger(minutes=1), CronTrigger(day_of_week='mon-fri', hour=0-19)])

Ramp and Hold Users for Some time and Ramp again

I have the following scenario to be load tested for a service and it does not seem to work as expected. My scenario is as follows.
Test with rampUsers(100) over 15 minutes duration
Hold the users for about 10 minutes holdFor(10 minutes)
Then again rampUsers(200) over 15 minutes duration
Hold the users for about 10 minutes holdFor(10 minutes)
Then again rampUsers(200) over 15 minutes duration
I am trying to use throttle option for this but it does not seem to work as expected
here is my code snippets combinations that I have tried so far
//NUM_USERS = 300
//DURATION = 15 minutes
//CONSTANT_DURATION = 5 minutes
// Tried with different combinations of NUM_USERS and DURATION but not helpful
scn.inject(
rampUsers(NUM_USERS*1) during DURATION,
constantUsersPerSec(1) during CONSTANT_DURATION,
rampUsers(NUM_USERS*2) during DURATION,
constantUsersPerSec(2) during CONSTANT_DURATION,
rampUsers(NUM_USERS*3) during DURATION,
constantUsersPerSec(3) during CONSTANT_DURATION
)
scn.inject(
rampUsers(NUM_USERS) during DURATION
).throttle(
reachRps(NUM_USERS/4) in (CONSTANT_DURATION),
holdFor(CONSTANT_DURATION),
jumpToRps(NUM_USERS/3),
holdFor(CONSTANT_DURATION),
jumpToRps(NUM_USERS/2),
holdFor(CONSTANT_DURATION)
)
scn.inject(
rampUsers(NUM_USERS) during DURATION
).throttle(
holdFor(CONSTANT_DURATION),
reachRps(NUM_USERS+NUM_USERS) in (DURATION+DURATION),
holdFor(CONSTANT_DURATION)
)
Can any one help on this which one works in this case. I would like to have a graph like this
To target injection rate as you stated you want in the comments, you need something like this
scn.inject(
rampUsersPerSec(0) to (300) during DURATION,
constantUsersPerSec(300) during CONSTANT_DURATION,
rampUsersPerSec(300) to (600) during DURATION,
constantUsersPerSec(600) during CONSTANT_DURATION,
...
)

Resources