Skip to content

Commit

Permalink
Merge pull request #38 from Emergya/concurrency-problem
Browse files Browse the repository at this point in the history
Added temporary solution to the concurrency problem
  • Loading branch information
alfem committed Nov 17, 2015
2 parents 45234db + fc34630 commit c7084ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config-templates/development.ini
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ chef.url = https://localhost/chef/api/
chef.cookbook_name = gecos_ws_mgmt
chef.seconds_sleep_is_busy = 5
chef.seconds_block_is_busy = 3600
# smart_lock_sleep_factor is use to avoid concurrency problem
# We use this parameter to sleep the process between GET and POST request.
# It's a temporary solution
chef.smart_lock_sleep_factor = 3
## Change this if you dont want to use mongo
## BROKER_URL = mongodb://localhost:27017/gecoscc
## CELERY_RESULT_BACKEND = mongodb
Expand Down
12 changes: 12 additions & 0 deletions gecoscc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,17 @@ def is_node_busy_and_reserve_it(node_id, api, controller_requestor='gcc', attemp


def _is_node_busy_and_reserve_it(node_id, api, controller_requestor='gcc'):
'''
Check if the node is busy, else try to get it and write in control and expiration date in the field USE_NODE.
'''
settings = get_current_registry().settings
seconds_block_is_busy = int(settings.get('chef.seconds_block_is_busy'))
time_to_exp = datetime.timedelta(seconds=seconds_block_is_busy)

time_get = time.time()
node = ChefNode(node_id, api)
time_get = time.time() - time_get

current_use_node = node.attributes.get(USE_NODE, {})
current_use_node_control = current_use_node.get('control', None)
current_use_node_exp_date = current_use_node.get('exp_date', None)
Expand All @@ -272,6 +279,11 @@ def _is_node_busy_and_reserve_it(node_id, api, controller_requestor='gcc'):
node.attributes.set_dotted(USE_NODE, {'control': controller_requestor,
'exp_date': json.dumps(exp_date, default=json_util.default)})
node.save()

smart_lock_sleep_parameter = settings.get('chef.smart_lock_sleep_factor', 3)
seconds_sleep_is_busy = time_get * int(smart_lock_sleep_parameter)
time.sleep(seconds_sleep_is_busy)

node2 = ChefNode(node.name, api) # second check
current_use_node2 = node2.attributes.get(USE_NODE, {})
current_use_control2 = current_use_node2.get('control', None)
Expand Down

0 comments on commit c7084ef

Please sign in to comment.