I want to do very basic cron job on Azure Function.
I'm writing functions on Visual Studio and the functions will be dockerized job.
There are two different azure function on my solution.
Runs for every month. (Refreshes whole table).
Runs for every five minutes. (Refhreshes just feature records. Not historical records.)
My expectation is that the functions shouldn't block eachother. So, they shouldn't work at the same time.
My cron expressions for the functions are;
Function1: 0 0 0 1 * * (Runs every month on 1st.)
Function2: 0 */5 * * * * (Runs every five minutes)
I don't want to run function2 on every month like 01/01/2021 00:00:00.
How can I exclude the time from function2?
There is no direct way to do that.
As a workaround, you can add if else code block in Function2. For example, in the if block, you can determine if it's the time like 01/01/2021 00:00:00. if yes, then do nothing. If not, then go to the else block to execute your logic.
Like below:
public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
var the_time = DateTime.Now;
//if the date is the first day of a month, and time is 00:00:00
if(the_time.Day==1 && the_time.ToLongTimeString().Contains("00:00:00"))
{
//do nothing, don't write any code here.
}
else
{
//write your code logic here
}
}
Related
what the function does is deviding the numertor with the denomirator and updates the app's text view accordingly after every second, the problem is that it doesn't update the screen its just simply shows the original number of the numerator that is 60.
what do I change in order to make this work?
fun division() {
val numerator = 60
var denominator = 4
repeat(4) {
Thread.sleep(1_000)
findViewById<TextView>(R.id.division_textview).setText("${numerator / denominator}")
denominator--
}
}
Because you are setting (basically overwritting) the text everytime it loops through, you will only see the value of the last increment which would be 60/1 and that's why you are only seeing 60 value. Try like this:
fun division() {
val numerator = 60
var denominator = 4
repeat(4) {
Thread.sleep(1_000)
findViewById<TextView>(R.id.division_textview).append("${numerator / denominator}\n")
denominator--
}
}
setText() was overwriting the text with the new one but append() is gonna keep the previous text.
This is that dang Codelab again isn't it? I knew it looked familiar... I already answered a similar question here - but basically, when you run division on the main thread (which you must be since you're messing with UI components), you're freezing the app because you're blocking the thread with Thread.sleep
The display can't actually update until your code has finished running, i.e. after you exit the division function, because it's all running on the same thread, and the display update pass comes later. So this is what's actually happening:
freeze the app for 1 second
set the text as the result of 60 / 4 - it won't actually redraw until later, after your code has finished, so there's no visual change
freeze the app for 1 second
set the text as the result of 60 / 3 - again you won't see anything happen yet, but now it's going to show 60 / 3 instead of 60 / 4, because you just updated the state of that TextView
etc.
The last text you set is the result of 60 / 1, and then your code finishes, so the system can finally get around to updating the display. So the first thing you see after the app stops freezing is 60 - it's not just the numerator, it's the last calculation from your loop.
If you want something to update while the app is running, there are lots of solutions like coroutines, CountdownTimers, posting runnables that execute at a specific time, etc. The answer I linked shows how to create a separate thread to run basically the same code on, so you can block it as much as you like without affecting the running of the app. The one thing you don't do is block the main thread like that Codelab example does. It's a bad Codelab
You can use delay and then call from a coroutine:
private suspend fun division() {
val numerator = 60
var denominator = 4
repeat(4) {
delay(1000)
findViewById<TextView>(R.id.division_textview).text = "${numerator / denominator}"
denominator--
}
}
Then from your Activity/Fragment:
lifecycleScope.launch {
division()
}
I have a script that send an email to specific customer but what I'm trying to do is to fire that email in a given time and date .. so the solution is to use cron module like below and changing the parameters with what I want
cron.schedule("* * * * * " , function(){
}
the problem that I want to modify those parameters with varibales which contains a result for a specific calculation! like this below
const X = 234;// this values will change everyday automatically
cron.schedule("X * * * * " , function(){
}
so is it possible to do something like that or is there a better solution that allows me to modify cron parameters
the solution that I tried but nothing is working is below :
const x = 40;// 40 seconds
cron.schedule(`${x} * * * *`, function(){
}
Best Regards,
Many Thanks to num8er ,
the only solution for my problem is
Simply save to table jobs the stuff need to do and put cron script to run every minute which will check jobs table and will run what is scheduled by time. Example table: jobs [id, runAt, method, arguments, done], cron will run and will take jobs which is not done and runAtis less than now, will run method and pass arguments to it and after finishing it will set done=true
that's enough simple to achieve: 1 insert to table, 1 method that will run by cron and get jobs from table and execute
I'm using Node-Schedule package and I'm having some trouble to define the criteria using the * system.
Does anyone know how can I run this task every day 15 and 30 of a month (15 days interval)
var schedule = require('node-schedule');
var tarefa = schedule.scheduleJob('15-30 * * ', function() {
console.log("TAREFA");
});
One more question, let's say I wanna change this later based on user selected option, how can I get this current task schedule and change this interval later?
Thanks in advance!
0 0 0 1,15 * ? should work (see Quartz Cron expression :Run every 15 days ie twice in a month).
To change the schedule, you can call the rescheduleJob method with the job's name and the new user-specified schedule.
var schedule = require('node-schedule')
schedule.scheduleJob('myJob', '0 0 0 1,15 ? *', function() { console.log('hi') } )
schedule.rescheduleJob('myJob', '0 0 0 1,20 ? *')
I have a task scheduled like this:
RecurringJob.AddOrUpdate("Process inbound external messages", () => ProcessInboundExternalMessages(), Cron.Minutely);
It works.
But I need one to fire ever midnight.
So I tried to create a cron expression:
private const string CronDailyAtMidnight = "0 0 * * *";
And then assign that to my task.
RecurringJob.AddOrUpdate("Deactivate user role allocation on expiry", () => DeactivateUserRoleAllocationOnExpiry(), CronDailyAtMidnight);
However, it never fires. Can anyone see an issue?
The issue was - it uses UTC date. Fixed.
I was wondering if this concept is doable:
Scenario:
4 areas on 1 stage which are quite similar (eg webcamconference, each area has the same functions)
Buttonobjects are numbered(eg area 1 has playbutton1,mutebutton1,namebutton1,namelabel1, etc)
every area gets a close-Button which closes/shuts down the area.(close1,close2,close3...)
i want to archive the following:
if(close1.isPressed){
function invisall(1);
}
/*
* instead of writing
* if(close1.isPressed){
* playbutton1._visible=false;
* mutebutton1._visible=false;
* }else if(close2.isPressed){
* playbutton2._visible=false;
* mutebutton2._visible=false;
* etc. resulting in an enormous block.
* the interesting part. buttonNr gets added via String to become a real buttonname(eg
* playbutton1 as mentioned above).*/
function invisall(int buttonNr){
String newPlayButtonObjectName="playbutton"+buttonNr;
newPlayButtonObjectName._visible=false;
String newMuteButton="mutebutton"+buttonNr;
newMuteButton._visible=false;
}
this should do the trick via dynamic Nr at the end of each default button(eg
playbutton)
but ofc
"playbutton1"._visible=false;
doesnt work because playbutton1 is still a String.
how can i take the String as a Buttonname/ButtonObject?
do i need to write an new function? this would destroy the purpose of less code.
maybe u like this idea. for now i will split the area into frames that i put on the stage. that should help.
cheers
function invisall(int buttonNr){
this["playbutton"+buttonNr]._visible=false;
this["mutebutton"+buttonNr]._visible=false;
}
I hope this is what you want.