Changeset 249:9afe2fc1bab1 for uwsgi.c
Legend:
- Unmodified
- Added
- Removed
-
uwsgi.c
r248 r249 462 462 int main (int argc, char *argv[], char *envp[]) { 463 463 464 uint64_t master_cycles = 0 ; 464 465 struct timeval check_interval = {.tv_sec = 1,.tv_usec = 0 }; 465 466 … … 520 521 int socket_type = 0; 521 522 socklen_t socket_type_len; 522 523 fprintf(stderr,"ARGV0 = %s %s\n", argv[0], argv[1]);524 523 525 524 /* anti signal bombing */ … … 605 604 {"nagios", no_argument, &nagios, 1}, 606 605 {"binary-path", required_argument, 0, LONG_ARGS_BINARY_PATH}, 606 {"proxy", required_argument, 0, LONG_ARGS_PROXY}, 607 {"proxy-node", required_argument, 0, LONG_ARGS_PROXY_NODE}, 608 {"proxy-max-connections", required_argument, 0, LONG_ARGS_PROXY_MAX_CONNECTIONS}, 607 609 {0, 0, 0, 0} 608 610 }; … … 1069 1071 socket_type_len = sizeof (int); 1070 1072 if (getsockopt (uwsgi.serverfd, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len)) { 1071 perror ("getsockopt()"); 1072 fprintf(stderr, "The -s/--socket option is missing and stdin is not a socket.\n"); 1073 exit (1); 1074 } 1075 1076 } 1073 //perror ("getsockopt()"); 1074 uwsgi.numproc = 0 ; 1075 } 1076 1077 #ifdef UWSGI_PROXY 1078 if (uwsgi.proxy_socket_name) { 1079 uwsgi.shared->proxy_pid = proxy_start(uwsgi.master_process); 1080 } 1081 #endif 1082 1083 1084 } 1085 1086 #ifdef UWSGI_PROXY 1087 if (uwsgi.numproc == 0 && (!uwsgi.proxy_socket_name || uwsgi.shared->proxy_pid <= 0)) { 1088 #else 1089 if (uwsgi.numproc == 0) { 1090 #endif 1091 fprintf(stderr, "The -s/--socket option is missing and stdin is not a socket.\n"); 1092 exit (1); 1093 } 1094 1077 1095 1078 1096 … … 1185 1203 #endif 1186 1204 1205 // is this a proxy only worker ? 1206 1207 if (!uwsgi.master_process && uwsgi.numproc == 0) { 1208 exit(0); 1209 } 1210 1187 1211 if (!uwsgi.single_interpreter) { 1188 1212 fprintf(stderr,"*** uWSGI is running in multiple interpreter mode !!! ***\n"); … … 1215 1239 1216 1240 #ifdef UWSGI_SPOOLER 1217 if (spool_dir != NULL ) {1241 if (spool_dir != NULL && uwsgi.numproc > 0) { 1218 1242 uwsgi.shared->spooler_pid = spooler_start (uwsgi.serverfd, uwsgi_module); 1219 1243 } … … 1302 1326 1303 1327 #endif 1328 1329 #ifdef UWSGI_PROXY 1330 if (uwsgi.proxy_socket_name && uwsgi.shared->proxy_pid > 0) { 1331 kill (uwsgi.shared->proxy_pid, SIGKILL); 1332 fprintf(stderr,"killed proxy with pid %d\n", uwsgi.shared->proxy_pid); 1333 } 1334 #endif 1304 1335 fprintf (stderr, "goodbye to uWSGI.\n"); 1305 1336 exit (0); … … 1311 1342 fprintf(stderr,"wait4() the spooler with pid %d...", uwsgi.shared->spooler_pid); 1312 1343 diedpid = waitpid(uwsgi.shared->spooler_pid, &waitpid_status, 0); 1344 fprintf(stderr,"done."); 1345 } 1346 #endif 1347 1348 #ifdef UWSGI_PROXY 1349 if (uwsgi.proxy_socket_name && uwsgi.shared->proxy_pid > 0) { 1350 kill (uwsgi.shared->proxy_pid, SIGKILL); 1351 fprintf(stderr,"wait4() the proxy with pid %d...", uwsgi.shared->proxy_pid); 1352 diedpid = waitpid(uwsgi.shared->proxy_pid, &waitpid_status, 0); 1313 1353 fprintf(stderr,"done."); 1314 1354 } … … 1381 1421 if (inet_ntop (AF_INET, &udp_client.sin_addr.s_addr, udp_client_addr, 16)) { 1382 1422 fprintf (stderr, "received udp packet of %d bytes from %s:%d\n", rlen, udp_client_addr, ntohs (udp_client.sin_port)); 1423 #ifdef UWSGI_SNMP 1383 1424 if (uwsgi.buffer[0] == 0x30) { 1384 1425 manage_snmp (uwsgi_poll.fd, (uint8_t *) uwsgi.buffer, rlen, &udp_client); 1385 1426 } 1427 #endif 1386 1428 } 1387 1429 else { … … 1394 1436 select (0, NULL, NULL, NULL, &check_interval); 1395 1437 } 1438 master_cycles++; 1396 1439 working_workers = 0; 1397 1440 blocking_workers = 0; … … 1425 1468 } 1426 1469 1470 // check for cluster nodes 1471 for(i=0;i<MAX_CLUSTER_NODES;i++) { 1472 struct uwsgi_cluster_node *ucn = &uwsgi.shared->nodes[i]; 1473 1474 if (ucn->name[0] != 0 && ucn->status == UWSGI_NODE_FAILED) { 1475 // should i retry ? 1476 if (master_cycles % ucn->errors == 0) { 1477 if (!uwsgi_ping_node(i, &wsgi_req)) { 1478 ucn->status = UWSGI_NODE_OK ; 1479 fprintf(stderr,"re-enabled cluster node %d/%s\n", i, ucn->name); 1480 } 1481 else { 1482 ucn->errors++; 1483 } 1484 } 1485 } 1486 } 1487 1427 1488 continue; 1428 1489 … … 1435 1496 uwsgi.shared->spooler_pid = spooler_start (uwsgi.serverfd, uwsgi_module); 1436 1497 continue; 1498 } 1499 } 1500 #endif 1501 1502 #ifdef UWSGI_SPOOLER 1503 /* reload the proxy (can be the only process running) */ 1504 if (uwsgi.proxy_socket_name && uwsgi.shared->proxy_pid > 0) { 1505 if (diedpid == uwsgi.shared->proxy_pid) { 1506 if (WIFEXITED (waitpid_status)) { 1507 if (WEXITSTATUS (waitpid_status) != UWSGI_END_CODE) { 1508 fprintf(stderr,"OOOPS the proxy is no more...trying respawn...\n"); 1509 uwsgi.shared->spooler_pid = proxy_start(1); 1510 continue; 1511 } 1512 } 1437 1513 } 1438 1514 } … … 1483 1559 if (uwsgi.mywid <= 0) { 1484 1560 #endif 1485 fprintf (stderr, "warning the died pid was not in the workers list. Probably you hit a BUG of uWSGI\n"); 1561 1562 #ifdef UWSGI_PROXY 1563 if (diedpid != uwsgi.shared->proxy_pid) { 1564 #endif 1565 fprintf (stderr, "warning the died pid was not in the workers list. Probably you hit a BUG of uWSGI\n"); 1566 #ifdef UWSGI_PROXY 1567 } 1568 #endif 1486 1569 } 1487 1570 } … … 2287 2370 #endif 2288 2371 2372 #ifdef UWSGI_PROXY 2373 pid_t proxy_start (has_master) { 2374 2375 pid_t pid ; 2376 2377 char *tcp_port = strchr (uwsgi.proxy_socket_name, ':'); 2378 2379 if (tcp_port == NULL) { 2380 uwsgi.proxyfd = bind_to_unix (uwsgi.proxy_socket_name, UWSGI_LISTEN_QUEUE, uwsgi.chmod_socket, uwsgi.abstract_socket); 2381 } 2382 else { 2383 uwsgi.proxyfd = bind_to_tcp (uwsgi.proxy_socket_name, UWSGI_LISTEN_QUEUE, tcp_port); 2384 tcp_port[0] = ':'; 2385 } 2386 2387 if (uwsgi.proxyfd < 0) { 2388 fprintf (stderr, "unable to create the server socket.\n"); 2389 exit (1); 2390 } 2391 2392 if (!has_master && uwsgi.numproc == 0) { 2393 uwsgi_proxy(uwsgi.proxyfd); 2394 // never here 2395 exit(1); 2396 } 2397 else { 2398 pid = fork() ; 2399 if (pid < 0) { 2400 perror("fork()"); 2401 exit(1); 2402 } 2403 else if (pid > 0) { 2404 close(uwsgi.proxyfd); 2405 return pid ; 2406 // continue with uWSGI spawn... 2407 } 2408 else { 2409 uwsgi_proxy(uwsgi.proxyfd); 2410 // never here 2411 exit(1); 2412 } 2413 } 2414 } 2415 #endif 2416 2289 2417 #ifdef UWSGI_SPOOLER 2290 2418 pid_t spooler_start (int serverfd, PyObject * uwsgi_module) { … … 2332 2460 uwsgi.binary_path = optarg; 2333 2461 break; 2462 #ifdef UWSGI_PROXY 2463 case LONG_ARGS_PROXY_NODE: 2464 uwsgi_cluster_add_node(optarg, 1); 2465 break; 2466 case LONG_ARGS_PROXY: 2467 uwsgi.proxy_socket_name = optarg; 2468 break; 2469 #endif 2334 2470 #ifdef UWSGI_ERLANG 2335 2471 case LONG_ARGS_ERLANG: … … 2544 2680 } 2545 2681 2682 2683 void uwsgi_cluster_add_node(char *nodename, int workers) { 2684 2685 int i ; 2686 struct uwsgi_cluster_node *ucn ; 2687 char *tcp_port ; 2688 2689 if (strlen(nodename) > 100) { 2690 fprintf(stderr,"invalid cluster node name %s\n", nodename); 2691 return; 2692 } 2693 2694 tcp_port = strchr (nodename, ':'); 2695 if (tcp_port == NULL) { 2696 fprintf(stdout,"invalid cluster node name %s\n", nodename); 2697 return ; 2698 } 2699 2700 for(i=0;i<MAX_CLUSTER_NODES; i++) { 2701 ucn = &uwsgi.shared->nodes[i] ; 2702 2703 if (ucn->name[0] == 0) { 2704 strcpy(ucn->name, nodename); 2705 ucn->workers = workers ; 2706 ucn->ucn_addr.sin_family = AF_INET ; 2707 ucn->ucn_addr.sin_port = htons(atoi(tcp_port+1)); 2708 tcp_port[0] = 0 ; 2709 if (nodename[0] == 0) { 2710 ucn->ucn_addr.sin_addr.s_addr = INADDR_ANY ; 2711 } 2712 else { 2713 fprintf(stderr,"%s\n", nodename); 2714 ucn->ucn_addr.sin_addr.s_addr = inet_addr(nodename); 2715 } 2716 2717 ucn->last_seen = time(NULL); 2718 2719 return ; 2720 } 2721 } 2722 2723 fprintf(stderr,"unable to add node %s\n", nodename); 2724 }
