Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: main_handler() takes 1 positional argument but 2 were given with @app.topic('mytopic') #95

Closed
amirbtb opened this issue Nov 11, 2021 · 5 comments · Fixed by #97

Comments

@amirbtb
Copy link

amirbtb commented Nov 11, 2021

Hello,

I created a topic in PubSub called mytopic and I tried to create a function triggered by a message in this topic. The deployment is successful but after I publish a message, I get a TypeError: dbt_handler() takes 1 positional argument but 2 were given. Here is the code of my function and the error traceback :

Function code

from goblet import Goblet

app = Goblet(function_name="goblet-pubsub")


@app.topic('mytopic')
def main_handler(data):
    app.log.info(data)
    return 

Error traceback

2021-11-11T15:20:13.051Z goblet-pubsub-topic-mytopic6gbe9k87mz0j 
Exception on / [POST] Traceback (most recent call last): 
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 2073, in wsgi_app response
    = self.full_dispatch_request() 
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1518, in full_dispatch_request 
    rv = self.handle_user_exception(e) 
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1516, in full_dispatch_request 
    rv = self.dispatch_request() 
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1502, in dispatch_request 
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) 
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/functions_framework/__init__.py", line 171, in view_func 
    function(data, context) TypeError: main_handler() takes 1 positional argument but 2 were given

Exception on / [POST] Traceback (most recent call last): 
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 2073, in wsgi_app response 
    = self.full_dispatch_request() 
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1518, in full_dispatch_request 
    rv = self.handle_user_exception(e) 
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1516, in full_dispatch_request 
    rv = self.dispatch_request() 
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1502, in dispatch_request 
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) 
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/functions_framework/__init__.py", line 171, in view_func 
    function(data, context) 

TypeError: main_handler() takes 1 positional argument but 2 were given

What am I supposed to provided to main_handler ?

Thank you !

@anovis
Copy link
Collaborator

anovis commented Nov 11, 2021

what is the entrypoint for the cloudfunction? I am thinking the entrypoint is pointing to main_handler directly which would require an event and context param.

cloudfunction recently had a breaking change (#88 ) which requires you to now wrap the app with goblet_entrypoint(app)

@amirbtb
Copy link
Author

amirbtb commented Nov 12, 2021

I used the following config.json :

{
    "cloudfunction": {
        "entryPoint":"main_handler",
        "availableMemoryMb": 256,
        "timeout": "30s"
    }
}

In a second attempt, I wrapped the app using goblet_entrypoint(app) and I removed the entryPoint key from the config.json. I still get the same error..

@amirbtb
Copy link
Author

amirbtb commented Nov 12, 2021

I did the following modifications but the deployment fails and throws a InvalidTargetTypeException

Function code

from goblet import Goblet, goblet_entrypoint

app = Goblet(function_name="goblet-pubsub")
goblet_entrypoint(app)

@app.topic('mytopic')
def main(data):
    app.log.info(data)
    return 

config.json

{
    "cloudfunction": {
        "availableMemoryMb": 256,
        "timeout": "30s"
    }
}

Error Traceback

Traceback (most recent call last):
File "/layers/google.python.pip/pip/bin/functions-framework", line 8, in <module>
sys.exit(_cli())
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/click/core.py", line 1128, in __call__
return self.main(*args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/click/core.py", line 1053, in main
rv = self.invoke(ctx)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/click/core.py", line 1395, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/click/core.py", line 754, in invoke
return __callback(*args, **kwargs)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/functions_framework/_cli.py", line 37, in _cli
app = create_app(target, source, signature_type)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/functions_framework/__init__.py", line 292, in create_app
function = _function_registry.get_user_function(source, source_module, target)
File "/layers/google.python.pip/pip/lib/python3.7/site-packages/functions_framework/_function_registry.py", line 52, in get_user_function
source=source, target=target, target_type=type(function)
functions_framework.exceptions
.
InvalidTargetTypeException
:
The function defined in file /workspace/main.py as app needs to be of type function. Got: invalid type <class 'goblet.app.Goblet'>
Function cannot be initialized. Error: function terminated. Recommended action: inspect logs for termination reason. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging

It looks similar to [Cloudfunction] Error type <class 'goblet.app.Goblet'> during deployment #88 but I made sure to wrap the app using goblet_entrypoint(app).

@anovis
Copy link
Collaborator

anovis commented Nov 12, 2021

ah sorry for the delay and you are correct. this is because of #88 and didnt catch this error in pubsub or storage triggers. have a fix and will release later today

anovis added a commit that referenced this issue Nov 12, 2021
set entrypoint to `goblet_entrypoint` instead of app because of #88 (closes #95)
@anovis
Copy link
Collaborator

anovis commented Nov 12, 2021

should be live in 0.5.1

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

Successfully merging a pull request may close this issue.

2 participants