Scope
- Use Case:
- Consider the situation where a number of orders that have been added to a job chain. These orders should then be serialized to guarantee that each order has completed the job chain before the next order starts.
- Consider the situation that a number of job chains makes use of the same resource, e.g. by access to objects in a database. These orders should be serialized to guarantee that only one order of one job chain can access the resource at the same time.
- Solution Outline:
- The solution is twofold:
- Resource Locks implement a job chain that accepts shadow orders for access to resources. The job chain guarantees that only one order at a time is granted a resource lock. The job chain will suspend orders in originating job chains as long as a resource lock is blocked and will continue such orders if the resource lock is available. Any number of orders of the same or of different job chains can use the Resource Locks job chain for serialization.
- Resource Lock Consumers implements a sample job chain that makes use of a resource that may be accessed exclusively by one order at a time.
- The solution is twofold:
- References
Solution
- Download resource_locks.zip
- Download resource_lock_consumer.zip
- Extract the archives to the
./config/live
folder of your JobScheduler installation. - The archive extracts the files to the folders
resource_locks
andresource_lock_consumer
respectively. - You can store the
resource_lock_consumer
files in any folder as you like, however, if you move theresource_lock
files to some other location then you will have to adjust settings in theresource_lock_consumer
objects.
Pattern
Implementation
Components
- The solution implements a job named
sorter
that can be added at the start of any job chain.- This job implements a
spooler_process()
function that suspends all incoming orders. - This job is configured for a single task and with an idle timeout attribute. This means that it will execute incoming orders sequentially.
- Having received the last available order this job will wait for the duration specified with the
idle_timeout
attribute for new orders.- The idle timeout is configured using, for example
<job idle_timeout="10">
with thesorter
job definition. - Once the idle timeout has expired this job will execute its
spooler_exit()
function and then sort and move all orders that have previously been suspended.- Sorting is done in alphabetical order.
- The orders are moved to the next job chain node that follows the
sorter
job in the job chain.
- The idle timeout is configured using, for example
- This job implements a
- The download example uses a job chain named
job_chain1
that includes the job nodes for thesorter
job and ahello
job. This job chain accepts ad hoc orders that are added by JOC and it can easily be modified to watch for incoming files and to create an order for each file. - Hint: to re-use the
sorter
job you can:- store the job in a central folder and reference the job in individual job chains.
- move the job's JavaScript code to a central location and use an appropriate
<include>
element for individual job scripts.
Usage
- Add two orders to the
job_chain1
job chain.- Use an order ID in descending alphabetical order, e.g. "cba" for the order ID of the first order and "abc" for the order ID of the second.
- Both orders will be suspended at the first node of the job chain.
- After an idle timeout of 10s both orders will be moved to the next job node in the job chain.
- This time the orders will be processed in ascending alphabetical order.