Skip to content

Commit

Permalink
change log levle to error when load lib job failed (#172)
Browse files Browse the repository at this point in the history
* change log level when load lib jobs
Co-authored-by: youcai <youcai.li@shanbay.com>
  • Loading branch information
HeathLee authored Sep 17, 2020
1 parent b906ef9 commit 8064405
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
37 changes: 19 additions & 18 deletions sea/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ def __init__(self, *args, **kwargs):


class JobManager:

def __init__(self):
self._jobs = {}

def job(self, name, inapp=True, env='development', proxy=False,
*args, **kwargs):
def job(
self, name, inapp=True, env="development", proxy=False, *args, **kwargs
):
def wrapper(func):
func.parser = JobOption(*args, **kwargs)
func.proxy = proxy
func.inapp = inapp
func.env = env
self._jobs[name] = func
return func

return wrapper

def option(self, *args, **kwargs):
def wrapper(func):
opts = getattr(func, 'opts', [])
opts = getattr(func, "opts", [])
opts.append(JobOption(*args, **kwargs))
func.opts = opts
return func

return wrapper

@property
Expand All @@ -55,30 +57,29 @@ def _load_jobs():
sys.path.append(path)

# load builtin
import_string('sea.cmds')
import_string("sea.cmds")

# load lib jobs
for ep in pkg_resources.iter_entry_points('sea.jobs'):
for ep in pkg_resources.iter_entry_points("sea.jobs"):
try:
ep.load()
except Exception as e:
logger = logging.getLogger('sea.cmd')
logger.debug(
'error has occurred during pkg loading: {}'.format(e))
logger = logging.getLogger("sea.cmd")
logger.error("error has occurred during pkg loading: {}".format(e))

# load app jobs
appjobs = os.path.join(path, 'jobs')
appjobs = os.path.join(path, "jobs")
if os.path.exists(appjobs):
import_string('jobs')
import_string("jobs")
for m in os.listdir(appjobs):
if m != '__init__.py' and m.endswith('.py'):
import_string('jobs.{}'.format(m[:-3]))
if m != "__init__.py" and m.endswith(".py"):
import_string("jobs.{}".format(m[:-3]))


def _build_parser(subparsers):
for name, handler in jobm.jobs.items():
parser = handler.parser
opts = getattr(handler, 'opts', [])
opts = getattr(handler, "opts", [])
p = subparsers.add_parser(name, *parser.args, **parser.kwargs)
for opt in opts:
p.add_argument(*opt.args, **opt.kwargs)
Expand All @@ -87,11 +88,11 @@ def _build_parser(subparsers):

def _run(root):
# show help message when user run sea command directly
args = sys.argv[1:] or ['--help']
args = sys.argv[1:] or ["--help"]
known, argv = root.parse_known_args(args)
kwargs = vars(known)
handler = kwargs.pop('handler')
os.environ.setdefault('SEA_ENV', handler.env)
handler = kwargs.pop("handler")
os.environ.setdefault("SEA_ENV", handler.env)
if handler.inapp:
create_app()
try:
Expand All @@ -103,7 +104,7 @@ def _run(root):


def main():
root = argparse.ArgumentParser('sea')
root = argparse.ArgumentParser("sea")
subparsers = root.add_subparsers()

_load_jobs()
Expand Down
14 changes: 12 additions & 2 deletions tests/test_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,23 @@ def f2():

return f2

class FailedEntryPoint:
def load(self):
raise Exception("Failed entry point")

def new_entry_iter(name):
return [EntryPoint()]
return [EntryPoint(), FailedEntryPoint()]

with mock.patch("pkg_resources.iter_entry_points", new=new_entry_iter):
mock_logger = mock.Mock()
with mock.patch(
"pkg_resources.iter_entry_points", new=new_entry_iter
), mock.patch("logging.getLogger", return_value=mock_logger):
sys.argv = "sea xyz".split()
assert cli.main() is None
assert app.config.get("XYZ") == "hello"
mock_logger.error.assert_called_with(
"error has occurred during pkg loading: Failed entry point"
)


def test_main():
Expand Down

0 comments on commit 8064405

Please sign in to comment.