better cron limits number of threads, thanks P.Pastori

This commit is contained in:
mdipierro
2019-05-22 21:44:42 -07:00
parent f258cbecee
commit 94af61e732
5 changed files with 269 additions and 156 deletions
+28 -5
View File
@@ -8,8 +8,10 @@ import unittest
import os
import shutil
import time
import sys
from gluon.newcron import Token, crondance, subprocess_count
from gluon.newcron import (Token, crondance, subprocess_count,
SimplePool, stopcron, reset)
from gluon.fileutils import create_app, write_file
@@ -28,13 +30,18 @@ def tearDownModule():
TEST_CRONTAB = """@reboot peppe **applications/%s/cron/test.py
""" % test_app_name
TEST_SCRIPT = """
TEST_SCRIPT1 = """
from os.path import join as pjoin
with open(pjoin(request.folder, 'private', 'cron_req'), 'w') as f:
f.write(str(request))
"""
TARGET = os.path.join(appdir, 'private', 'cron_req')
TEST_SCRIPT2 = """
import time
time.sleep(13)
"""
class TestCron(unittest.TestCase):
@@ -44,7 +51,7 @@ class TestCron(unittest.TestCase):
if os.path.exists(master):
os.unlink(master)
def test_Token(self):
def test_1_Token(self):
app_path = os.path.join(os.getcwd(), 'applications', test_app_name)
t = Token(path=app_path)
self.assertEqual(t.acquire(), t.now)
@@ -52,10 +59,10 @@ class TestCron(unittest.TestCase):
self.assertIsNone(t.acquire())
self.assertTrue(t.release())
def test_crondance(self):
def test_2_crondance(self):
base = os.path.join(appdir, 'cron')
write_file(os.path.join(base, 'crontab'), TEST_CRONTAB)
write_file(os.path.join(base, 'test.py'), TEST_SCRIPT)
write_file(os.path.join(base, 'test.py'), TEST_SCRIPT1)
if os.path.exists(TARGET):
os.unlink(TARGET)
crondance(os.getcwd(), 'hard', startup=True, apps=[test_app_name])
@@ -65,3 +72,19 @@ class TestCron(unittest.TestCase):
time.sleep(1)
time.sleep(1)
self.assertTrue(os.path.exists(TARGET))
def test_3_SimplePool(self):
base = os.path.join(appdir, 'cron')
write_file(os.path.join(base, 'test.py'), TEST_SCRIPT2)
w2p_path = os.path.join(os.getcwd(), 'web2py.py')
self.assertTrue(os.path.exists(w2p_path))
launcher = SimplePool(1)
cmd1 = [sys.executable, w2p_path,
'--cron_job', '--no_banner', '--no_gui', '--plain',
'-S', test_app_name, '-R', "applications/%s/cron/test.py" % test_app_name]
self.assertTrue(launcher(cmd1))
self.assertFalse(launcher(None))
time.sleep(1)
stopcron()
time.sleep(1)
reset()