Skip to content

Commit

Permalink
[Fixed Issue celery#6017]
Browse files Browse the repository at this point in the history
--> Added Multi default logfiles and pidfiles paths

[Description]:
--> Changed the default paths for log files & pid files to be '/var/log/celery' and '/var/run/celery'
--> Handled by creating the respective paths if not exist.
--> Used os.makedir(path,if_exists=True)

[Unit Test Added]:
--> .travis.yml - config updated with 'before install'.
--> t/unit/apps/test_multi.py - Changed the default log files & pid files paths wherever required.
  • Loading branch information
hanchau committed Apr 22, 2020
1 parent c0e587e commit 96c26f0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ matrix:
env: TOXENV=pypy3

before_install:
- sudo mkdir -p /var/log/celery
- sudo mkdir -p /var/run/celery
- sudo chown travis /var/log/celery
- sudo chown travis /var/run/celery
- sudo apt install libcurl4-openssl-dev libssl-dev gnutls-dev
- if [[ -v MATRIX_TOXENV ]]; then export TOXENV=${TRAVIS_PYTHON_VERSION}-${MATRIX_TOXENV}; fi; env
- |
Expand Down
8 changes: 6 additions & 2 deletions celery/apps/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ def __init__(self, name,

def _annotate_with_default_opts(self, options):
options['-n'] = self.name
self._setdefaultopt(options, ['--pidfile', '-p'], '%n.pid')
self._setdefaultopt(options, ['--logfile', '-f'], '%n%I.log')
self._setdefaultopt(options, ['--pidfile', '-p'], '/var/run/celery/%n.pid')
self._setdefaultopt(options, ['--logfile', '-f'], '/var/log/celery/%n%I.log')
self._setdefaultopt(options, ['--executable'], sys.executable)
return options

Expand All @@ -151,6 +151,10 @@ def _setdefaultopt(self, d, alt, value):
return d[opt]
except KeyError:
pass
path_split = value.split("/")
dir_path = "/".join(path_split[0:-1])
if not os.path.exists(dir_path):
os.makedirs(dir_path)
return d.setdefault(alt[0], value)

def _prepare_expander(self):
Expand Down
24 changes: 12 additions & 12 deletions t/unit/apps/test_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def assert_line_in(name, args):

def _args(name, *args):
return args + (
'--pidfile={0}.pid'.format(name),
'--logfile={0}%I.log'.format(name),
'--pidfile=/var/run/celery/{}.pid'.format(name),
'--logfile=/var/log/celery/{}%I.log'.format(name),
'--executable={0}'.format(sys.executable),
'',
)
Expand Down Expand Up @@ -176,7 +176,7 @@ def setup(self):
self.p = Mock(name='p')
self.p.options = {
'--executable': 'python',
'--logfile': 'foo.log',
'--logfile': '/var/log/celery/foo.log',
}
self.p.namespaces = {}
self.node = Node('[email protected]', options={'-A': 'proj'})
Expand All @@ -194,10 +194,10 @@ def test_from_kwargs(self):
'--executable={0}'.format(n.executable),
'-O fair',
'-n [email protected]',
'--logfile=foo%I.log',
'--logfile=/var/log/celery/foo%I.log',
'-Q q1,q2',
'--max-tasks-per-child=30',
'--pidfile=foo.pid',
'--pidfile=/var/run/celery/foo.pid',
'',
])

Expand Down Expand Up @@ -275,7 +275,7 @@ def test_handle_process_exit__signalled(self):

def test_logfile(self):
assert self.node.logfile == self.expander.return_value
self.expander.assert_called_with('%n%I.log')
self.expander.assert_called_with('/var/log/celery/%n%I.log')


class test_Cluster:
Expand Down Expand Up @@ -375,8 +375,8 @@ def test_getpids(self):
assert sorted(node_0.argv) == sorted([
'',
'--executable={0}'.format(node_0.executable),
'--logfile=foo%I.log',
'--pidfile=foo.pid',
'--logfile=/var/log/celery/foo%I.log',
'--pidfile=/var/run/celery/foo.pid',
'-m celery worker --detach',
'-n [email protected]',
])
Expand All @@ -386,8 +386,8 @@ def test_getpids(self):
assert sorted(node_1.argv) == sorted([
'',
'--executable={0}'.format(node_1.executable),
'--logfile=bar%I.log',
'--pidfile=bar.pid',
'--logfile=/var/log/celery/bar%I.log',
'--pidfile=/var/run/celery/bar.pid',
'-m celery worker --detach',
'-n [email protected]',
])
Expand All @@ -404,8 +404,8 @@ def __init__(self, path):

def read_pid(self):
try:
return {'foo.pid': 10,
'bar.pid': 11}[self.path]
return {'/var/run/celery/foo.pid': 10,
'/var/run/celery/bar.pid': 11}[self.path]
except KeyError:
raise ValueError()
self.Pidfile.side_effect = pids

0 comments on commit 96c26f0

Please sign in to comment.