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
Line 
1/*
2 * Authors : David Saggiorato
3 *           Gérald Fenoy
4 *  Copyright 2015 GeoLabs SARL. All rights reserved.
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
25#include "ogr_api.h"
26#include "ogrsf_frmts.h"
27#include "ogr_p.h"
28#include "response_print.h"
29#if GDAL_VERSION_MAJOR >= 2
30#include <gdal_priv.h>
31#endif
32
33#include "sqlapi.h"
34#include <fcgi_stdio.h>
35#include <stdlib.h>
36
37/**
38 * Global GDALDataset pointer
39 */
40#if GDAL_VERSION_MAJOR >=2
41GDALDataset
42#else
43OGRDataSource
44#endif
45 **zoo_DS = NULL;
46
47/**
48 * Global OGRLayer pointer pointing to the lastest result set
49 */
50OGRLayer *zoo_ResultSet = NULL;
51
52/**
53 * Create a GDAL / OGR string for connecting to a db backend defined in the
54 * key section.
55 *
56 * @param conf the maps containing the setting of the main.cfg file
57 * @param key the name of the section containing the connection setting
58 * @return the OGR connection string
59 */
60char* _createInitString(maps* conf,const char* key){
61  char* res=NULL;
62  char keywords[6][14]={
63    "dbname",
64    "host",
65    "port",
66    "user",
67    "password",
68    "active_schema"   
69  };
70  int i=0;
71  maps* cconf=getMaps(conf,key);
72  if(cconf==NULL){
73    fprintf(stderr,"%s %d\n",__FILE__,__LINE__);
74    return "-1";
75  }
76  int len=0;
77  for(i=0;i<6;i++){
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  }
102  return res;
103}
104
105/**
106 * Create a GDAL / OGR string for connecting to the db backend
107 *
108 * @param conf the maps containing the setting of the main.cfg file
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
119 * @see createInitString
120 */
121int _init_sql(maps* conf,const char* key){
122  char* sqlInitString=_createInitString(conf,key);
123#ifdef SQL_DEBUG
124  fprintf(stderr,"Try to connect to: %s %s !\n",key,sqlInitString);
125  fflush(stderr); 
126#endif
127  if(strncmp(sqlInitString,"-1",2)==0)
128    return -1;
129  OGRSFDriver *poDriver = NULL;
130  OGRRegisterAll();
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=
144#if GDAL_VERSION_MAJOR >= 2
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,
161                                      GDAL_OF_UPDATE | GDAL_OF_VECTOR,
162                                      NULL, NULL, NULL );
163#else
164  zoo_DS[zoo_ds_nb] = OGRSFDriverRegistrar::Open(sqlInitString,false,&poDriver);
165#endif
166  if( zoo_DS[zoo_ds_nb] == NULL ){
167#ifdef SQL_DEBUG
168    fprintf(stderr,"sqlInitString: %s FAILED !\n",sqlInitString);
169    fflush(stderr);
170#endif
171    free(sqlInitString);
172    setMapInMaps(conf,"lenv","dbIssue","1");
173    setMapInMaps(conf,"lenv","message",_("Failed to connect to the database backend"));
174    return -2;
175  }
176#ifdef SQL_DEBUG
177  fprintf(stderr,"sqlInitString: %s SUCEED !\n",sqlInitString);
178  fflush(stderr);
179#endif
180  free(sqlInitString);
181  zoo_ds_nb++;
182  return zoo_ds_nb;
183}
184
185/**
186 * Connect to the db backend.
187 *
188 * @param conf the maps containing the setting of the main.cfg file
189 * @see createInitString
190 */
191int init_sql(maps* conf){
192  return _init_sql(conf,"database");
193}
194
195/**
196 * Close any connection to the db backend.
197 *
198 * @param conf the maps containing the setting of the main.cfg file
199 */
200void close_sql(maps* conf,int cid){
201  if( zoo_ResultSet != NULL ){
202    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
203    zoo_ResultSet=NULL;
204  }
205  if(zoo_DS!=NULL && zoo_DS[cid]!=NULL){
206#if GDAL_VERSION_MAJOR >= 2
207    GDALClose(zoo_DS[cid]);
208#else
209    OGRDataSource::DestroyDataSource( zoo_DS[cid] );
210#endif
211    zoo_DS[cid]=NULL;
212  }
213}
214
215/**
216 * Call OGRCleanupAll.
217 *
218 */
219void end_sql(){
220  OGRCleanupAll();
221}
222
223/**
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 */
230OGRLayer *fetchSql(maps* conf,int cid,const char* sqlQuery){
231  if(zoo_DS==NULL || zoo_DS[cid]==NULL)
232    return NULL;
233  OGRLayer *res=NULL;
234#ifdef SQL_DEBUG
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);
239  return res;
240}
241
242void cleanFetchSql(maps* conf,int cid,OGRLayer *objects){
243  if( objects != NULL ){
244    zoo_DS[cid]->ReleaseResultSet( objects );
245    objects=NULL;
246  }
247}
248
249/**
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 */
256int execSql(maps* conf,int cid,const char* sqlQuery){
257  int res=-1;
258  if(zoo_DS == NULL || zoo_DS[cid]==NULL)
259    return -1;
260  zoo_ResultSet = zoo_DS[cid]->ExecuteSQL( sqlQuery, NULL, NULL);
261  if( zoo_ResultSet != NULL ){
262    res=1;
263  }
264  return res;
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 */
274void cleanUpResultSet(const maps* conf,int cid){
275  if( zoo_ResultSet != NULL ){
276    zoo_DS[cid]->ReleaseResultSet( zoo_ResultSet );
277    zoo_ResultSet=NULL;
278  }
279}
280
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
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){
299  int zoo_ds_nb=getCurrentId(conf);
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);
307  execSql(conf,zoo_ds_nb-1,sqlQuery);
308  free(sqlQuery);
309  cleanUpResultSet(conf,zoo_ds_nb-1);
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){
318  int zoo_ds_nb=getCurrentId(conf);
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)+
326                                strlen(sid->value)+
327                                strlen(wpsStatus[2])+66+1)*sizeof(char));
328  sprintf(sqlQuery,
329          "INSERT INTO %s.services (uuid,sid,osid,fstate)"
330          "VALUES ('%s','%s','%s','%s');",
331          schema->value,
332          uusid->value,
333          sid->value,
334          osid->value,
335          wpsStatus[2]);
336  execSql(conf,zoo_ds_nb-1,sqlQuery);
337  free(sqlQuery);
338  cleanUpResultSet(conf,zoo_ds_nb-1);
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){
348  int zoo_ds_nb=getCurrentId(conf);
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");
359  char *sqlQuery=(char*)malloc((strlen(schema->value)+flen+strlen(sid->value)+57+1)*sizeof(char));
360  sprintf(sqlQuery,"INSERT INTO %s.responses (content,uuid) VALUES ($$%s$$,$$%s$$);",schema->value,tmps,sid->value);
361  execSql(conf,zoo_ds_nb-1,sqlQuery);
362  free(sqlQuery);
363  free(tmps);
364  cleanUpResultSet(conf,zoo_ds_nb-1);
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){
374  int zoo_ds_nb=getCurrentId(conf);
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);
381  if( zoo_ds_nb==
382#ifdef META_DB
383      1
384#else
385      0
386#endif     
387     ){
388    init_sql(conf);
389    zoo_ds_nb++;
390  }
391  execSql(conf,0,sqlQuery);
392  cleanUpResultSet(conf,0);
393  close_sql(conf,0);
394  free(sqlQuery);
395  setMapInMaps(conf,"lenv","ds_nb","0");
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){
407  int zoo_ds_nb=getCurrentId(conf);
408  int created=-1;
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);
412  if( zoo_ds_nb==
413#ifdef META_DB
414      1
415#else
416      0
417#endif
418      ){
419    init_sql(conf);
420    zoo_ds_nb++;
421    created=1;
422  }
423  execSql(conf,zoo_ds_nb-1,sqlQuery);
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  }
436  cleanUpResultSet(conf,zoo_ds_nb-1);
437  free(sqlQuery);
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){
449  int zoo_ds_nb=getCurrentId(conf);
450  map *schema=getMapFromMaps(conf,"database","schema");
451  OGRFeature  *poFeature = NULL;
452  const char *tmp1=NULL;
453  int hasRes=-1;
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);
458  if( zoo_ds_nb==
459#ifdef META_DB
460      1
461#else
462      0
463#endif
464      ){
465    init_sql(conf);
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 );
480      }
481  }
482  if(hasRes<0)
483    tmp1=NULL;
484  cleanUpResultSet(conf,zoo_ds_nb-1);
485  free(sqlQuery);
486  return (char*)tmp1;
487}
488
489/**
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){
496  int zoo_ds_nb=getCurrentId(conf);
497  map *schema=getMapFromMaps(conf,"database","schema");
498  char *sqlQuery=(char*)
499    malloc((strlen(pid)+strlen(schema->value)+38+1)
500           *sizeof(char));
501  if( zoo_ds_nb==
502#ifdef META_DB
503      1
504#else
505      0
506#endif
507      ){
508    init_sql(conf);
509    zoo_ds_nb++;
510  }
511  sprintf(sqlQuery,
512          "DELETE FROM %s.services where uuid=$$%s$$;",
513          schema->value,pid);
514  execSql(conf,zoo_ds_nb-1,sqlQuery);
515  cleanUpResultSet(conf,zoo_ds_nb-1);
516  close_sql(conf,zoo_ds_nb-1);
517  free(sqlQuery);
518  end_sql();
519}
520
521/**
522 * Stop handling status repport.
523 *
524 * @param conf the map containing the setting of the main.cfg file
525 */
526void unhandleStatus(maps* conf){
527  int zoo_ds_nb=getCurrentId(conf);
528  map *schema=getMapFromMaps(conf,"database","schema");
529  map *sid=getMapFromMaps(conf,"lenv","usid");
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);
541  execSql(conf,zoo_ds_nb-1,sqlQuery);
542  cleanUpResultSet(conf,zoo_ds_nb-1);
543  //close_sql(conf,zoo_ds_nb-1);
544  free(sqlQuery);
545  //end_sql();
546}
547
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){
556  int zoo_ds_nb=getCurrentId(conf);
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);
562  if( zoo_ds_nb==0 ){
563    init_sql(conf);
564    zoo_ds_nb++;
565  }
566  if(execSql(conf,zoo_ds_nb-1,sqlQuery)<0)
567    return NULL;
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;
583  free(sqlQuery);
584  cleanUpResultSet(conf,zoo_ds_nb-1);
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){
595  int zoo_ds_nb=getCurrentId(conf);
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);
603  execSql(conf,zoo_ds_nb-1,sqlQuery);
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  }
616  cleanUpResultSet(conf,zoo_ds_nb-1);
617  if(hasRes<0)
618    addToMap(statusInfo,"Status","Failed");
619  free(sqlQuery);
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;
632  int zoo_ds_nb=getCurrentId(conf);
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);
636  if( zoo_ds_nb == 0 ){
637    init_sql(conf);
638    zoo_ds_nb++;
639  }
640  execSql(conf,zoo_ds_nb-1,sqlQuery);
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  }
653  cleanUpResultSet(conf,zoo_ds_nb-1);
654  free(sqlQuery);
655  return res;
656}
657
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