External Server Client Sockets

Hi Team,

Im struggling with something that im hoping someone can lead me in the right direction.

I have created an app with a schedule hook. The py script im running needs to listen on a server socket I got from my service provider that receives incoming ussd messages in xml format. I have this working but my challenge is that the service providers connection timesout after a hour and does not send any disconnect message so the socket remains open but dead so the python code now keeps running in this state.

Even if I set the schedule to ‘all’ its not stopping the script and rerunning.

What I would like to do i be able to stop the client socket and re-run or reconnect. Or maybe some guideline on how to better structure this code.

all this is all server code cause im planning to extend this app so it can interact with erpnext so my customer can use ussd messaging to get data from erpnext.

Any feedback is most welcomed…

thnx

regards

here is my first attempt for a newbie python developer ;), in case someone wants to look at the code

I have removed any sensitive data with *.

regards

@hpema108 good start. Really hard to debug such stuff without having the full access to APIs

@rmetha I think the API is not important cause im trying to understand how sockets work in ERPNext. Even if we use simple sockets to test with what im trying to achieve is that when the next schedule need to run my code I need to figure out how to kill the previous running schedule so that we can reconnect and get the socket active again.

Also, if I use threads with that work, trying to study that now and I see that I can run threads as daemons as well. Im sure I can write a separate python app for what im trying to achieve and just use the erpnext rest api to access data but I wanted to have all running in erpnext if possible.

my procedure is:

  1. On the hour start the ussd.py script
  2. The script needs to spawn other processes depending on user input as i dont want to run long processes in ussd.py - this should only receive messages and then pass to a message process - or maybe I can use a queue or something that another py file can be listening on
  3. Scheduler should re-run ussd.py and kill and sockets if active then re-connect.
  4. what would happen is I run command sys.exit()? Cause if this does not stop frappe to stop then I can have 2 schedules, one will be uusd.py and the other will set a flag in the database that I can read during the “While 1:” to exit ussd.py then the scheduler will start it up on next run. I would prefer if I can immediately run it in stead of waiting for the schedule.

This what I want to achieve, just need some ideas on how I can go about trying this. Hope im clear.

thanking you in advance for you help.

regards

How about running your script outside frappe and use cron and execute it using bench execute you have more control.

Hi @ccfiel,

Sorry for my ignorance, noob here…but is it possible to give a simple sample code or a how-to that I can use to build on top?

regards

just make a new script and run it with python thats it.

Use frappe white list methods to communicate with erpnext, read the docs for this one.

@hpema108, use the “frappe-client” in GitHub - frappe/frappe-client: Python library to use Frappe API to work around the API

@hpema108 its better to share you code for us we can help. Show your hooks and your task

I’m going to get the test server I have created for testing so that we can all work on the client code. Ive put the code I have on pastebin, but will do so again with the connection to this test server.

I have a full system running but on Windows and now I’m trying to port to erpnext.

Thanks for all your assistance.

Regards

Hi all

I am getting somewhere now…I know I said I was going to create a test but I could not get the windev code to work with my python code…so scrapped that idea.

where am I now.

  1. created a stand alone python script that almost does what I want
  2. Got it running using supervisor
  3. created an app and added to erpnext
  4. created a settings page to manage state variables
  5. Got the script to talk to erpnext using frappe_client (GitHub - frappe/frappe-client: Python library to use Frappe API) - BUT

how do I get the frappe_client to read the settings page i created. I read another place on the forum that the data sits in a Singles table in the database, checked there and its there. But when I tried to use frappe_client I get an error saying there is no doctype named Singles.

Any idea?

regards

Hi All,

Got everything working nicely except how to restart my supervisor application.

Im running:
os.system(“supervisorctl stop ussd”)
os.system(“supervisorctl start ussd”)

to restart using an event hoot with ‘all’, so every 3 minutes there is a check. But Im suspecting that when frappe runs its not using super user and these commands are not being executed.

Any idea how I can restart supervisor using the python hook that I created?

regards

How about putting it to cron? For more control

Hi all,

Got it all working with using python threading, frappeclient, rest and supervisor.

Now next step is to do message processing. Thnx for all your guys help.

If there are any leads regarding creating heirarchies will be great.

regards