Archived Support Site

This support site is archived. You can view the old support discussions but you cannot post new discussions.

504 Gateway timeout error

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. 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:

    • The HTTP 504 is only returned if no data is received from the backend worker in 120 seconds. Perhaps you can restructure the app so it streams data back to the client and keep the connection open while doing so.
    • If you get a dedicated load balancer/IP-based SSL we can set the timeout much higher as it won't potentially impact other customers. However, note that there are still certain issues related to running long-running tasks in web workers/IIS so ideally the task would be moved to a background worker over time.
    • Increase the worker resource allocation by scaling vertically to process jobs faster. Scaling out to multiple workers may also be helpful, but it depends a lot on your application. You can read more about vertical and horizontal scaling in this blog post.
    • If the bottleneck is an external dependency, such as an database, you may be able to achieve better performance by scaling that.

    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. 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. 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.:

    public class Program
    {
        public static void Main(string[] args)
        {
            while (true)
            {
                //Do work
                Thread.Sleep(1000); // Artificial sleep to prevent the worker from consuming excessive resources while idling.
            }
        }
    }
    

    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. 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/

  5. rune closed this discussion on 28 Sep, 2015 03:48 PM.

Discussions are closed to public comments.
If you need help with AppHarbor please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac