Give me a session, or give me death



  • Found this gem, intended to provide a lock so that only one instance of a task is running at a time. We have a service that starts up a thread on a scheduled interval; and we don't want to start a second instance if the first instance hasn't finished yet. Used like this:

       using (var session = JobLock.GetSession()) {
           if (session != null)
               // do work
       };
    
        public static class JobLock
        {
            static Object _syncLock = new object();
            static Session _session;
            public static Session GetSession()
            {
                lock(_syncLock)
                {
                    return _session == null ? new Session(CompleteSession) : null;
                }
            }
    
            private static void CompleteSession()
            {
                lock(_syncLock)
                {
                    _session = null;
                }
            }
        }
    

    Hint: _session is never set. At least the code is threadsafe.


  • Winner of the 2016 Presidential Election

    @DrPepper said:

    Hint: _session is never set. At least the code is threadsafe.

    Well, if it is set, it returns null anyway. If it's not set, it creates a new session. Brillant!



  • The desired behavior is:

    Give me a token (a Session object) if I can run; or don't give me a token (return null) if I can't. The token (Session object) encapsulates the logic needed to clear the token when I'm done running.


    So that's not a WTF, it's actually pretty reasonable.


    The problem is, of course, is that internally in the class, the _session object is never set to the Session that is handed out; so the next person to come along also gets a Session object -- thus completely NOT implementing the intention of this class.



  • Just curious, is there any reason it starts up a thread on a set interval, instead of starting it up a certain amount of time after the task finishes?


  • Winner of the 2016 Presidential Election

    @DrPepper said:

    The desired behavior is:

    Give me a token (a Session object) if I can run; or don't give me a token (return null) if I can't. The token (Session object) encapsulates the logic needed to clear the token when I'm done running.


    So that's not a WTF, it's actually pretty reasonable.

    Ah, got it. I assumed it was a borked Singleton.



  • @Sutherlands said:

    Just curious, is there any reason it starts up a thread on a set interval, instead of starting it up a certain amount of time after the task finishes?

    Presumably there's no central node and nodes can spawn and die arbitrarily.



  • I'm assuming the "return" in GetSession() is supposed to look like this?

    return _session ?? (_session = new Session(CompleteSession));
    

    If so, it's a minor WTF really. Assuming there's a test to check that GetSession() doesn't return null, that test would pass regardless of whether _session was being set or not.

    I'm more worried about _syncLock not being defined as private readonly, and _session not being defined as private. And why is the using statement checking if session isn't null, when GetSession()'s definition implicitly dictates it will never return null?



  • Why would you name a class "jocklock"?



  • @blakeyrat said:

    Why would you name a class "jocklock"?
    You missread it.  it's JobLock.  However, I have to admit, I like JockLock better.



  • @The_Assimilator said:

    I'm assuming the "return" in GetSession() is supposed to look like this?

    return _session ?? (_session = new Session(CompleteSession));
    

     

    If so, it's a minor WTF really. Assuming there's a test to check that GetSession() doesn't return null, that test would pass regardless of whether _session was being set or not.

    I'm more worried about _syncLock not being defined as private readonly, and _session not being defined as private. And why is the using statement checking if session isn't null, when GetSession()'s definition implicitly dictates it will never return null?

     No, you only want to return a session to the one object that should run.  Should be something like:<pre>

    if(_session == null)

       _session = new Session; return _session;

    else

       return null;</pre>



  • @El_Heffe said:

    @blakeyrat said:
    Why would you name a class "jocklock"?
    You missread it.  it's JobLock.  However, I have to admit, I like JockLock better.

    See Dave Barry link in my sig please.



  • @blakeyrat said:

    @El_Heffe said:
    @blakeyrat said:
    Why would you name a class "jocklock"?
    You missread it.  it's JobLock.  However, I have to admit, I like JockLock better.

    See Dave Barry link in my sig please.

    I have my configuration set to not display signatures.  I find them repetitive, annoying and repetitive.



  • @Sutherlands said:

    @The_Assimilator said:

    I'm assuming the "return" in GetSession() is supposed to look like this?

    return _session ?? (_session = new Session(CompleteSession));
    

     

    If so, it's a minor WTF really. Assuming there's a test to check that GetSession() doesn't return null, that test would pass regardless of whether _session was being set or not.

    I'm more worried about _syncLock not being defined as private readonly, and _session not being defined as private. And why is the using statement checking if session isn't null, when GetSession()'s definition implicitly dictates it will never return null?

     No, you only want to return a session to the one object that should run.  Should be something like:<pre>

    if(_session == null)

       _session = new Session; return _session;

    else

       return null;</pre>

    Unexpected else. Expecting } or statement.


  • Discourse touched me in a no-no place

    @El_Heffe said:

    I have my configuration set to not display signatures.
    You can do that? Awesome…



  • @dkf said:

    @El_Heffe said:
    I have my configuration set to not display signatures.
    You can do that? Awesome…
    Yes, I am awesome.



  • @El_Heffe said:

    @blakeyrat said:

    Why would you name a class "jocklock"?
    You missread it.  it's JobLock.  However, I have to admit, I like JockLock better.

    I had a severe case of jock lock the other day. But after a [i]session[/i] with me...oh forget it.



  •  Am I the only one here who keeps seeing the header of this thread and thinking "Okay then, death.  But first: SNOO-SNOO!"



  • @da Doctah said:

    Am I the only one here who keeps seeing the header of this thread and thinking "Okay then, death.  But first: SNOO-SNOO!"
    Yes.  So please stop.

     



  • @dkf said:

    @El_Heffe said:
    I have my configuration set to not display signatures.
    You can do that? Awesome…
    Even if you couldn't, you could still do something like this...<!-- HTML generated using hilite.me -->

    // ==UserScript==
    // @name NoSig
    // @namespace anon
    // @include http://forums.thedailywtf.com/*
    // @version 1
    // @grant none
    // ==/UserScript==

    var sigs = document.getElementsByClassName('ForumPostSignature');
    while (sigs.length > 0) sig[0].parentNode.removeChild(sig[0]);

     



  • @blakeyrat said:

    See Dave Barry link in my sig please.

    There are sigs?



  • @Anonymouse said:

    @dkf said:

    @El_Heffe said:
    I have my configuration set to not display signatures.
    You can do that? Awesome…
    Even if you couldn't, you could still do something like this...

    // ==UserScript==
    // @name NoSig
    // @namespace anon
    // @include http://forums.thedailywtf.com/*
    // @version 1
    // @grant none
    // ==/UserScript==

    var sigs = document.getElementsByClassName('ForumPostSignature');
    while (sigs.length > 0) sig[0].parentNode.removeChild(sig[0]);

     

    My psychic powers determine that you are not using this script.

    Edit: What happens to Signature Guy?



  • @blakeyrat said:

    See Dave Barry link in my sig please.

    Still doesn't explain why you put effort into something you do by osmosis.



  • @pjt33 said:

    @blakeyrat said:
    See Dave Barry link in my sig please.

    There are sigs?
     



  • @immibis said:

    Edit: What happens to Signature Guy?
    A couple of years ago, people here were talking about Signature Guy and it took me a long time to figure out what was going on.  If you have signatures turned off in your profile settings, as I do,  then you do not see Signature Guy.  I can only assume that your script would have the same effect.  But, since this is Community Server, where unexpected behavior is the norm, that's not entirely certain.


  • Winner of the 2016 Presidential Election

    @El_Heffe said:

    @immibis said:

    Edit: What happens to Signature Guy?
    A couple of years ago, people here were talking about Signature Guy and it took me a long time to figure out what was going on.  If you have signatures turned off in your profile settings, as I do,  then you do not see Signature Guy.  I can only assume that your script would have the same effect.  But, since this is Community Server, where unexpected behavior is the norm, that's not entirely certain.


    So, if you expect the unexpected, you'll get what you expect?

    My head hurts...



  • @joe.edwards said:

    So, if you expect the unexpected, you'll get what you expect?

    My head hurts...

    I think you probably get what you deserve.



  • @El_Heffe said:

    I can only assume that your script would have the same effect.
    No, the script can't do anything about Signature Guy because it works on the final document structure. It can't tell which tags were actually supposed to be part of the document, and which ones were injected by the signature (fake closing 'div' tags are the culprit here) - at least not in just 2 lines of code...


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.