Background Task asp.net - asp.net

I would like to create a background task which continuously inputs the location from a mobile to a database and in a website, I would like to get the same location immediately as it changes.
I am using an SQL Azure database. so pushing and polling are not an option. Also I am not sure if I can use a cache since the location continuously changes.
I think I have to create some infinite loop which carries on a task continuously. But how does this concept work?
Does this simply involve the create of a thread and a while(true) { ... } ?

I worked on a similar situation, and the approach I went for was to have an special page (/StartJob.aspx?AccessKey=xxxxxxxxxxxxx), that when hit with the right access key, would start a job cycle.
I then setup a "Cron Job" using www.setCronJob.com, to call this page at regular intervals. This service can notify you by email if it fails too.

Have a look at the timer control
http://msdn.microsoft.com/en-us/library/bb386404.aspx
sounds like something that could help out with achieving what you need :)

Related

Arcgis Add feature task got cancelled

We were trying to add a feature to the feature server using following snippet,
// Add the feature to the table.
await serviceFeatureTable.AddFeatureAsync(feature);
// Apply the edits to the service.
await serviceFeatureTable.ApplyEditsAsync();
It works most of the time but on rare case while adding an asset it take too much time to process and got timeout eventually gives TaskCancelledException, but the operation has been done as the item added in the server.
Does anybody have any ideas how to handle this situation or any thread related to this please share, thanks.

Create eXist-DB query queue

I’m looking for insight on how to build a system for my eXist DB App with which I can start multiple queries and have some kind of broker to ensure that only say the first 3 of them run simultaneously and when one of the queries is finished, it starts the next one waiting and so on.
I guess this can’t be done in pure Xquery, perhaps with some JavaScript hook? I also guess this means I have to look into Java Management Extensions, is that right?
In index.html of Monex the running queries seem to get displayed with the following code:
<span id="jmx-queries" data-bind="text: $data.jmx.ProcessReport.RunningQueries().length || 0">?</span>
I would like to replicate this in my own app and from here on look further into using jmx in my own app. So thanks for any input on how to do this or any help/further infos on how to achieve my main goal!
Thanks
You could keep the state of the queue in a document in the database, and you could have 3 scheduled tasks which poll that document. Each would pop the next item of the queue and run it. You can achieve all of that from XQuery.

Wordpress cron aka schedule?

register_activation_hook(__FILE__, 'cron_post_activation');
add_action('post_event', 'cron_post');
function cron_post_activation() {
wp_schedule_event(time(), 'hourly', 'post_event');
}
function cron_post() {
//Do stuff
}
This is not working by default - found out the hard way!
After debugging for hours I stumbled on several posts which claimed that you need to add next line to wp-config.php:
define('DISABLE_WP_CRON', true);
1) What's the logic behind that? Disabling something in order it to work doesn't make sense to me.
2) How to set it up with your own intervals? Docs states you can only use 'hourly', 'twicedaily' and 'daily'.
Yes, you do need to disable WP_CRON, however, you also need to set up your own cron job outside of WordPress to trigger the event. By default, WP's cron feature only kicks in when someone views the site, and even then it's not guaranteed that it will run exactly on time. It's not useful at all if you need something to happen at a specific time or interval.
Assuming you're on a *nix server, the best explanation I have found on how to do this is:
http://bitswapping.com/2010/10/using-cron-to-trigger-wp-cron-php/
The gist of it is that you turn off WordPress's way of triggering its own scheduler, and then access that system directly on a schedule that you set up using the cron program on your server. What this does is set up a barebones WP environment in which to execute tasks without sending any HTML back. This can result in much better performance than pinging your site homepage directly if you have the job set up to visit every minute.
And to the point about "turning it off to make it work" - yes, that seems counterintuitive. What you are actually turning off is the system that checks whether or not to execute the cron task or not. By turning that off, you essentially are building a direct pipeline to that system and telling it to do the job every single time, whenever you tell it to. You're taking the variance out of the system.

Run function on VB.NET, leave page, then have it email result to user?

I have an admin function that's rarely used that is built in VB.NET. It prepares a very long and complicated document and takes a very long time to process after user has hit the "Export PDF" button. What I would like to do is have ability for user to hit the Export button, leave the page and have it still going in the background and then when it's done, email the user to let them know report is ready with the URL of the file it generated.
I'm unsure if this is possible. It's my understanding that if you leave a page before a process is finished, it will interrupt/cancel the process. I can't reprogram my function in another language because it's just too complex for me to attempt to do that, so I need to stick with VB.NET.
I realize that it's not good practice to have a function that takes a long time on the server, but as I said, this is rarely used by a select amount of users and I'd like to make it more convenient for them.
Anyone know the best method/if any to get this accomplished?
Thanks!
I you can do one of these:
Create a child thread (spawn) and let that thread create and email PDF
Create a service and call that service to create and email PDF

Strategies for "Pre-Warming" an ASP.NET Cache

I have an ASP.NET MVC 3 / .NET Web Application, which is heavily data-driven, mainly around the concept of "Locations" (New York, California, etc).
Anyway, we have some pretty busy database queries, which get cached after they are finished.
E.g:
public ICollection<Location> FindXForX(string x)
{
var result = _cache.Get(x.ToKey()) as Locaiton; // try cache
if (result == null) {
result = _repo.Get(x.ToKey()); // call db
_cache.Add(x.ToKey(), result); // add to cache
}
return result;
}
But i don't want to the unlucky first user to be waiting for this database call.
The database call can take anywhere from 40-60 seconds, well over the default timeout for an ASP.NET request.
I want to "pre-warm" these calls for certain "popular" locations (e.g New York, California) when my app starts up, or shortly after.
I don't want to simply do this in Global asax (Application_Start), because the app will take too long to start up. (i plan to pre-cache around 15 locations, so that's a few minutes of work).
Is there any way i can fire off this logic asynchronously? Maybe a service on the side is a better option?
The only other alternative i can think of is have an admin page which has buttons for these actions. So an administrator (e.g me) can fire off these queries once the app has started up. That would be the easiest solution.
Any advice?
The quick and dirty way would be to fire-off a Task from Application_Start
But I've found that it's nice to wrap this functionality into a bit of infrastructure so that you can create an ~/Admin/CacheInfo page to let you monitor the progress, state, and exceptions that may be in the process of loading up the cache.
Look into "Always running" app setting for IIS 7.5. What this basically do is have an app pool ready whenever the existing one is to be recycled. Of course, the very first would take the 40-60 seconds but afterwards things would be fast unless you physically restart the machine.
Before you start cache warming, I suggest you check that the query is "as fast as it can be" by first looking at how many logical reads it is doing.
Sounds like you should just dump the results in a separate table and have a scheduled task to repopulate that table periodically.
If one pre-calculated table isn't enough because it ends up with too much data that you need to search through, you could use more than one.
One solution is to launch a worker thread in your Application_Start method that does the pre-warming in the background. If you do it right, your app won't take longer to start up, because the thread will be executed asynchronously.
One option is to use a website health monitoring service. It can be used to both check website health, and if scheduled frequently enough, to invoke your common URLs.
Doing the loading in a Task from Application_Start is the way to go, as mentioned by Scott.
Just be careful - if your site restarts and 10 people try to view California, you don't want to end up with 10 instances of _repo.Get(x.ToKey()); // call db simultaneously trying to load the same data.
It might be a good idea to store a boolean value "IsPreloading" in the application state. Set it to true at the start of your preload function and false at the end. If the value is set, make sure you don't load any of your 15 preloaded locations in FindXForX.
Would suggest taking a look at auto-starting your app, especially if you are load balanced.

Resources