Commit 5bebd24b55e033578ff513cfc7ed545d4bbe8e5c

Authored by Sergio Oliveira
1 parent cf548efb
Exists in spb-release/3.0

Locking import_proxy_data runs

Signed-off-by: Sergio Oliveira <sergio@tracy.com.br>
colab/proxy/management/commands/import_proxy_data.py
@@ -2,6 +2,9 @@ @@ -2,6 +2,9 @@
2 2
3 import importlib 3 import importlib
4 import inspect 4 import inspect
  5 +import logging
  6 +import os
  7 +import sys
5 8
6 from django.core.management.base import BaseCommand 9 from django.core.management.base import BaseCommand
7 from django.conf import settings 10 from django.conf import settings
@@ -12,7 +15,30 @@ from colab.proxy.utils.proxy_data_api import ProxyDataAPI @@ -12,7 +15,30 @@ from colab.proxy.utils.proxy_data_api import ProxyDataAPI
12 class Command(BaseCommand): 15 class Command(BaseCommand):
13 help = "Import proxy data into colab database" 16 help = "Import proxy data into colab database"
14 17
  18 + lock_file = settings.IMPORT_DATA_LOCK_FILE
  19 +
15 def handle(self, *args, **kwargs): 20 def handle(self, *args, **kwargs):
  21 + if os.path.exists(self.lock_file):
  22 + print(("This script is already running. "
  23 + "(If your are sure it's not please "
  24 + "delete the lock file in {}')").format(self.lock_file))
  25 + sys.exit(1)
  26 +
  27 + if not os.path.exists(os.path.dirname(self.lock_file)):
  28 + os.mkdir(os.path.dirname(self.lock_file), 0755)
  29 +
  30 + run_lock = file(self.lock_file, 'w')
  31 + run_lock.close()
  32 +
  33 + try:
  34 + self.run()
  35 + except Exception as e:
  36 + logging.exception(e)
  37 + raise
  38 + finally:
  39 + os.remove(self.lock_file)
  40 +
  41 + def run(self):
16 print "Executing extraction command..." 42 print "Executing extraction command..."
17 43
18 for module_name in settings.PROXIED_APPS.keys(): 44 for module_name in settings.PROXIED_APPS.keys():
colab/settings.py
@@ -251,6 +251,9 @@ MESSAGE_TAGS = { @@ -251,6 +251,9 @@ MESSAGE_TAGS = {
251 # Colab Settings 251 # Colab Settings
252 COLAB_HOME_URL = '/dashboard' 252 COLAB_HOME_URL = '/dashboard'
253 253
  254 +# Plugins
  255 +IMPORT_DATA_LOCK_FILE = '/var/lock/colab/import_data.lock'
  256 +
254 # Super Archives 257 # Super Archives
255 SUPER_ARCHIVES_PATH = '/var/lib/mailman/archives/private' 258 SUPER_ARCHIVES_PATH = '/var/lib/mailman/archives/private'
256 SUPER_ARCHIVES_EXCLUDE = [] 259 SUPER_ARCHIVES_EXCLUDE = []
colab/super_archives/management/commands/import_emails.py
@@ -270,7 +270,7 @@ class Command(BaseCommand, object): @@ -270,7 +270,7 @@ class Command(BaseCommand, object):
270 self.log(("This script is already running. " 270 self.log(("This script is already running. "
271 "(If your are sure it's not please " 271 "(If your are sure it's not please "
272 "delete the lock file in {}')").format(self.lock_file)) 272 "delete the lock file in {}')").format(self.lock_file))
273 - sys.exit(0) 273 + sys.exit(1)
274 274
275 if not os.path.exists(os.path.dirname(self.lock_file)): 275 if not os.path.exists(os.path.dirname(self.lock_file)):
276 os.mkdir(os.path.dirname(self.lock_file), 0755) 276 os.mkdir(os.path.dirname(self.lock_file), 0755)
  • 80bf7e338265f7a91f4144eaeded1f29?s=40&d=identicon
    Charles Oliveira @chaws

    @seocam: lines 27 and 28 from import_proxy_data.py, when creating the lock file isn't it better to do it with a try-except wrapping it around? I learned that the default '/var/lock/colab' directory is created already here (https://portal.softwarepublico.gov.br/gitlab/softwarepublico/softwarepublico/blob/master/cookbooks/colab/recipes/default.rb#L31) so it's OK for now, but if the file changes, in the future, to some other file which colab user doesn't have writing rights then an OSError is raised.

    Choose File ...   File name...
    Cancel
  • 9fe63c7bd60deeb55e409a1d7dd173f5?s=40&d=identicon
    Sergio Oliveira @seocam

    @chaws if we can't write the lock it's fine to fail. We must have write rights for the directory here the lock will be written.

    Choose File ...   File name...
    Cancel
  • F1c4e7359f83d87a9b8ffc5a003e9cc9?s=40&d=identicon
    Alexandre Barbosa @alexandre

    Line 28 won't work because the script (as colab user) tries to create a directory "/var/lock", which belongs to root user.

    Choose File ...   File name...
    Cancel
  • F1c4e7359f83d87a9b8ffc5a003e9cc9?s=40&d=identicon
    Alexandre Barbosa @alexandre

    Fixing this, i believe that everything is ok.

    Choose File ...   File name...
    Cancel