Changeset 654 for trunk/zoo-project/zoo-kernel/server_internal.c
- Timestamp:
- Jun 15, 2015, 1:47:59 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/zoo-project/zoo-kernel/server_internal.c
r653 r654 24 24 25 25 #include "server_internal.h" 26 #include "service_internal.h" 26 27 #include "response_print.h" 27 28 #include "mimetypes.h" … … 785 786 } 786 787 787 788 788 #include <dirent.h> 789 #ifndef RELY_ON_DB 790 /** 791 * Read the Result file (.res). 792 * 793 * @param conf the maps containing the setting of the main.cfg file 794 * @param pid the service identifier (usid key from the [lenv] section) 795 */ 796 void readFinalRes(maps* conf,char* pid,map* statusInfo){ 797 map* r_inputs = getMapFromMaps (conf, "main", "tmpPath"); 798 char* fbkpid = 799 (char *) 800 malloc ((strlen (r_inputs->value) + strlen (pid) + 7) * sizeof (char)); 801 sprintf (fbkpid, "%s/%s.res", r_inputs->value, pid); 802 struct stat file_status; 803 int istat = stat (fbkpid, &file_status); 804 if (istat == 0 && file_status.st_size > 0) 805 { 806 maps *res = (maps *) malloc (MAPS_SIZE); 807 conf_read (fbkpid, res); 808 map* status=getMapFromMaps(res,"status","status"); 809 addToMap(statusInfo,"Status",status->value); 810 freeMaps(&res); 811 free(res); 812 } 813 else 814 addToMap(statusInfo,"Status","Failed"); 815 free(fbkpid); 816 } 817 818 /** 819 * Check if a service is running. 820 * 821 * @param conf the maps containing the setting of the main.cfg file 822 * @param pid the unique service identifier (usid from the lenv section) 823 * @return 1 in case the service is still running, 0 otherwise 824 */ 825 int isRunning(maps* conf,char* pid){ 826 int res=0; 827 map* r_inputs = getMapFromMaps (conf, "main", "tmpPath"); 828 char* fbkpid = 829 (char *) 830 malloc ((strlen (r_inputs->value) + strlen (pid) + 7) * sizeof (char)); 831 sprintf (fbkpid, "%s/%s.pid", r_inputs->value, pid); 832 FILE* f0 = fopen (fbkpid, "r"); 833 if(f0!=NULL){ 834 fclose(f0); 835 res=1; 836 } 837 free(fbkpid); 838 return res; 839 } 840 #else 841 #include "sqlapi.h" 842 #endif 843 844 /** 845 * Run GetStatus requests. 846 * 847 * @param conf the maps containing the setting of the main.cfg file 848 * @param pid the service identifier (usid key from the [lenv] section) 849 * @param req the request (GetStatus / GetResult) 850 */ 851 void runGetStatus(maps* conf,char* pid,char* req){ 852 map* r_inputs = getMapFromMaps (conf, "main", "tmpPath"); 853 char *sid=getStatusId(conf,pid); 854 if(sid==NULL){ 855 errorException (conf, _("The JobID from the request does not match any of the Jobs running on this server"), 856 "NoSuchJob", pid); 857 }else{ 858 map* statusInfo=createMap("JobID",pid); 859 if(isRunning(conf,pid)>0){ 860 if(strncasecmp(req,"GetResult",strlen(req))==0){ 861 errorException (conf, _("The result for the requested JobID has not yet been generated. "), 862 "ResultNotReady", pid); 863 return; 864 } 865 else 866 if(strncasecmp(req,"GetStatus",strlen(req))==0){ 867 addToMap(statusInfo,"Status","Running"); 868 char* tmpStr=_getStatus(conf,pid); 869 if(tmpStr!=NULL && strncmp(tmpStr,"-1",2)!=0){ 870 char *tmpStr1=strdup(tmpStr); 871 char *tmpStr0=strdup(strstr(tmpStr,"|")+1); 872 free(tmpStr); 873 tmpStr1[strlen(tmpStr1)-strlen(tmpStr0)-1]='\0'; 874 addToMap(statusInfo,"PercentCompleted",tmpStr1); 875 addToMap(statusInfo,"Message",tmpStr0); 876 free(tmpStr0); 877 free(tmpStr1); 878 } 879 } 880 } 881 else{ 882 if(strncasecmp(req,"GetResult",strlen(req))==0){ 883 char* result=_getStatusFile(conf,pid); 884 if(result!=NULL){ 885 char *encoding=getEncoding(conf); 886 fprintf(stdout,"Content-Type: text/xml; charset=%s\r\nStatus: 200 OK\r\n\r\n",encoding); 887 fprintf(stdout,"%s",result); 888 fflush(stdout); 889 freeMap(&statusInfo); 890 free(statusInfo); 891 return; 892 }else{ 893 errorException (conf, _("The result for the requested JobID has not yet been generated. "), 894 "ResultNotReady", pid); 895 freeMap(&statusInfo); 896 free(statusInfo); 897 return; 898 } 899 }else 900 if(strncasecmp(req,"GetStatus",strlen(req))==0){ 901 readFinalRes(conf,pid,statusInfo); 902 char* tmpStr=_getStatus(conf,pid); 903 if(tmpStr!=NULL && strncmp(tmpStr,"-1",2)!=0){ 904 char *tmpStr1=strdup(tmpStr); 905 char *tmpStr0=strdup(strstr(tmpStr,"|")+1); 906 free(tmpStr); 907 tmpStr1[strlen(tmpStr1)-strlen(tmpStr0)-1]='\0'; 908 addToMap(statusInfo,"PercentCompleted",tmpStr1); 909 addToMap(statusInfo,"Message",tmpStr0); 910 free(tmpStr0); 911 free(tmpStr1); 912 } 913 } 914 } 915 printStatusInfo(conf,statusInfo,req); 916 freeMap(&statusInfo); 917 free(statusInfo); 918 } 919 return; 920 } 921 922 /** 923 * Run Dismiss requests. 924 * 925 * @param conf the maps containing the setting of the main.cfg file 926 * @param pid the service identifier (usid key from the [lenv] section) 927 */ 928 void runDismiss(maps* conf,char* pid){ 929 map* r_inputs = getMapFromMaps (conf, "main", "tmpPath"); 930 char *sid=getStatusId(conf,pid); 931 if(sid==NULL){ 932 errorException (conf, _("The JobID from the request does not match any of the Jobs running on this server"), 933 "NoSuchJob", pid); 934 }else{ 935 // We should send the Dismiss request to the target host if it differs 936 char* fbkpid = 937 (char *) 938 malloc ((strlen (r_inputs->value) + strlen (pid) + 7) * sizeof (char)); 939 sprintf (fbkpid, "%s/%s.pid", r_inputs->value, pid); 940 FILE* f0 = fopen (fbkpid, "r"); 941 if(f0!=NULL){ 942 long flen; 943 char *fcontent; 944 fseek (f0, 0, SEEK_END); 945 flen = ftell (f0); 946 fseek (f0, 0, SEEK_SET); 947 fcontent = (char *) malloc ((flen + 1) * sizeof (char)); 948 fread(fcontent,flen,1,f0); 949 fcontent[flen]=0; 950 fclose(f0); 951 kill(atoi(fcontent),SIGKILL); 952 free(fcontent); 953 } 954 free(fbkpid); 955 struct dirent *dp; 956 DIR *dirp = opendir(r_inputs->value); 957 char fileName[1024]; 958 int hasFile=-1; 959 if(dirp!=NULL){ 960 while ((dp = readdir(dirp)) != NULL){ 961 #ifdef DEBUG 962 fprintf(stderr,"File : %s searched : %s\n",dp->d_name,tmp); 963 #endif 964 if(strstr(dp->d_name,pid)!=0){ 965 sprintf(fileName,"%s/%s",r_inputs->value,dp->d_name); 966 if(unlink(fileName)!=0){ 967 errorException (conf, _("The job cannot be removed, a file cannot be removed"), 968 "NoApplicableCode", NULL); 969 return; 970 } 971 } 972 } 973 } 974 #ifdef RELY_ON_DB 975 removeService(conf,pid); 976 #endif 977 map* statusInfo=createMap("JobID",pid); 978 addToMap(statusInfo,"Status","Dismissed"); 979 printStatusInfo(conf,statusInfo,"Dismiss"); 980 free(statusInfo); 981 } 982 return; 983 }
Note: See TracChangeset
for help on using the changeset viewer.