swift/bin/swift-recon-cron
Thiago da Silva a7c5ca0806 Fix locking in swift-recon-cron
The previous locking method would leave the lock dir lying around
if the process died unexpectedly, preventing others swift-recon-cron
process from running sucessfuly and requiring a manual clean.

Change-Id: Icb328b2766057a2a4d126f63e2d6dfa5163dd223
2018-08-15 21:51:14 +00:00

77 lines
2.7 KiB
Python
Executable File

#!/usr/bin/env python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
swift-recon-cron.py
"""
import os
import sys
from eventlet import Timeout
from swift.common.utils import get_logger, dump_recon_cache, readconf, \
lock_path
from swift.obj.diskfile import ASYNCDIR_BASE
def get_async_count(device_dir, logger):
async_count = 0
for i in os.listdir(device_dir):
device = os.path.join(device_dir, i)
if not os.path.isdir(device):
continue
for asyncdir in os.listdir(device):
# skip stuff like "accounts", "containers", etc.
if not (asyncdir == ASYNCDIR_BASE or
asyncdir.startswith(ASYNCDIR_BASE + '-')):
continue
async_pending = os.path.join(device, asyncdir)
if os.path.isdir(async_pending):
for entry in os.listdir(async_pending):
if os.path.isdir(os.path.join(async_pending, entry)):
async_hdir = os.path.join(async_pending, entry)
async_count += len(os.listdir(async_hdir))
return async_count
def main():
try:
conf_path = sys.argv[1]
except Exception:
print("Usage: %s CONF_FILE" % sys.argv[0].split('/')[-1])
print("ex: swift-recon-cron /etc/swift/object-server.conf")
sys.exit(1)
conf = readconf(conf_path, 'filter:recon')
device_dir = conf.get('devices', '/srv/node')
recon_cache_path = conf.get('recon_cache_path', '/var/cache/swift')
recon_lock_path = conf.get('recon_lock_path', '/var/lock')
cache_file = os.path.join(recon_cache_path, "object.recon")
lock_dir = os.path.join(recon_lock_path, "swift-recon-object-cron")
conf['log_name'] = conf.get('log_name', 'recon-cron')
logger = get_logger(conf, log_route='recon-cron')
try:
with lock_path(lock_dir):
asyncs = get_async_count(device_dir, logger)
dump_recon_cache({'async_pending': asyncs}, cache_file, logger)
except (Exception, Timeout) as err:
msg = 'Exception during recon-cron while accessing devices'
logger.exception(msg)
print('%s: %s' % (msg, err))
sys.exit(1)
if __name__ == '__main__':
main()