Skip to content

Commit

Permalink
Merge 907ca02 into 978a26d
Browse files Browse the repository at this point in the history
  • Loading branch information
w136712058 committed Dec 27, 2019
2 parents 978a26d + 907ca02 commit 8268886
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.rst
Expand Up @@ -108,6 +108,9 @@ CRONJOBS
# format 2
('0 0 1 * *', 'myapp.cron.other_scheduled_job', ['myapp']),
# format 3 specify user to run job(can avoid authority problem such as file I/O authority)
('0 0 * * 0', 'myapp.cron.other_scheduled_job [byuser=username]', '>> /tmp/scheduled_job.log'),
('0 0 * * 0', 'django.core.management.call_command', ['dumpdata', 'auth'], {'indent': 4}, '> /home/john/backups/last_sunday_auth_backup.json'),
]
Expand Down
49 changes: 37 additions & 12 deletions django_crontab/crontab.py
Expand Up @@ -14,7 +14,10 @@

from django_crontab.app_settings import Settings

string_type = basestring if sys.version_info[0] == 2 else str # flake8: noqa
if sys.version_info[0] == 2:
string_type = basestring
else:
string_type = str

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -165,16 +168,39 @@ def run_job(self, job_hash):
logger.warning('Tried to start cron job %s that is already running.', job)
return

# parse the module and function names from the job
module_path, function_name = job_name.rsplit('.', 1)
# import the module and get the function
module = import_module(module_path)
func = getattr(module, function_name)
# run the function
try:
func(*job_args, **job_kwargs)
except:
logger.exception('Failed to complete cronjob at %s', job)
if "byuser=" not in job[1]:
# parse the module and function names from the job
module_path, function_name = job_name.rsplit('.', 1)
# import the module and get the function
module = import_module(module_path)
func = getattr(module, function_name)
# run the function
try:
func(*job_args, **job_kwargs)
except Exception as e:
logger.exception('Failed to complete cronjob at %s err:%s' % (str(job), str(e)))
else:
olduid = os.getuid()
oldgid = os.getgid()
try:
job_name = job[1].split('byuser=')[0].strip()
username = job[1].split('byuser=')[1].strip()
if 'byuser' in job[1]:
ids = os.popen("grep -E '^%s' /etc/passwd|awk -F':' '{print $3,$4}'" % username).readlines()[0]
uid = int(ids.split(' ')[0])
gid = int(ids.split(' ')[1])
os.setgid(gid)
os.setuid(uid)
module_path, function_name = job_name.rsplit('.', 1)
module = import_module(module_path)
func = getattr(module, function_name)
func(*job_args, **job_kwargs)
os.setgid(oldgid)
os.setuid(olduid)
except Exception as e:
os.setgid(oldgid)
os.setuid(olduid)
logger.error('Failed to complete cronjob at %s err:%s' % (str(job), str(e)))

# if the LOCK_JOBS option is specified in settings
if self.settings.LOCK_JOBS:
Expand Down Expand Up @@ -208,4 +234,3 @@ def __get_job_by_hash(self, job_hash):
'No job with hash %s found. It seems the crontab is out of sync with your settings.CRONJOBS. '
'Run "python manage.py crontab add" again to resolve this issue!' % job_hash
)

0 comments on commit 8268886

Please sign in to comment.