Create a service running continuously

99 views
Skip to first unread message

Paul Gerrard

unread,
Feb 29, 2016, 4:36:23 AM2/29/16
to web2py-users
I have written some Python code to act as a chatbot working with Slack. All looks good so far. I could add it as a service to /etc/init.d etc and make it work at startup. However...

I want to enhance the service to access my MySQL database using the Web2py DAL. Now, I created a webservice to do this - but of course when run the Apache server eventually times out and I get a 500 error. Is there a simple way of creating a permanently running service that can access models, the DAL etc?

Now, in the web2py directory, I guess I could run:

python -M -S myapp/controller/function

But I need to add credentials to the command too? How do I do this?

Assuming there's a way to provide credentials through the shell, would a permanently running service created this way cause any other issues?

thanks, Paul.

Nico de Groot

unread,
Feb 29, 2016, 6:13:21 AM2/29/16
to web2py-users
Hi Paul,
You can consider using (stand-alone) DAL to access your database directly from your service. No need for a web service for db access. Your web2py app just uses the same db.
Nico de Groot

Ian Ryder

unread,
Mar 2, 2016, 3:42:38 PM3/2/16
to web2py-users
Sounds like you just need some sort of never ending loop (obviously you can put checks in the loop for stop requests and whatever other complexity you like / need).

You can pass arguments on the python command line. So for example you could do something like:

python web2py.py -S your_app -M -A 1 run_my_loop

In the example, 1 is the user record to use and then:

if sys.argv[2] == 'run_my_loop':
    this_user
= db_auth.auth_user[sys.argv[1]]

if not this_user:
   
raise Exception('...')
auth
.login_bare(this_user.email, this_user.password)
run
_my_loop()

Paul Gerrard

unread,
Mar 6, 2016, 12:17:15 PM3/6/16
to web...@googlegroups.com
Hi thanks for the suggestions. Sorry for not responding sooner. I figured a way of working and been working abroad for a few days...) so I haven't tried your suggestions.

What worked for me was I wrote a service that returned the contents of a table that define the bot commands and called it using requests in Python. The response contains the data as json. Then I connect to my Slack room and listen for commands that invoke my bot. the bot responds according to the commands received and looked-up responses read from the DB.

I suspect using the DAL directly could have achieved the same goal and also retain the DB connection to store stuff too if required - but right now I don't need to do that.

Happy to share my code if you have a similar need to drive a Slack bot. 



--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/XEbLuixLJUY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maurice Waka

unread,
Sep 15, 2018, 3:24:59 PM9/15/18
to web2py-users
Hi Paul.
I just came across this. Could you please sh ate the code. I have a similar problem of not getting a continuous loop.
Regards
Reply all
Reply to author
Forward
0 new messages