Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Can't find the wsgi wrapper programatically #9

Closed
7 tasks done
dgonzo opened this issue Aug 13, 2018 · 7 comments
Closed
7 tasks done

Can't find the wsgi wrapper programatically #9

dgonzo opened this issue Aug 13, 2018 · 7 comments

Comments

@dgonzo
Copy link

dgonzo commented Aug 13, 2018

Checklist

  • Does your title concisely summarize the problem?
  • Did you include a minimal, reproducible example?
  • What OS are you using?
  • What version of molten are you using?
  • What did you do?
  • What did you expect would happen?
  • What happened?

What OS are you using?

macOS 10.13.6

What version of molten are you using?

molten==0.4.1

What did you do?

I'm using the serverless framework to deploy an API to AWS lambda. It has a function call -- serve -- that is expecting to find a wsgi app.

With flask this "just works" (i.e. default supported framework for most of these easy-of-use frameworks like zappa or serverless).

You can see that the serve function call isn't very complex other than some monkey patching.

# serve.py
def serve(cwd, app, port, host="localhost"):
    sys.path.insert(0, cwd)

    os.environ["IS_OFFLINE"] = "True"

    print(app)
    wsgi_fqn = app.rsplit(".", 1)
    print(wsgi_fqn)
    wsgi_fqn_parts = wsgi_fqn[0].rsplit("/", 1)
    print(wsgi_fqn_parts)
    if len(wsgi_fqn_parts) == 2:
        sys.path.insert(0, os.path.join(cwd, wsgi_fqn_parts[0]))
    wsgi_module = importlib.import_module(wsgi_fqn_parts[-1])
    print(wsgi_module)
    wsgi_app = getattr(wsgi_module, wsgi_fqn[1])

    # Attempt to force Flask into debug mode
    try:
        wsgi_app.debug = True
    except:  # noqa: E722
        pass

    serving.run_simple(
        host,
        int(port),
        wsgi_app,
        use_debugger=True,
        use_reloader=True,
        use_evalex=True,
        threaded=True,
    )

This works with flask. It also works with the hug but it's not strait forward. With that I have to point serverless explicitly at the wisgi "magic method": __hug_wsgi__.

Digging through hug I see where the http.server is added into the app singleton.

What did you expect would happen?

Ideally, the molten app instance would caugh up wsgi as simply as flask but even if it were like hug that would work. I'm open to other options.

I was able to get werkzeug to serve molten like this:

if __name__ == '__main__':
    from werkzeug import serving

    serving.run_simple('127.0.0.1', 8888, app)

What happened?

Right now I'm unable to find something suitable in molten to pass to pass into werkzeug so at present I can't host molten inside serverless.

@Bogdanp
Copy link
Owner

Bogdanp commented Aug 14, 2018

Sorry, I'm not sure I follow what exactly isn't working.

I'm not familiar with serverless, but I was able to follow the serverless-wsgi guide to implement this example: a61993d

It seems to work just fine with molten.

@dgonzo
Copy link
Author

dgonzo commented Aug 14, 2018

@Bogdanp that’s great. I’ll dig and see where I went wrong on this.

I’m loving the craftsmanship on molten. I’ll get this into production and have more feedback.

@dgonzo dgonzo closed this as completed Aug 14, 2018
@Bogdanp
Copy link
Owner

Bogdanp commented Aug 14, 2018

There does appear to be a bug here, though it's not related to the App object.

When I made my last comment, I had only tried it using the local server, but it looks like once you deploy the package to AWS, routing breaks down due to the way the request path is interpreted by Request objects (it shouldn't include the script name in the request path). That should be easy to fix and I'll try to do it tonight.

@Bogdanp
Copy link
Owner

Bogdanp commented Aug 14, 2018

Alright, that bug I mentioned is fixed in 0.4.2.

@joseditcrowd
Copy link

joseditcrowd commented Apr 4, 2019

Hi Folks, @dgonzo @Bogdanp were you able to fix this? I'm having some issues to deploy like openapi example (and the other molten examples too) to lambda with Zappa

the error received is like this

[1554393723522] 'NoneType' object is not callable

Thanks!

@dgonzo
Copy link
Author

dgonzo commented Apr 4, 2019

Happy to help. Could you put the entire trace here the error is generic enough that no immediate solution comes to mind.

@joseditcrowd
Copy link

joseditcrowd commented Apr 4, 2019

Thank you @dgonzo ! sure

Zappa tail
zappa_logs.txt

Zappa Status

Status for openapi-dev:
Lambda Versions: 2
Lambda Name: openapi-dev
Lambda ARN: arn:aws:lambda:us-east-1:430329019624:function:openapi-dev
Lambda Role ARN: arn:aws:iam::430329019624:role/service-role/lambdaRole
Lambda Handler: handler.lambda_handler
Lambda Code Size: 7834087
Lambda Version: $LATEST
Lambda Last Modified: 2019-04-04T17:50:55.176+0000
Lambda Memory Size: 512
Lambda Timeout: 30
Lambda Runtime: python3.6
Lambda VPC ID: None
Invocations (24h): 3
Errors (24h): 0
Error Rate (24h): 0.00%
API Gateway URL: https://bgjcdl9ql0.execute-api.us-east-1.amazonaws.com/dev
Domain URL: None Supplied
Num. Event Rules: 1
Event Rule Name: openapi-dev-zappa-keep-warm-handler.keep_warm_callback
Event Rule Schedule: rate(4 minutes)
Event Rule State: Enabled
Event Rule ARN: arn:aws:events:us-east-1:430329019624:rule/openapi-dev-zappa-keep-warm-handler.keep_warm_callback

Pip3 freeze

argcomplete==1.9.3
base58==0.2.4
boto3==1.9.128
botocore==1.12.128
certifi==2019.3.9
cfn-flip==1.2.0
chardet==3.0.4
Click==7.0
docutils==0.14
durationpy==0.5
future==0.16.0
hjson==3.0.1
idna==2.8
jmespath==0.9.3
kappa==0.6.0
lambda-packages==0.20.0
molten==0.7.4
mypy-extensions==0.4.1
placebo==0.9.0
python-dateutil==2.6.1
python-slugify==1.2.4
PyYAML==5.1
requests==2.21.0
s3transfer==0.2.0
six==1.12.0
toml==0.10.0
tqdm==4.19.1
troposphere==2.4.6
typing-extensions==3.7.2
typing-inspect==0.3.1
Unidecode==1.0.23
urllib3==1.24.1
virtualenv==16.4.3
Werkzeug==0.15.2
wsgi-request-logger==0.4.6
zappa==0.48.2

Zappa Config (as txt)

zappa_settings.txt

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants