Browse Source

app_python: enabled rpc command to reload the script

- reload command performs the mod_init and child_init operations
- not tested yet
Daniel-Constantin Mierla 7 years ago
parent
commit
22975d721b

+ 47 - 39
src/modules/app_python/app_python_mod.c

@@ -55,6 +55,8 @@ PyObject *_sr_apy_handler_obj;
 
 char *dname = NULL, *bname = NULL;
 
+int _apy_process_rank = 0;
+
 PyThreadState *myThreadState;
 
 /** module parameters */
@@ -93,16 +95,14 @@ struct module_exports exports = {
 	child_init                      /* per-child init function */
 };
 
+
 /**
  *
  */
 static int mod_init(void)
 {
 	char *dname_src, *bname_src;
-
 	int i;
-	PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
-	PyThreadState *mainThreadState;
 
 	if(apy_sr_init_mod()<0) {
 		LM_ERR("failed to init the sr mod\n");
@@ -157,6 +157,48 @@ static int mod_init(void)
 		return -1;
 	}
 
+	if(apy_load_script()<0) {
+		pkg_free(dname_src);
+		pkg_free(bname_src);
+		LM_ERR("failed to load python script\n");
+		return -1;
+	}
+
+	pkg_free(dname_src);
+	pkg_free(bname_src);
+	return 0;
+}
+
+/**
+ *
+ */
+static int child_init(int rank)
+{
+	_apy_process_rank = rank;
+	return apy_init_script(rank);
+}
+
+/**
+ *
+ */
+static void mod_destroy(void)
+{
+	if (dname)
+		free(dname);	// dname was strdup'ed
+	if (bname)
+		free(bname);	// bname was strdup'ed
+
+	destroy_mod_Core();
+	destroy_mod_Ranks();
+	destroy_mod_Logger();
+	destroy_mod_Router();
+}
+
+int apy_load_script(void)
+{
+	PyObject *sys_path, *pDir, *pModule, *pFunc, *pArgs;
+	PyThreadState *mainThreadState;
+
 	Py_Initialize();
 	PyEval_InitThreads();
 	mainThreadState = PyThreadState_Get();
@@ -167,8 +209,6 @@ static int mod_init(void)
 	{
 		Py_XDECREF(format_exc_obj);
 		PyEval_ReleaseLock();
-		pkg_free(dname_src);
-		pkg_free(bname_src);
 		return -1;
 	}
 
@@ -181,8 +221,6 @@ static int mod_init(void)
 		python_handle_exception("mod_init");
 		Py_DECREF(format_exc_obj);
 		PyEval_ReleaseLock();
-		pkg_free(dname_src);
-		pkg_free(bname_src);
 		return -1;
 	}
 
@@ -194,8 +232,6 @@ static int mod_init(void)
 		python_handle_exception("mod_init");
 		Py_DECREF(format_exc_obj);
 		PyEval_ReleaseLock();
-		pkg_free(dname_src);
-		pkg_free(bname_src);
 		return -1;
 	}
 
@@ -208,8 +244,6 @@ static int mod_init(void)
 		python_handle_exception("mod_init");
 		Py_DECREF(format_exc_obj);
 		PyEval_ReleaseLock();
-		pkg_free(dname_src);
-		pkg_free(bname_src);
 		return -1;
 	}
 
@@ -220,8 +254,6 @@ static int mod_init(void)
 		python_handle_exception("mod_init");
 		Py_DECREF(format_exc_obj);
 		PyEval_ReleaseLock();
-		pkg_free(dname_src);
-		pkg_free(bname_src);
 		return -1;
 	}
 
@@ -232,14 +264,10 @@ static int mod_init(void)
 		python_handle_exception("mod_init");
 		Py_DECREF(format_exc_obj);
 		PyEval_ReleaseLock();
-		pkg_free(dname_src);
-		pkg_free(bname_src);
+
 		return -1;
 	}
 
-	pkg_free(dname_src);
-	pkg_free(bname_src);
-
 	pFunc = PyObject_GetAttrString(pModule, mod_init_fname.s);
 	Py_DECREF(pModule);
 
@@ -322,10 +350,7 @@ static int mod_init(void)
 	return 0;
 }
 
-/**
- *
- */
-static int child_init(int rank)
+int apy_init_script(int rank)
 {
 	PyObject *pFunc, *pArgs, *pValue, *pResult;
 	int rval;
@@ -436,23 +461,6 @@ static int child_init(int rank)
 
 	return rval;
 }
-
-/**
- *
- */
-static void mod_destroy(void)
-{
-	if (dname)
-		free(dname);	// dname was strdup'ed
-	if (bname)
-		free(bname);	// bname was strdup'ed
-
-	destroy_mod_Core();
-	destroy_mod_Ranks();
-	destroy_mod_Logger();
-	destroy_mod_Router();
-}
-
 /**
  *
  */

+ 29 - 3
src/modules/app_python/apy_kemi.c

@@ -42,7 +42,31 @@
 static int *_sr_python_reload_version = NULL;
 static int _sr_python_local_version = 0;
 extern str _sr_python_load_file;
+extern int _apy_process_rank;
 
+/**
+ * 
+ */
+
+int apy_reload_script(void)
+{
+	if(_sr_python_reload_version == NULL) {
+		return 0;
+	}
+	if(*_sr_python_reload_version == _sr_python_local_version) {
+		return 0;
+	}
+	if(apy_load_script()<0) {
+		LM_ERR("failed to load script file\n");
+		return -1;
+	}
+	if(apy_init_script(_apy_process_rank)<0) {
+		LM_ERR("failed to init script\n");
+		return -1;
+	}
+	_sr_python_local_version = *_sr_python_reload_version;
+	return 0;
+}
 /**
  *
  */
@@ -1119,7 +1143,6 @@ static const char* app_python_rpc_reload_doc[2] = {
 
 static void app_python_rpc_reload(rpc_t* rpc, void* ctx)
 {
-#if 0
 	int v;
 	void *vh;
 
@@ -1140,6 +1163,11 @@ static void app_python_rpc_reload(rpc_t* rpc, void* ctx)
 				_sr_python_local_version, v);
 	*_sr_python_reload_version += 1;
 
+	if(apy_reload_script()<0) {
+		rpc->fault(ctx, 500, "Reload failed");
+		return;	
+	}
+
 	if (rpc->add(ctx, "{", &vh) < 0) {
 		rpc->fault(ctx, 500, "Server error");
 		return;
@@ -1147,9 +1175,7 @@ static void app_python_rpc_reload(rpc_t* rpc, void* ctx)
 	rpc->struct_add(vh, "dd",
 			"old", v,
 			"new", *_sr_python_reload_version);
-#endif
 
-	rpc->fault(ctx, 500, "Not implemented");
 	return;
 }
 

+ 2 - 0
src/modules/app_python/apy_kemi.h

@@ -34,5 +34,7 @@ PyObject *sr_apy_kemi_exec_func(PyObject *self, PyObject *args, int idx);
 
 int apy_sr_init_mod(void);
 int app_python_init_rpc(void);
+int apy_load_script(void);
+int apy_init_script(int rank);
 
 #endif