HTTPContext Session and worker thread (Fire and Forget)

B

Braulio Diez

Hello,

In my ASP .net application in one of the pages I have to fire a process
that takes quite long to execute, my idea is to execute this using the fire
and forget pattern (let the page to process and respond, but start a worker
thread to execute the process, it takes 1 minute to execute or something like
that).

All this I got it more or less
working(http://www.eggheadcafe.com/articles/20050818.asp).

Now comes the question...

My idea is to store the result in a session varialbe (later on using
script services I will play with that value), is a good approach to pass as a
parameter for the worker thread the HTTPContext or Session ? Too risky?(I
know an additional restriction is to use a static method :-() What other
approaches could I take?

Thanks, Regards
Braulio

/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------
 
G

George Ter-Saakov

I am not sure about implications of passing Session to another thread but in
general I would not do that. Unless you want to troubleshoot your
application for the rest of your life :)
---------------------------------------------------
I would create something more robust.

1. Table in DB tblJobs(JobId, Owner, Status, TimeIn, TimeOut, MessageIn,
MessageOut)
Status = (Pending, Processing, Completed)
Owner is who kciked off the job. List specific UserId

2. To kick of the job you insert record into tblJobs with MessageIn having
XML serialized object needed to do the job (like bunch of parameters). Also
you would need to increase "ActiveJobs" counter in the sesssion.

3. Some External Thread/External Process/MSMQ (your choice) pooling Jobs
to-do from the table and working on it. After it's done it populates TimeOut
and MessageOut with XML serialized object of the result of the job.

4. Your aspx page is checking for ActiveJobs counter if it's not 0 then
checks result of the job(s) submitted by that Owner. Updates Session if job
is completed.....

--------------------------------------------------------------------
PS: In order to avoid multithreading/multiprocessing problems you must be
careful. Always do optimistic locking when updating tblJobs
Meaning that if you have more than one process/thread that pools tblJobs for
new jobs then folow following steps (actually always do that)

1. Get a pending job "SELECT ... FROM tblJobs Where status = Pending"
2. Set status to processing "UPDATE tblJobs SET status=Processing WHERE
JobId=1 AND status = Pending"
3. Check that 1 (not 0) records were updated. SqlCommand.ExecuteNonQuery
returns that.
4. If returned value was 1 then proceed to execute job. If it's 0 then do
not execute job cause it was grabbed by other pooling thread between step 1
and 2.


George.








George.
 
B

Braulio Diez

Well, I think that approach is valid if your batch processes take long time
to execute (30 minutes, one hour)...

But in my case, my process would take a minute to execute, and I want to let
it run and then via AJAX take the result back.

As far as I know Session is thread safe (not the content that you store), so
passing the HTTPContext taking into account that a session can last for
instance 20 minutes and my process can take a minute, should be crazy stuff.

Is there some documentation about this topic indicating what are the
limitations of passing the HTTPContext?

Thanks in advance
Braulio



--
/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------
 
B

Braulio Diez

Mmm... sorry now I'm getting more understanding about the HTTPContext, only
it's alive meanwhile the main thread is active but debugging in an XP machine
things goes a bit different and seem to work (on a Windows 2003 Server or
Vista things changes). :-(((

/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------
 
B

Braulio Diez

Googling a bit I have found that, I could:

- Once the page is loaded on the client side, make the request via script
services:

- Good thing: I will get notified once the request has been completed
without affecting my page.

- Bad thing: I have to wait for the page to be served on the client side
and then launch from there the script service petition.

I could implement as well a custom HttpHandler, but I think we would have
the same scenario.

Any extra thought about this? Thanks, Regards
Braulio



/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top