Multiple templates running concurrently - coldfusion

I have scheduled a coldfusion template to run every 10 minutes how do i prevent it from running when the previous run exceeds 10 minutes.
I've tried using a counter variable in the application scope unfortunately when the template times out or errors out the counter is not decremented.
PS. Is there a coldfuison framework for integrating applications (backend stuff)

Use an exclusive named cflock:
<cflock
timeout = "#createTimeSpan(0,0,0,1)#"
name = "myProcess"
throwOnTimeout = "no"
type = "exclusive">
<cfset start = now()>
<!--- CFML to be synchronized. --->
<cfset interval = 10 * 60> <!--- 10 minutes in seconds --->
<cfif dateDiff("s", start, now()) GT interval>
<cfhttp url="yourtemplate.cfm">
</cfif>
</cflock>
This ensures only thread ever runs the block of code in question. Simultaneous access will fail in one second without error.
To ensure the next run gets kicked off if the prior exceeds the time interval, track the start time inside the lock and then at the end, if its exceeded the interval, have the last statement inside the cflock be a cfhttp call to itself.

One possible route you could explore:
You could set up a database table to track the progress of the task. Maybe table name "task", with columns "taskName" and "inProgress", with the latter being a boolean. When the task starts, set inProgress to true. When it finishes, set inProgress to false.
In the template called by the scheduled task, have it first check the "inProgress" status of the specified task. If it's true, just abort. Otherwise, proceed.
EDIT:
Hmm... that actually wouldn't work any better than your application variables in the case of timeouts or errors. So now thinking that instead of a boolean, you use a timestamp. When the scheduled task fires, update the value with the current time. When it finishes, clear it out.
So when the task starts again, it will see that the previous task either finished (a null value), or it's still in progress. -If- it's still in progress, you can do a dateDiff() on the value to see if it was more than 'x' minutes ago. If it was, you can assume the previous task timed out (or errored out... but in that case I'd think you could put some error handling into the task itself) and run the current instance of the task.

Related

AWS Step Function Map with a Wait state in it

I want to use Step function Map, and have this logic that for each item in the input array
Lambda: checks a status of something:
CHOICE: true -> move on | false -> wait X hours and check for status again.
Does this mean if we set a max concurrency of 10 & have all 10 concurrently running maps in the wait stage because the status of something is false for all 10, my step function is essentially stuck and won't be able to continue on until one or more status of something comes back true?
It's probably the case that you want to avoid using Max Concurrency with Wait state in it, but I just want to confirm this is the case.
My alternative approach is to simply stage it to be processed again later and not block it.
Thanks.

Is there a way to set an expiration time for a Django cache lock?

I have a Django 3.1.3 server that uses Redis for its cache via django-redis 4.12.1. I know that cache locks can generally be set via the following:
with cache.lock('my_cache_lock_key'):
# Execute some logic here, such as:
cache.set('some_key', 'Hello world', 3000)
Generally, the cache lock releases when the with block completes execution. However, I have some custom logic in my code that sometimes does not release the cache lock (which is fine for my own reasons).
My question: is there a way to set a timeout value for Django cache locks, much in the same way as you can set timeouts for setting cache values (cache.set('some_key', 'Hello world', 3000))?
I've answered my own question. The following arguments are available for cache.lock():
def lock(
self,
key,
version=None,
timeout=None,
sleep=0.1,
blocking_timeout=None,
client=None,
thread_local=True,
):
Cross referencing that with comments from the Python Redis source, which uses the same arguments:
``timeout`` indicates a maximum life for the lock.
By default, it will remain locked until release() is called.
``timeout`` can be specified as a float or integer, both representing
the number of seconds to wait.
``sleep`` indicates the amount of time to sleep per loop iteration
when the lock is in blocking mode and another client is currently
holding the lock.
``blocking`` indicates whether calling ``acquire`` should block until
the lock has been acquired or to fail immediately, causing ``acquire``
to return False and the lock not being acquired. Defaults to True.
Note this value can be overridden by passing a ``blocking``
argument to ``acquire``.
``blocking_timeout`` indicates the maximum amount of time in seconds to
spend trying to acquire the lock. A value of ``None`` indicates
continue trying forever. ``blocking_timeout`` can be specified as a
float or integer, both representing the number of seconds to wait.
Therefore, to set the maximum time period of 2 seconds for which a cache lock takes effect, do something like this:
with cache.lock(key='my_cache_lock_key', timeout=2):
# Execute some logic here, such as:
cache.set('some_key', 'Hello world', 3000)

Scheduling reset every 24 hours at midnight

I have a counter "numberOrders" and i want to reset it everyday at midnight, to know how many orders I get in one day, what I have right now is this:
val system = akka.actor.ActorSystem("system")
system.scheduler.schedule(86400000 milliseconds, 0 milliseconds){(numberOrders = 0)}
This piece of code is inside a def which is called every time i get a new order, so want it does is: reset numberOrders after 24hours from the first order or from every order, I'm not really sure if every time there's a new order is going to reset after 24 hours, which is not what I want. I want to rest the variable everyday at midnight, any idea? Thanks!
To further increase pushy's answer. Since you might not always be sure when the site started and if you want to be exactly sure it runs at midnight you can do the following
val system = akka.actor.ActorSystem("system")
val wait = (24 hours).toMillis - System.currentTimeMillis
system.scheduler.schedule(Duration.apply(wait, MILLISECONDS), 24 hours, orderActor, ResetCounterMessage)
Might not be the tidiest of solutions but it does the job.
As schedule supports repeated executions, you could just set the interval parameter to 24 hours, the initial delay to the amount of time between now and midnight, and initiate the code at startup. You seem to be creating a new actorSystem every time you get an order right now, that does not seem quite right, and you would be rid of that as well.
Also I would suggest using the schedule method which sends messages to actors instead. This way the actor that processes the order could keep count, and if it receives a ResetCounter message it would simply reset the counter. You could simply write:
system.scheduler.schedule(x seconds, 24 hours, orderActor, ResetCounterMessage)
when you start up your actor system initially, and be done with it.

How to know when SendGatewayMessage fails

When using SendGatewayMessage it can sometimes fail if the "Maximum number of events to queue" setting (found in CF8 Admin > Event Gateways > Settings) is reached. I need to know when that happens, how can I?
found the following here:
You can check on how many threads are
running and the maximum thread size,
especially useful if you find you
exceeding the 'Maximum number of
events to queue' and don't/can't
change it easily.
<cfset gatewayService = createObject("java", "coldfusion.eventgateway.GatewayServices").getGatewayServices()>
<cfoutput>
Current: #gatewayService.getQueueSize()#
<br>
Max: #gatewayService.getMaxQueueSize()#
</cfoutput>
These classes appear to be documented a bit here.

C++ Timer control

I want to create a timer so that after completing the time(suppose 10 sec) the control should come out of the function..Please note that am starting the timer inside the function.Code is given below..I want to give certain time limit to that function so that after completing the time the control should come out of the function..I don't want to calculate the time..I want to give my own time so that the function should complete its execution within that time period..suppose if function is waiting for an input then also after completing time limit the control should come out indicating that "time has expired"..once it comes out of the function then it should continue with the next function execution...Is this possible in c++...
Begin();
// here I would like to add timer.
v_CallId = v_CallId1;
call_setup_ind();
call_alert_ind();
dir_read_search_cnf();
dir_save_cnf();
END();
If the code is linear and the functions called cannot be chopped into smaller pieces, your stuck to letting an external process/thread do the timing and abort the worker thread when the timeout is exceeded.
When you can chop the worker into smaller pieces you could do something like this
Timeout.Start(5000);
while ((TimeOut.TimeOut() == false) && (completed == false))
{
completed = WorkToDo()
}
This is a pattern we frequently use in our embbeded application. The timeout class was in house develop. It just reads the tick counter and looks if the time has passed. An framework like QT or MFC should have such a class itself.