Using the Locking System

Here we will have a look at the Locking System. We will learn how to lock and unlock objects using locks. Locks are used to make sure objects are only used by one owner.

Getting ready

The Locking System doesn't need anything, as it itself is just virtual.

How to do it...

There are three phases to locking: locking, checking and unlocking.

Create a lock

We will now create a lock:

  1. Create a new workflow and add a scriptable task.
  2. Add the following variables as input:

    Name

    Type

    Section

    Use

    object

    String

    IN

    Any string

    owner

    String

    IN

    Any string

  3. Enter the following script:
    LockingSystem.lockAndWait(object ,owner); 
    
  4. Add the workflow Display all Locks at the end.
  5. Save and run the workflow. Enter as object any string, such as Test and as owner or any other string such as Goofy.
  6. Check the logs.

Check for lock

We will now check if an object is locked:

  1. Create a new workflow and add a scriptable task.
  2. Add the following variables as input:

    Name

    Type

    Section

    Use

    object

    String

    IN

    Any string

    owner

    String

    IN

    Any string

    newLock

    Boolean

    OUT

    True if a new lock should be acquired

  3. Enter the following script:
          newLock=LockingSystem.lock(object ,owner); 
    
  4. Save and run the workflow. Enter as object the string from the first workflow and, as owner, any string you like.
  5. Check the output, it should be false as the workflow is still locked from the first run.

Unlock

We will now unlock a locked object:

  1. Create a new workflow and add a scriptable task.
  2. Add the following variables as input:

    Name

    Type

    Section

    Use

    object

    String

    IN

    Any string

    owner

    String

    IN

    Any string

  3. Enter the following script:
          LockingSystem.unlock(object ,owner); 
    
  4. Add the workflow Display all Locks before and after the scriptable task.
  5. Save and run the workflow. Enter as object the string from the first workflow and the owner from the first workflow.
  6. Check the logs.

How it works...

Orchestrator has an internal locking mechanism. It enables you to lock a virtual object, basically, a string. You set a lock with LockingSystem.lock(object, owner). Using the LockingSystem.lockAndWait(object, owner) method with the same object string will pause the workflow until the object is unlocked with LockingSystem.unlock(object, owner).

Please note that the object isn't really locked; only a lock entry is set. The lock is just a flag and nothing else. If you want to use the Locking System, you will need to check for the locking entry. You can check all locking entries with LockingSystem.retrieveAll(), which returns an array of strings where each string represents object,owner. You can release all locks with LockingSystem.unlockAll(). In the example packages, there are three examples of how to use the Locking System.

Have a look at the example workflows 08.02.4 Locktest (RUN ME) and 08.02.5 secondary locker, which show how the locking works.

A very important point to note here is that you have to make sure your workflow handles errors (in the workflow) well. You could end up in a situation where you lock a resource and the workflow locking it terminates on error and does not release the log. Have a look at Try-catch-finally in the recipe JavaScript special statements in Chapter 6, Advanced Programming.

See also

For locking elements in the Orchestrator Client, see the recipe Locking elements in Chapter 4, Programming Skills.

The following are some example workflows:

  • 08.02.1 Lock
  • 08.02.2 Check for Lock
  • 08.02.3 unlock
  • 08.02.4 Locktest (RUN ME)
  • 08.02.5 secondary locker
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset