public
Description: Phusion Passenger (mod_rails)
Homepage: http://www.modrails.com/
Clone URL: git://github.com/FooBarWidget/passenger.git
Click here to lend your support to: passenger and make a donation at www.pledgie.com !
passenger / ext / apache2 / ApplicationPool.h
100644 85 lines (67 sloc) 1.938 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef _PASSENGER_APPLICATION_POOL_H_
#define _PASSENGER_APPLICATION_POOL_H_
 
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
 
#include <string>
#include <map>
 
#ifdef PASSENGER_USE_DUMMY_SPAWN_MANAGER
  #include "DummySpawnManager.h"
#else
  #include "SpawnManager.h"
#endif
 
namespace Passenger {
 
using namespace std;
using namespace boost;
 
class ApplicationPool {
public:
  virtual ~ApplicationPool() {};
  
  virtual ApplicationPtr get(const string &appRoot, const string &user = "", const string &group = "") = 0;
};
 
// TODO: document this
class StandardApplicationPool: public ApplicationPool {
private:
  typedef map<string, ApplicationPtr> ApplicationMap;
 
  #ifdef PASSENGER_USE_DUMMY_SPAWN_MANAGER
    DummySpawnManager spawnManager;
  #else
    SpawnManager spawnManager;
  #endif
  ApplicationMap apps;
  mutex lock;
  bool threadSafe;
  
  string normalizePath(const string &path) {
    // TODO
    return path;
  }
  
public:
  StandardApplicationPool(const string &spawnManagerCommand,
   const string &logFile = "",
   const string &environment = "production",
   const string &rubyCommand = "ruby")
  #ifndef PASSENGER_USE_DUMMY_SPAWN_MANAGER
    : spawnManager(spawnManagerCommand, logFile, environment, rubyCommand)
  #endif
  {
    threadSafe = false;
  }
  
  void setThreadSafe() {
    threadSafe = true;
  }
  
  // TODO: improve algorithm
  virtual ApplicationPtr get(const string &appRoot, const string &user = "", const string &group = "") {
    string normalizedAppRoot(normalizePath(appRoot));
    ApplicationPtr app;
    mutex::scoped_lock l(lock, threadSafe);
    
    ApplicationMap::iterator it(apps.find(appRoot));
    if (it == apps.end()) {
      app = spawnManager.spawn(appRoot, user, group);
      apps[appRoot] = app;
    } else {
      app = it->second;
    }
    return app;
  }
};
 
typedef shared_ptr<ApplicationPool> ApplicationPoolPtr;
 
}; // namespace Passenger
 
#endif /* _PASSENGER_APPLICATION_POOL_H_ */