Skip to content

Commit

Permalink
Add locking so that cASO does not run in parallel
Browse files Browse the repository at this point in the history
If cASO runs in parallel it will end up providing duplicate records. We
add now a locking mechanism so that it does not run in parallel, but it
locks until the first execution has ended.

Closes #34
  • Loading branch information
alvarolopez committed Jun 4, 2018
1 parent a9d2e7a commit a77e54e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
15 changes: 15 additions & 0 deletions caso/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
# under the License.

import datetime
import os
import os.path

import dateutil.parser
from dateutil import tz
from oslo_concurrency import lockutils
from oslo_config import cfg

import caso.extract.manager
Expand All @@ -37,6 +39,16 @@
help='Spool directory.'),
]

override_lock = cfg.StrOpt(
"lock_path",
default=os.environ.get("CASO_LOCK_PATH", "$spooldir"),
help="Directory to use for lock files. For security, the specified "
"directory should only be writable by the user running the "
"processes that need locking. Defaults to environment variable "
"CASO_LOCK_PATH or $spooldir"
)
opts.append(override_lock)

cli_opts = [
cfg.BoolOpt('dry-run',
deprecated_name='dry_run',
Expand All @@ -50,6 +62,8 @@
CONF.register_opts(opts)
CONF.register_cli_opts(cli_opts)

CONF.set_override("lock_path", override_lock, group="oslo_concurrency")


class Manager(object):
def __init__(self):
Expand All @@ -74,6 +88,7 @@ def lastrun(self):
raise
return date

@lockutils.synchronized("caso_should_not_run_in_parallel", external=True)
def run(self):
records = self.extractor_manager.get_records(lastrun=self.lastrun)
if not CONF.dry_run:
Expand Down
2 changes: 2 additions & 0 deletions caso/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

from dateutil import tz
import mock
from oslo_concurrency.fixture import lockutils as lock_fixture
import six

from caso import manager
Expand All @@ -28,6 +29,7 @@

class TestCasoManager(base.TestCase):
def setUp(self):
self.useFixture(lock_fixture.ExternalLockFixture())
super(TestCasoManager, self).setUp()
self.patchers = {
"makedirs": mock.patch('caso.utils.makedirs'),
Expand Down
1 change: 1 addition & 0 deletions etc/caso/caso-config-generator.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ wrap_width = 79
namespace = caso
namespace = caso.config
namespace = oslo.log
namespace = oslo.concurrency
21 changes: 21 additions & 0 deletions etc/caso/caso.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
# Spool directory. (string value)
#spooldir = /var/spool/caso

# Directory to use for lock files. For security, the specified directory should
# only be writable by the user running the processes that need locking.
# Defaults to environment variable CASO_LOCK_PATH or $spooldir (string value)
#lock_path = $spooldir

# Extract records but do not push records to SSM. This will not update the last
# run date. (boolean value)
# Deprecated group/name - [DEFAULT]/dry_run
Expand Down Expand Up @@ -281,6 +286,22 @@
#port = 5000


[oslo_concurrency]

#
# From oslo.concurrency
#

# Enables or disables inter-process locks. (boolean value)
#disable_process_locking = false

# Directory to use for lock files. For security, the specified directory
# should only be writable by the user running the processes that need locking.
# Defaults to environment variable OSLO_LOCK_PATH. If external locks are used,
# a lock path must be set. (string value)
#lock_path = <None>


[ssm]

#
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# process, which may cause wedges in the gate later.
pbr>=1.6
oslo.config>=2.3.0 # Apache-2.0
oslo.concurrency
oslo.log>=1.8.0 # Apache-2.0
oslo.utils!=2.6.0,>=2.0.0 # Apache-2.0

Expand Down

0 comments on commit a77e54e

Please sign in to comment.