Hi Support,
I am receiving a 504 gateway timeout error while performing a long task through my asp.net web app hosted at app harbor. After couple of minutes of sending request, the page throws error. Giving you some more info, on a asp.net page there is a button when clicked the page post-backs and performs little longer operation than other pages on my site.
How can I solve the problem? I tried setting server variables like Request.ScriptTimeOut as well as executionTimeout but no luck.
Is there even possible to allow some requests to perform longer than default time before it gets timed out?
Thanks,
Evil J.
#1 Posted by rune Support staff on 16 Sep, 2015 05:01 PM
Hi Evil J,
I'd generally recommend moving such expensive/CPU-intensive workloads to a background worker, which is a lot more suitable for long-running processes like this. For a number of reasons we do not plan on increasing the timeout on the shared load balancers, but there are a couple of ways you can work around it:
I'm also happy to take closer look, but before I can do that it'd be great if you install the Librato add-on (or perhaps the New Relic add-on) if you haven't already done so. That will allow me to provide better advice based on your application's performance and resource utilization profile.
Just a couple of thoughts -- let me know if there anything else I can help shed some light on.
Best,
Rune
#2 Posted by BrailleApps on 25 Sep, 2015 10:12 AM
Hi Rune,
Can I get a sample of background worker? I may want to see how both web worker and background worker interact with each other.
Actually, the long running tasks which takes more than 20 seconds, I want to execute using background worker. It's export basically, but heavy operations and takes more time so it deals with database hosted at amazon. I would like to run it on a daily basis.
I appreciate the chance to get a high level sample code how background worker works? Any deployment steps offered would a great help too!
Thanks,
Evil J.
#3 Posted by rune Support staff on 27 Sep, 2015 02:02 PM
Hi,
First I'd recommend reading this KB article about background workers. Background workers are fairly simple in that it's a worker type that will execute any windows executables contained in the build output, so a simple console application can run as a background worker. Just scale the app to have one background worker on the Subscription page and you can then track the realtime output of the background worker on the application's "Logging" page.
There are a few other things to keep in mind while building and designing your background worker:
Looping and exit codes: AppHarbor will try and restart your application if it exits with a non-zero exit code. This ensures that processing of tasks can continue when an exceptional condition causing a process termination occurs. If a background worker process exits with code 0 it won't be restarted until the next deployment as AppHarbor, so you'll likely want to add some kind of loop/blocking action with a sleep, which prevents the application from exiting while allowing you to poll for new tasks, i.e.:
You can find an example background worker here. You may also want to check this project, which shows a web and background worker using the Roslyn compiler that can run under the same application on AppHarbor when scaled to one web worker and one background worker.
Worker shutdown and graceful exits: Background workers should generally be idempotent as more than one instance of a worker can run at the same time. During a new deployment, for instance, a new background worker is started prior to terminating the old background worker. The old background worker will have 10 seconds to exit which allows you to release resources and free shared resources if necessary. A file is written to a location specified in an environment variable which signals that the worker is about to be shut down - you can see an example implementation of a background worker doing just that right here.
Queuing and scheduling: It will often be desirable to implement a queuing solution to ensure that several tasks can reliably be processed by several concurrent worker processes. This separation often leads to more reliable processing of tasks than for instance an in-memory queue in a web worker, which may be lost of the process suddenly terminates. It sounds like you need to run a report of a daily basis, so a queuing solution may not be necessary for your use case - instead you may want to use a scheduling library such as Quartz.NET, TinyCron or similar. We wrote a blog post about using the Quartz.NET scheduler that may be helpful if this is the solution you'd like to implement.
I hope this helps - let me know if you hit any snags or implementing the worker leads to new questions.
Best,
Rune
#4 Posted by chrispeoples on 27 Sep, 2015 11:23 PM
This comment was split into a new private discussion: 504 Gateway timeout error
I'm also getting a 504 Gateway Timeout error on my site, but I'm not doing any intensive processing. This site worked earlier this week without timing out.
http://rcpickem.apphb.com/
rune closed this discussion on 28 Sep, 2015 03:48 PM.