source: branches/prototype-v0/zoo-project/zoo-kernel/sqlapi.c @ 860

Last change on this file since 860 was 860, checked in by djay, 6 years ago

Add status_code key to the lenv section to support returning a specific HTTP error code from the service code. Fix callback invocation to support inputs arrays at step 1 and 2. Fix issue with cpu usage. Fix issue with mapserver publication when an input is optional. Fix callback invocation at step 7 in case the service has failed on the HPC side.

  • Property svn:keywords set to Id
File size: 18.9 KB
RevLine 
[644]1/*
[822]2 * Authors : David Saggiorato
3 *           Gérald Fenoy
[652]4 *  Copyright 2015 GeoLabs SARL. All rights reserved.
[644]5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
[652]25#include "ogr_api.h"
26#include "ogrsf_frmts.h"
27#include "ogr_p.h"
[785]28#include "response_print.h"
[784]29#if GDAL_VERSION_MAJOR >= 2
30#include <gdal_priv.h>
31#endif
[652]32
[644]33#include "sqlapi.h"
34#include <fcgi_stdio.h>
35#include <stdlib.h>
36
37/**
[652]38 * Global GDALDataset pointer
[644]39 */
[784]40#if GDAL_VERSION_MAJOR >=2
41GDALDataset
42#else
43OGRDataSource
44#endif
[839]45 **zoo_DS = NULL;
[652]46
47/**
48 * Global OGRLayer pointer pointing to the lastest result set
49 */
50OGRLayer *zoo_ResultSet = NULL;
51
52/**
[822]53 * Create a GDAL / OGR string for connecting to a db backend defined in the
54 * key section.
[652]55 *
56 * @param conf the maps containing the setting of the main.cfg file
[822]57 * @param key the name of the section containing the connection setting
58 * @return the OGR connection string
[652]59 */
[822]60char* _createInitString(maps* conf,const char* key){
[652]61  char* res=NULL;
[822]62  char keywords[6][14]={
[652]63    "dbname",
64    "host",
65    "port",
66    "user",
[822]67    "password",
68    "active_schema"   
[652]69  };
70  int i=0;
[822]71  maps* cconf=getMaps(conf,key);
[854]72  if(cconf==NULL){
73    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
[839]74    return "-1";
[854]75  }
[652]76  int len=0;
[822]77  for(i=0;i<6;i++){
[652]78    map* tmp=getMap(cconf->content,keywords[i]);
79    if(tmp!=NULL){
80      if(res==NULL){
81        res=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+4)*sizeof(char));
82        sprintf(res,"%s='%s'",keywords[i],tmp->value);
83        len+=strlen(res);
84      }else{
85        char* res1=(char*)malloc((strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char));
86        sprintf(res1," %s='%s'",keywords[i],tmp->value);
87        res=(char*)realloc(res,(len+strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char));
88        memcpy(res+len,res1,(strlen(keywords[i])+strlen(tmp->value)+5)*sizeof(char));
89        len+=strlen(res1);
90        res[len]=0;
91        free(res1);
92      }
93    }
94  }
95  map* tmp=getMap(cconf->content,"type");
96  if(tmp!=NULL){
97    char* fres=(char*)malloc((strlen(res)+strlen(tmp->value)+2)*sizeof(char));
98    sprintf(fres,"%s:%s",tmp->value,res);
99    free(res);
100    return fres;
101  }
[644]102  return res;
103}
[652]104
105/**
[822]106 * Create a GDAL / OGR string for connecting to the db backend
[652]107 *
108 * @param conf the maps containing the setting of the main.cfg file
[822]109 * @return the OGR connection string
110 */
111char* createInitString(maps* conf){
112  return _createInitString(conf,"database");
113}
114
115/**
116 * Connect to a db backend.
117 *
118 * @param conf the maps containing the setting of the main.cfg file
[652]119 * @see createInitString
120 */
[839]121int _init_sql(maps* conf,const char* key){
[822]122  char* sqlInitString=_createInitString(conf,key);
[860]123#ifdef SQL_DEBUG
[854]124  fprintf(stderr,"Try to connect to: %s %s !\n",key,sqlInitString);
125  fflush(stderr); 
[860]126#endif
[839]127  if(strncmp(sqlInitString,"-1",2)==0)
128    return -1;
[652]129  OGRSFDriver *poDriver = NULL;
130  OGRRegisterAll();
[839]131  int zoo_ds_nb=0;
132  map* dsNb=getMapFromMaps(conf,"lenv","ds_nb");
133  if(dsNb==NULL){
134    setMapInMaps(conf,"lenv","ds_nb","1");
135  }else{
136    zoo_ds_nb=atoi(dsNb->value);
137    char* tmp=(char*)malloc(11*sizeof(char));
138    sprintf(tmp,"%d",zoo_ds_nb+1);
139    setMapInMaps(conf,"lenv","ds_nb",(const char*)tmp);
140    free(tmp);
141  }
142  if(zoo_DS==NULL)
143    zoo_DS=
[784]144#if GDAL_VERSION_MAJOR >= 2
[839]145      (GDALDataset**) malloc(sizeof(GDALDataset*))
146#else
147      (OGRDataSource**) malloc(sizeof(OGRDataSource*))
148#endif
149      ;
150  else
151    zoo_DS=     
152#if GDAL_VERSION_MAJOR >= 2
153      (GDALDataset**)realloc(zoo_DS,(zoo_ds_nb+1)*sizeof(GDALDataset*))
154#else
155      (OGRDataSource**)realloc(zoo_DS,(zoo_ds_nb+1)*sizeof(OGRDataSource*))
156#endif
157      ;
158 
159#if GDAL_VERSION_MAJOR >= 2
160  zoo_DS[zoo_ds_nb] = (GDALDataset*) GDALOpenEx( sqlInitString,
[784]161                                      GDAL_OF_UPDATE | GDAL_OF_VECTOR,
162                                      NULL, NULL, NULL );
163#else
[839]164  zoo_DS[zoo_ds_nb] = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver);
[784]165#endif
[839]166  if( zoo_DS[zoo_ds_nb] == NULL ){
[860]167#ifdef SQL_DEBUG
[652]168    fprintf(stderr,"sqlInitString: %s FAILED !\n",sqlInitString);
169    fflush(stderr);
170#endif
171    free(sqlInitString);
[839]172    setMapInMaps(conf,"lenv","dbIssue","1");
173    setMapInMaps(conf,"lenv","message",_("Failed to connect to the database backend"));
174    return -2;
[652]175  }
[860]176#ifdef SQL_DEBUG
[652]177  fprintf(stderr,"sqlInitString: %s SUCEED !\n",sqlInitString);
178  fflush(stderr);
179#endif
180  free(sqlInitString);
[839]181  zoo_ds_nb++;
182  return zoo_ds_nb;
[652]183}
184
185/**
[822]186 * Connect to the db backend.
187 *
188 * @param conf the maps containing the setting of the main.cfg file
189 * @see createInitString
190 */
[839]191int init_sql(maps* conf){
[822]192  return _init_sql(conf,"database");
193}
194
195/**
[652]196 * Close any connection to the db backend.
197 *
198 * @param conf the maps containing the setting of the main.cfg file
199 */
[839]200void close_sql(maps* conf,int cid){
[854]201  if( zoo_ResultSet != NULL ){
[839]202    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
[854]203    zoo_ResultSet=NULL;
204  }
205  if(zoo_DS!=NULL && zoo_DS[cid]!=NULL){
[784]206#if GDAL_VERSION_MAJOR >= 2
[839]207    GDALClose(zoo_DS[cid]);
[784]208#else
[839]209    OGRDataSource::DestroyDataSource( zoo_DS[cid] );
[784]210#endif
[839]211    zoo_DS[cid]=NULL;
[652]212  }
213}
214
215/**
216 * Call OGRCleanupAll.
217 *
218 */
219void end_sql(){
220  OGRCleanupAll();
221}
222
223/**
[822]224 * Fetch a tuple set by executing a SQL query to the Database Backend.
225 *
226 * @param conf the maps containing the setting of the main.cfg file
227 * @param sqlQuery the SQL query to run
228 * @return NULL in case of failure or an OGRLayer pointer if the query succeed.
229 */
[839]230OGRLayer *fetchSql(maps* conf,int cid,const char* sqlQuery){
[854]231  if(zoo_DS==NULL || zoo_DS[cid]==NULL)
[839]232    return NULL;
[822]233  OGRLayer *res=NULL;
[860]234#ifdef SQL_DEBUG
[839]235  fprintf(stderr,"************************* %s %s %d\n\n",sqlQuery,__FILE__,__LINE__);
236  fflush(stderr);
237#endif
238  res = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL);
[822]239  return res;
240}
241
[839]242void cleanFetchSql(maps* conf,int cid,OGRLayer *objects){
[822]243  if( objects != NULL ){
[839]244    zoo_DS[cid]->ReleaseResultSet( objects );
[822]245    objects=NULL;
246  }
247}
248
249/**
[652]250 * Execute a SQL query to the SQL Database Backend.
251 *
252 * @param conf the maps containing the setting of the main.cfg file
253 * @param sqlQuery the SQL query to run
254 * @return -1 in case of failure and 1 if the query succeed.
255 */
[839]256int execSql(maps* conf,int cid,const char* sqlQuery){
[822]257  int res=-1;
[854]258  if(zoo_DS == NULL || zoo_DS[cid]==NULL)
259    return -1;
[839]260  zoo_ResultSet = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL);
[652]261  if( zoo_ResultSet != NULL ){
[822]262    res=1;
[652]263  }
[822]264  return res;
[652]265}
266
267/**
268 * Clean any memory allocated by executing a request
269 *
270 * @param conf the maps containing the setting of the main.cfg file
271 * @param sqlQuery the SQL query to run
272 * @return -1 in case of failure and 1 if the query succeed.
273 */
[839]274void cleanUpResultSet(const maps* conf,int cid){
[652]275  if( zoo_ResultSet != NULL ){
[839]276    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
[652]277    zoo_ResultSet=NULL;
278  }
279}
280
[839]281#ifdef RELY_ON_DB
282int getCurrentId(maps* conf){
283  int res=0;
284  map* dsNb=getMapFromMaps(conf,"lenv","ds_nb");
285  if(dsNb!=NULL)
286    res=atoi(dsNb->value);
287  return res;
288}
289
[652]290/**
291 * Record a file stored during ZOO-Kernel execution
292 *
293 * @param conf the maps containing the setting of the main.cfg file
294 * @param filename the file's name
295 * @param type the type (Intput,Output,Response)
296 * @param name the maps containing the setting of the main.cfg file
297 */
298void recordStoredFile(maps* conf,const char* filename,const char* type,const char* name){
[839]299  int zoo_ds_nb=getCurrentId(conf);
[652]300  map *uusid=getMapFromMaps(conf,"lenv","usid");
301  map *schema=getMapFromMaps(conf,"database","schema");
302  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(uusid->value)+strlen(filename)+strlen(type)+(name!=NULL?strlen(name):2)+68+1)*sizeof(char));
303  if(name!=NULL)
304    sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s','%s');",schema->value,uusid->value,filename,type,name);
305  else
306    sprintf(sqlQuery,"INSERT INTO %s.files (uuid,filename,nature,name) VALUES ('%s','%s','%s',NULL);",schema->value,uusid->value,filename,type);
[839]307  execSql(conf,zoo_ds_nb-1,sqlQuery);
[734]308  free(sqlQuery);
[839]309  cleanUpResultSet(conf,zoo_ds_nb-1);
[652]310}
311
312/**
313 * Insert the reference tuple corresponding to the running service
314 *
315 * @param conf the maps containing the setting of the main.cfg file
316 */
317void recordServiceStatus(maps* conf){
[839]318  int zoo_ds_nb=getCurrentId(conf);
[652]319  map *sid=getMapFromMaps(conf,"lenv","sid");
320  map *osid=getMapFromMaps(conf,"lenv","osid");
321  map *uusid=getMapFromMaps(conf,"lenv","usid");
322  map *schema=getMapFromMaps(conf,"database","schema");
323  char *sqlQuery=(char*)malloc((strlen(schema->value)+
324                                strlen(uusid->value)+
325                                strlen(osid->value)+
[785]326                                strlen(sid->value)+
327                                strlen(wpsStatus[2])+66+1)*sizeof(char));
[652]328  sprintf(sqlQuery,
[785]329          "INSERT INTO %s.services (uuid,sid,osid,fstate)"
330          "VALUES ('%s','%s','%s','%s');",
[652]331          schema->value,
[785]332          uusid->value,
333          sid->value,
334          osid->value,
335          wpsStatus[2]);
[839]336  execSql(conf,zoo_ds_nb-1,sqlQuery);
[734]337  free(sqlQuery);
[839]338  cleanUpResultSet(conf,zoo_ds_nb-1);
[652]339}
340
341/**
342 * Store the content of the result file
343 *
344 * @param conf the maps containing the setting of the main.cfg file
345 * @param filename the file's name
346 */
347void recordResponse(maps* conf,char* filename){
[839]348  int zoo_ds_nb=getCurrentId(conf);
[652]349  FILE *file = fopen (filename, "rb");
350  fseek (file, 0, SEEK_END);
351  long flen = ftell (file);
352  fseek (file, 0, SEEK_SET);
353  char *tmps = (char *) malloc ((flen + 1) * sizeof (char));
354  fread (tmps, flen, 1, file);
355  tmps[flen]=0;
356  fclose(file);
357  map *sid=getMapFromMaps(conf,"lenv","usid");
358  map *schema=getMapFromMaps(conf,"database","schema");
[786]359  char *sqlQuery=(char*)malloc((strlen(schema->value)+flen+strlen(sid->value)+57+1)*sizeof(char));
[654]360  sprintf(sqlQuery,"INSERT INTO %s.responses (content,uuid) VALUES ($$%s$$,$$%s$$);",schema->value,tmps,sid->value);
[839]361  execSql(conf,zoo_ds_nb-1,sqlQuery);
[734]362  free(sqlQuery);
363  free(tmps);
[839]364  cleanUpResultSet(conf,zoo_ds_nb-1);
[652]365}
366
367/**
368 * Update the current status of the running service.
369 *
370 * @param conf the map containing the setting of the main.cfg file
371 * @return 0 on success, -2 if shmget failed, -1 if shmat failed
372 */
373int _updateStatus(maps* conf){
[839]374  int zoo_ds_nb=getCurrentId(conf);
[652]375  map *sid=getMapFromMaps(conf,"lenv","usid");
376  map *p=getMapFromMaps(conf,"lenv","status");
377  map *msg=getMapFromMaps(conf,"lenv","message");
378  map *schema=getMapFromMaps(conf,"database","schema");
379  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(msg->value)+strlen(p->value)+strlen(sid->value)+64+1)*sizeof(char));
380  sprintf(sqlQuery,"UPDATE %s.services set status=$$%s$$,message=$$%s$$ where uuid=$$%s$$;",schema->value,p->value,msg->value,sid->value);
[860]381  if( zoo_ds_nb==
382#ifdef META_DB
383      1
384#else
385      0
386#endif     
387     ){
[652]388    init_sql(conf);
[839]389    zoo_ds_nb++;
390  }
[854]391  execSql(conf,0,sqlQuery);
392  cleanUpResultSet(conf,0);
393  close_sql(conf,0);
[734]394  free(sqlQuery);
[854]395  setMapInMaps(conf,"lenv","ds_nb","0");
[652]396  return 0;
397}
398
399/**
400 * Get the ongoing status of a running service
401 *
402 * @param conf the maps containing the setting of the main.cfg file
403 * @param pid the service identifier (usid key from the [lenv] section)
404 * @return the reported status char* (MESSAGE|POURCENTAGE)
405 */
406char* _getStatus(maps* conf,char* pid){
[839]407  int zoo_ds_nb=getCurrentId(conf);
[850]408  int created=-1;
[652]409  map *schema=getMapFromMaps(conf,"database","schema");
410  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
411  sprintf(sqlQuery,"select status||'|'||message from %s.services where uuid=$$%s$$;",schema->value,pid);
[839]412  if( zoo_ds_nb==
413#ifdef META_DB
414      1
415#else
416      0
417#endif
418      ){
[652]419    init_sql(conf);
[839]420    zoo_ds_nb++;
[850]421    created=1;
[839]422  }
423  execSql(conf,zoo_ds_nb-1,sqlQuery);
[652]424  OGRFeature  *poFeature = NULL;
425  const char *tmp1;
426  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
427    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
428      if( poFeature->IsFieldSet( iField ) ){
429        tmp1=strdup(poFeature->GetFieldAsString( iField ));
430      }
431      else
432        tmp1=NULL;
433    }
434    OGRFeature::DestroyFeature( poFeature );
435  }
[839]436  cleanUpResultSet(conf,zoo_ds_nb-1);
[734]437  free(sqlQuery);
[652]438  return (char*)tmp1;
439}
440
441/**
442 * Read the cache file of a running service
443 *
444 * @param conf the maps containing the setting of the main.cfg file
445 * @param pid the service identifier (usid key from the [lenv] section)
446 * @return the reported status char* (temporary/final result)
447 */
448char* _getStatusFile(maps* conf,char* pid){
[839]449  int zoo_ds_nb=getCurrentId(conf);
[652]450  map *schema=getMapFromMaps(conf,"database","schema");
[854]451  OGRFeature  *poFeature = NULL;
452  const char *tmp1=NULL;
453  int hasRes=-1;
[654]454  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+82+1)*sizeof(char));
455  sprintf(sqlQuery,
456          "select content from %s.responses where uuid=$$%s$$"
457          " order by creation_time desc limit 1",schema->value,pid);
[839]458  if( zoo_ds_nb==
459#ifdef META_DB
460      1
461#else
462      0
463#endif
464      ){
[652]465    init_sql(conf);
[839]466    zoo_ds_nb++;
467  }
468  execSql(conf,zoo_ds_nb-1,sqlQuery);
469  if(zoo_ResultSet!=NULL){
470      while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
471        for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
472          if( poFeature->IsFieldSet( iField ) ){
473            tmp1=strdup(poFeature->GetFieldAsString( iField ));
474            hasRes=1;
475          }
476          else
477            tmp1=NULL;
478        }
479        OGRFeature::DestroyFeature( poFeature );
[652]480      }
481  }
482  if(hasRes<0)
483    tmp1=NULL;
[854]484  cleanUpResultSet(conf,zoo_ds_nb-1);
[734]485  free(sqlQuery);
[652]486  return (char*)tmp1;
487}
488
489/**
[654]490 * Delete a service reference from the database.
491 *
492 * @param conf the map containing the setting of the main.cfg file
493 * @param pid the service identifier (usid key from the [lenv] section)
494 */
495void removeService(maps* conf,char* pid){
[839]496  int zoo_ds_nb=getCurrentId(conf);
[654]497  map *schema=getMapFromMaps(conf,"database","schema");
498  char *sqlQuery=(char*)
499    malloc((strlen(pid)+strlen(schema->value)+38+1)
500           *sizeof(char));
[839]501  if( zoo_ds_nb==
502#ifdef META_DB
503      1
504#else
505      0
506#endif
507      ){
[654]508    init_sql(conf);
[839]509    zoo_ds_nb++;
510  }
[654]511  sprintf(sqlQuery,
512          "DELETE FROM %s.services where uuid=$$%s$$;",
513          schema->value,pid);
[839]514  execSql(conf,zoo_ds_nb-1,sqlQuery);
515  cleanUpResultSet(conf,zoo_ds_nb-1);
516  close_sql(conf,zoo_ds_nb-1);
[734]517  free(sqlQuery);
[654]518  end_sql();
519}
520
521/**
[652]522 * Stop handling status repport.
523 *
524 * @param conf the map containing the setting of the main.cfg file
525 */
526void unhandleStatus(maps* conf){
[839]527  int zoo_ds_nb=getCurrentId(conf);
[654]528  map *schema=getMapFromMaps(conf,"database","schema");
[652]529  map *sid=getMapFromMaps(conf,"lenv","usid");
[654]530  map *fstate=getMapFromMaps(conf,"lenv","fstate");
531  char *sqlQuery=(char*)malloc((strlen(sid->value)+
532                                strlen(schema->value)+
533                                (fstate!=NULL?
534                                 strlen(fstate->value):
535                                 6)
536                                +66+1)*sizeof(char));
537  sprintf(sqlQuery,
538          "UPDATE %s.services set end_time=now(), fstate=$$%s$$"
539          " where uuid=$$%s$$;",
540          schema->value,(fstate!=NULL?fstate->value:"Failed"),sid->value);
[839]541  execSql(conf,zoo_ds_nb-1,sqlQuery);
542  cleanUpResultSet(conf,zoo_ds_nb-1);
[851]543  //close_sql(conf,zoo_ds_nb-1);
[734]544  free(sqlQuery);
[854]545  //end_sql();
[652]546}
547
[654]548/**
549 * Read the sid identifier attached of a service if any
550 *
551 * @param conf the maps containing the setting of the main.cfg file
552 * @param pid the service identifier (usid key from the [lenv] section)
553 * @return the sid value
554 */
555char* getStatusId(maps* conf,char* pid){
[839]556  int zoo_ds_nb=getCurrentId(conf);
[654]557  map *schema=getMapFromMaps(conf,"database","schema");
558  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
559  sprintf(sqlQuery,
560          "select osid from %s.services where uuid=$$%s$$",
561          schema->value,pid);
[839]562  if( zoo_ds_nb==0 ){
[654]563    init_sql(conf);
[839]564    zoo_ds_nb++;
565  }
[854]566  if(execSql(conf,zoo_ds_nb-1,sqlQuery)<0)
567    return NULL;
[654]568  OGRFeature  *poFeature = NULL;
569  const char *tmp1;
570  int hasRes=-1;
571  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
572    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
573      if( poFeature->IsFieldSet( iField ) ){
574        tmp1=zStrdup(poFeature->GetFieldAsString( iField ));
575        hasRes=1;
576        break;
577      }
578    }
579    OGRFeature::DestroyFeature( poFeature );
580  }
581  if(hasRes<0)
582    tmp1=NULL;
[734]583  free(sqlQuery);
[839]584  cleanUpResultSet(conf,zoo_ds_nb-1);
[654]585  return (char*)tmp1;
586}
587
588/**
589 * Read the Result file (.res).
590 *
591 * @param conf the maps containing the setting of the main.cfg file
592 * @param pid the service identifier (usid key from the [lenv] section)
593 */
594void readFinalRes(maps* conf,char* pid,map* statusInfo){
[839]595  int zoo_ds_nb=getCurrentId(conf);
[654]596  map *schema=getMapFromMaps(conf,"database","schema");
597  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+58+1)*sizeof(char));
598  sprintf(sqlQuery,
599          "select fstate from %s.services where uuid=$$%s$$",
600          schema->value,pid);
601  if( zoo_DS == NULL )
602    init_sql(conf);
[839]603  execSql(conf,zoo_ds_nb-1,sqlQuery);
[654]604  OGRFeature  *poFeature = NULL;
605  int hasRes=-1;
606  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
607    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
608      if( poFeature->IsFieldSet( iField ) ){
609        addToMap(statusInfo,"Status",poFeature->GetFieldAsString( iField ));
610        hasRes=1;
611        break;
612      }
613    }
614    OGRFeature::DestroyFeature( poFeature );
615  }
[851]616  cleanUpResultSet(conf,zoo_ds_nb-1);
[654]617  if(hasRes<0)
618    addToMap(statusInfo,"Status","Failed");
[734]619  free(sqlQuery);
[654]620  return;
621}
622
623/**
624 * Check if a service is running.
625 *
626 * @param conf the maps containing the setting of the main.cfg file
627 * @param pid the unique service identifier (usid from the lenv section)
628 * @return 1 in case the service is still running, 0 otherwise
629 */
630int isRunning(maps* conf,char* pid){
631  int res=0;
[839]632  int zoo_ds_nb=getCurrentId(conf);
[654]633  map *schema=getMapFromMaps(conf,"database","schema");
634  char *sqlQuery=(char*)malloc((strlen(schema->value)+strlen(pid)+73+1)*sizeof(char));
635  sprintf(sqlQuery,"select count(*) as t from %s.services where uuid=$$%s$$ and end_time is null;",schema->value,pid);
[839]636  if( zoo_ds_nb == 0 ){
[654]637    init_sql(conf);
[839]638    zoo_ds_nb++;
639  }
640  execSql(conf,zoo_ds_nb-1,sqlQuery);
[654]641  OGRFeature  *poFeature = NULL;
642  const char *tmp1;
643  while( (poFeature = zoo_ResultSet->GetNextFeature()) != NULL ){
644    for( int iField = 0; iField < poFeature->GetFieldCount(); iField++ ){
645      if( poFeature->IsFieldSet( iField ) && 
646          atoi(poFeature->GetFieldAsString( iField ))>0 ){
647        res=1;
648        break;
649      }
650    }
651    OGRFeature::DestroyFeature( poFeature );
652  }
[839]653  cleanUpResultSet(conf,zoo_ds_nb-1);
[734]654  free(sqlQuery);
[654]655  return res;
656}
657
[652]658#endif
Note: See TracBrowser for help on using the repository browser.

Search

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png