 http = require 'http'
 TransactionModel = require("../model/transactions").Transaction
 
+
+#####################################################################################################
+# Worker Process - Register the worker - Start the worker - Consume 'jobs' awaiting to be processed #
+#####################################################################################################
+
 worker = client.worker([ "transactions" ])
 worker.register rerun_transaction: (params, callback) ->
-  try
-    transactionID = params.transactionID;
-    taskID = params.taskID;
-
-    # Get the task object and update
-    # ensure transactionID is within task object
-    # pull and respond with transaction object
-    rerunGetTaskTransactionsData taskID, transactionID, (err, transaction) ->
+
+  transactionID = params.transactionID;
+  taskID = params.taskID;
+
+  # Get the task object and update
+  # ensure transactionID is within task object
+  # pull and respond with transaction object
+  rerunGetTaskTransactionsData taskID, transactionID, (err, transaction) ->
+
+    if err
+      logger.error(err)
+      return callback err, null
+
+    # setup the option object for the HTTP Request
+    rerunSetHTTPRequestOptions transaction, (err, options) ->
 
       if err
         logger.error(err)
         return callback err, null
 
-      # setup the option object for the HTTP Request
-      rerunSetHTTPRequestOptions transaction, (err, options) ->
+      #############################################################################
+      ### The job has processed correctly and is preparing to send HTTP Request ###
+      ### Move on to another job while this one waits for HTTPResponse to save  ###
+      ###     the result - "callback null, trasnactionID" completes the job     ###
+      #############################################################################
+      callback null, transactionID
+      
+      # Run the HTTP Request with details supplied in options object
+      rerunHttpRequestSend options, transaction, (err, HTTPResponse) ->
 
         if err
           logger.error(err)
           return callback err, null
-        
-        # Run the HTTP Request with details supplied in options object
-        rerunHttpRequestSend options, transaction, (err, HTTPResponse) ->
+
+        # Update the task object with the response details
+        rerunUpdateTaskObject taskID, transactionID, HTTPResponse, (err, updatedTask) ->
 
           if err
             logger.error(err)
             return callback err, null
 
-          # Update the task object with the response details
-          rerunUpdateTaskObject taskID, transactionID, HTTPResponse, (err, updatedTask) ->
-
-            if err
-              logger.error(err)
-              return callback err, null
-
-            # Return and process next job when rerun has completed
-            callback null, transactionID
-
-  catch err
-    callback err, null
-
 logger.info('Starting the workers')
 worker.start()
 
+#####################################################################################################
+# Worker Process - Register the worker - Start the worker - Consume 'jobs' awaiting to be processed #
+#####################################################################################################
+
 
 
 ###############################################################################


rerunUpdateTaskObject = (taskID, transactionID, response, callback) ->

  # decrement the remainingTransactions property
  TaskModel.update 
    _id: taskID

###############################################################################

rerunGetTaskTransactionsData = (taskID, transactionID, callback) ->

  # find the tasks object for the transaction being processed
  TaskModel.findById taskID, (err, task) ->
    if err
