scripts/search-index
changeset 99 8719ac564b22
parent 98 8c6e36849f9a
child 100 147be2910b02
equal deleted inserted replaced
98:8c6e36849f9a 99:8719ac564b22
     5 """
     5 """
     6 
     6 
     7 # XXX: fix path
     7 # XXX: fix path
     8 import sys; sys.path.insert(0, '.'); sys.path.insert(0, '..')
     8 import sys; sys.path.insert(0, '.'); sys.path.insert(0, '..')
     9 
     9 
    10 import os, os.path
    10 import os, os.path, fcntl
    11 import datetime, pytz
    11 import datetime, pytz
    12 
    12 
    13 # configuration and the LogSearchIndex module
    13 # configuration and the LogSearchIndex module
    14 import config, utils, log_search, channels
    14 import config, utils, log_search, channels
    15 
    15 
   145     """
   145     """
   146         Creates a new index
   146         Creates a new index
   147     """
   147     """
   148 
   148 
   149     # open index
   149     # open index
   150     index = _open_index(options, 'c' if not options.force else '*')
   150     index = _open_index(options, 'ctrunc' if options.force else 'c')
   151 
   151 
   152     # that's all
   152     # that's all
   153     pass
   153     pass
   154 
   154 
   155 def cmd_load (options, channel_name, *dates) :
   155 def cmd_load (options, channel_name, *dates) :
   259 def cmd_autoload (options, *channel_names) :
   259 def cmd_autoload (options, *channel_names) :
   260     """
   260     """
   261         Automatically loads all channel logs that have not been indexed yet (by logfile mtime)
   261         Automatically loads all channel logs that have not been indexed yet (by logfile mtime)
   262     """
   262     """
   263     
   263     
   264     # open index
   264     # open index, nonblocking
   265     index = _open_index(options, 'c' if options.create else 'a')
   265     index = _open_index(options, 'c?' if options.create else 'a?')
   266 
   266 
   267     # default to all channels
   267     # default to all channels
   268     if not channel_names :
   268     if not channel_names :
   269         channels = config.LOG_CHANNELS
   269         channels = config.LOG_CHANNELS
   270     
   270     
   282         # path to our state file
   282         # path to our state file
   283         statefile_path = os.path.join(options.autoload_state_path, 'chan-%s' % channel.id)
   283         statefile_path = os.path.join(options.autoload_state_path, 'chan-%s' % channel.id)
   284         statefile_tmppath = statefile_path + '.tmp'
   284         statefile_tmppath = statefile_path + '.tmp'
   285         
   285         
   286         # do we have a tempfile from a previous crash?
   286         # do we have a tempfile from a previous crash?
   287         # XXX: locking
       
   288         if os.path.exists(statefile_tmppath) :
   287         if os.path.exists(statefile_tmppath) :
   289             # load after from it
   288             # first, open it...
   290             statefile_tmp = open(statefile_tmppath, 'r+')
   289             statefile_tmp = open(statefile_tmppath, 'r+')
       
   290 
       
   291             # ... then lock it
       
   292             fcntl.lockf(statefile_tmp, fcntl.LOCK_EX | fcntl.LOCK_NB)
   291             
   293             
   292             # read after timestamp
   294             # read after timestamp
   293             after_str = statefile_tmp.read().rstrip()
   295             after_str = statefile_tmp.read().rstrip()
   294 
   296 
   295             if after_str :
   297             if after_str :
   305                     print "\tIgnoring empty temporary statefile"
   307                     print "\tIgnoring empty temporary statefile"
   306 
   308 
   307         else :
   309         else :
   308             # open new tempfile
   310             # open new tempfile
   309             statefile_tmp = open(statefile_tmppath, 'w')
   311             statefile_tmp = open(statefile_tmppath, 'w')
       
   312             
       
   313             # lock
       
   314             fcntl.lockf(statefile_tmp, fcntl.LOCK_EX | fcntl.LOCK_NB)
   310 
   315 
   311         # override?
   316         # override?
   312         if options.reload :
   317         if options.reload :
   313             # load all
   318             # load all
   314             mtime = None
   319             mtime = None