NetTalk Central

Author Topic: Avoiding doing time consuming work in the WebServer procedure  (Read 1842 times)

GordonF

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Avoiding doing time consuming work in the WebServer procedure
« on: August 06, 2021, 07:33:12 AM »
I think this is a question for Bruce but please feel free to comment.

A while back I posted a message about record locking (not literally) in a multi user system. The strategy that I proposed and Bruce clarified is working out ok so far, however I have one issue I'd like comments on please.

The record locking actually places records in a lock table and is in effect soft locking, under normal circumstances the locks get cleared, such as record update, update cancel, user logout and session timeout. However I do get occasional locks that don't get cleared due to people navigating away from an update in their browser, they stay in the product but the page never gets completed or cancelled so the lock never gets deleted.

I can only think of two ways to tackle this:

1. Detect the user has navigated away from the update page, they may have pressed the browser back button or closed the tab, unfortunately I don't know how to do this

2. I add a SessionID and DateTimeStamp to the lock records, so I can have a housekeeping task that clears old locks, that's easy I just have a thread running that bins locks older than a certain number of minutes, crude but it works. However I would like to also check for locks with sessionids that are no longer active in case any tidy up was missed. The way I've done this is to get a list of unique SessionIDs from the lock table and use _GetSessionLoggedIn to check if they logged in (no locks unless logged on this system), it works fine.

The thing that concerns me about doing this in the webserver procedure is the time it could take and the fact it could potentially disrupt the webservers functionality. So I call a procedure on a new thread and reference the webserver class and the _GetSessionLoggedIn  method in there, but now I'm becoming concerned that calling a method in the referenced class from a different thread may also clash with the same class instance running in the webserver procedure, is it a bad idea?

Any advice would be appreciated.

Gordon

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11179
    • View Profile
Re: Avoiding doing time consuming work in the WebServer procedure
« Reply #1 on: August 09, 2021, 07:07:14 PM »
Hi Gordon,

>> 1. Detect the user has navigated away from the update page, they may have pressed the browser back button or closed the tab, unfortunately I don't know how to do this

The short answer is "you can't" -since they could just terminate the browser program, or turn off the computer. This is the inherent danger with locking records as you are doing, and why I'm not a fan of your approach.

the longer answer is that you could create a websocket connection to the form, then detect when that connection times out (if the page disappears, it'll time out on the next internal ping). I fear this may be difficult for you at this stage though as there would be quite a lot of embed code, and a bit of javascript, to match up a sock to a lock.

>> 2. I add a SessionID and DateTimeStamp to the lock records, so I can have a housekeeping task that clears old locks,

yes, this is likely better, and easier

>> However I would like to also check for locks with sessionids that are no longer active in case any tidy up was missed.

there's a method in WebHandler, called NotifyDeleteSession which you can embed code into so you can do things when a session is deleted. You should also note the NotifyChangeSession as well as Session ID's can change. If you are using a table to store this data there are template options in WebHandler where you can specify the table, and the Change and Delete will be done for you.

Cheers
Bruce



GordonF

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Avoiding doing time consuming work in the WebServer procedure
« Reply #2 on: August 10, 2021, 12:23:22 AM »
Thankyou Bruce for a clear and very helpful reply.

I am using the NotifyDeleteSession and NotifyChangeSession and under normal circumstances the locks are maintained correctly, it's navigating away and closing the browser that are issues. I need some form of locking as the user is working on medical records and potential data loss is not an option, as in user A and B working on the same record at the same time and the first to save will have changes overwritten by the second to save without ever knowing. I will persevere for a little longer but I may have to switch to optimistic locking and check the record for changes before updating (I think Clarion's record buffers would be helpful with this). I just worry that potentially losing edits at the moment of saving will be very frustrating for a user.

Gordon

Jane

  • Sr. Member
  • ****
  • Posts: 349
  • Expert on nothing with opinions on everything.
    • View Profile
    • Email
Re: Avoiding doing time consuming work in the WebServer procedure
« Reply #3 on: August 10, 2021, 01:21:19 PM »
Gordon,

Maybe when User B opens a form on MRN 100 he gets a message "Record 100 is locked for update by User A since 9:14AM.  Click here to clear the lock and any edits he may be making."

If User B clicks the button you remove User A's lock and make a new lock for User B.

You also check the lock before saving, so if 5 minutes later User A tries to save what's on his open form he gets a "Sorry, Skippy" message.

People may not be happy (are they ever??) but at least they know what happened.

JAT.

Jane

GordonF

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Avoiding doing time consuming work in the WebServer procedure
« Reply #4 on: August 11, 2021, 12:06:12 AM »
Thanks Jane, that is an idea I will think about it may well work for me.

Gordon

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11179
    • View Profile
Re: Avoiding doing time consuming work in the WebServer procedure
« Reply #5 on: August 11, 2021, 04:16:59 AM »
>> I am using the NotifyDeleteSession and NotifyChangeSession and under normal circumstances the locks are maintained correctly, it's navigating away and closing the browser that are issues.

NotifyDeleteSession happens when the session ends. It has nothing to do with how they leave the form (or if they ever leave the form).

So if your tidy-up code is in these methods, then it'll run when the session ends. Of course if the user leaves the form, but doesn't end their session, then the lock remains...

Cheers
Bruce

GordonF

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Avoiding doing time consuming work in the WebServer procedure
« Reply #6 on: August 12, 2021, 01:46:44 AM »
"Of course if the user leaves the form, but doesn't end their session, then the lock remains..."

This is the part I'm trying to resolve, everything else is working fine. I do have a solution but its a bit brutal, my housekeeping process can remove lock records older than a number of defined minutes, but it's far from ideal. Your answer to another question has prompted me to look into the possibility of using websockets to popup a dialog in the user's browser, however even if it can be done I have a lot to learn before I can achieve that.

Gordon