HTTPContext Session and worker thread (Fire and Forget)

Discussion in 'ASP .Net' started by Braulio Diez, Feb 8, 2008.

  1. Braulio Diez

    Braulio Diez Guest

    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
    /// ------------------------------
     
    Braulio Diez, Feb 8, 2008
    #1
    1. Advertising

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


    "Braulio Diez" <> wrote in message
    news:...
    > 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
    > /// ------------------------------
    >
    >
     
    George Ter-Saakov, Feb 8, 2008
    #2
    1. Advertising

  3. Braulio Diez

    Braulio Diez Guest

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




    "George Ter-Saakov" wrote:

    > 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.
    >
    >
    > "Braulio Diez" <> wrote in message
    > news:...
    > > 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
    > > /// ------------------------------
    > >
    > >

    >
    >
    >
     
    Braulio Diez, Feb 9, 2008
    #3
  4. Braulio Diez

    Braulio Diez Guest

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




    "George Ter-Saakov" wrote:

    > 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.
    >
    >
    > "Braulio Diez" <> wrote in message
    > news:...
    > > 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
    > > /// ------------------------------
    > >
    > >

    >
    >
    >
     
    Braulio Diez, Feb 9, 2008
    #4
  5. Braulio Diez

    Braulio Diez Guest

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




    "George Ter-Saakov" wrote:

    > 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.
    >
    >
    > "Braulio Diez" <> wrote in message
    > news:...
    > > 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
    > > /// ------------------------------
    > >
    > >

    >
    >
    >
     
    Braulio Diez, Feb 9, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. alex
    Replies:
    1
    Views:
    650
    Lau Lei Cheong
    Feb 4, 2005
  2. Stephen Barrett

    Asynchronous Fire-And-Forget Call

    Stephen Barrett, Apr 11, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    977
    Stephen Barrett
    Apr 13, 2006
  3. Tony

    Dim context As HttpContext = HttpContext.Current

    Tony, Mar 2, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    310
    Teemu Keiski
    Mar 3, 2004
  4. Michael Ulm

    process fire and forget?

    Michael Ulm, Nov 29, 2004, in forum: Ruby
    Replies:
    1
    Views:
    150
    Yukihiro Matsumoto
    Nov 29, 2004
  5. Dave Saville

    Fire and forget under OS/2

    Dave Saville, Oct 3, 2012, in forum: Perl Misc
    Replies:
    2
    Views:
    339
    Dave Saville
    Oct 4, 2012
Loading...

Share This Page