Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am in the design phase of a project where the end user will submit a request from a web page that will spawn a long running asynchronous processed job. Is there a "best practice" for this problem? Are web services and service brokers a good way to go? Is Microsoft messaging queue applicable here?

share|improve this question
    
If doing this over the web, and if it takes a very long time, then you can send a text message or an email to the user when it has completed. –  Job Feb 13 '11 at 17:26

2 Answers 2

up vote 4 down vote accepted

I don't know about "best practice". I do know the most common mistakes.

First Mistake: DOS Yourself

You use the webhandler to process the long running job. This can be bad or extremely bad depending on your percentage of hits that become long running jobs, how long they run and how much sustained traffic you get.

You want to make sure that you aren't getting more than 1 long running job within the period of time it takes for that long running job to complete. If you do you DOS yourself. It will also get worse the more traffic you get assuming the percentage and time stays consistent. It's one of those problems which self-imposes a limit on traffic growth.

Second Mistake: Spawning from the webhandler

Spawning a process from the web handler to handle a long running process can be tricky, and as a result also error prone.

  • You have to dissociate from the parent correctly otherwise the webhandler waits for the child to complete.
  • When you fork a child in unix it inherits open handles from the parent. These will automatically be closed unless they are overwritten. This includes things like database connections, filehandles, other open network connections. All of which get closed when the child process completes.

Options

I usually use at(1) to cleanly dissociate from the webhandler without forking.

You can also use a polling implementation with cron.

You can communicate to another server process that handles the processing. That communication can be done with sockets, pipes, or higher level abstractions like a REST http call or routing a queue message.

share|improve this answer

I suppose you are talking about more than a few minutes.

If it's few minutes, you could launch a worker thread on the background and display a some progress on the UI. Lots of web applications use that method.

If it's more than, say 5 minutes, you may want to delegate the task to a dedicated service. One example is the reports generated by Google Analytics.

Microsoft Messaging queue can be used to transmit information from a system to another, or a component to another.

share|improve this answer
    
How do languages like PHP accomplish this? I know a few web apps that do this but are written in PHP –  TheLQ Feb 13 '11 at 18:52
    
I'm no PHP expert, but I guess instead of a thread launched by the same page, another is called. This is what actually happens with AJAX. –  user2567 Feb 13 '11 at 18:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.