forked from PostgresApp/PostgresApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostgresServer.m
More file actions
157 lines (133 loc) · 6.82 KB
/
PostgresServer.m
File metadata and controls
157 lines (133 loc) · 6.82 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// PostgresServer.m
//
// Created by Mattt Thompson (http://mattt.me/)
// Copyright (c) 2012 Heroku (http://heroku.com/)
//
// Portions Copyright (c) 1996-2012, The PostgreSQL Global Development Group
// Portions Copyright (c) 1994, The Regents of the University of California
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written agreement
// is hereby granted, provided that the above copyright notice and this
// paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
// LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
// EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
// THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
// "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO
// PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#import <xpc/xpc.h>
#import "PostgresServer.h"
#import "NSFileManager+DirectoryLocations.h"
@implementation PostgresServer {
__strong NSString *_binPath;
__strong NSString *_varPath;
__strong NSTask *_postgresTask;
NSUInteger _port;
xpc_connection_t _xpc_connection;
}
+ (PostgresServer *)sharedServer {
static PostgresServer *_sharedServer = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedServer = [[PostgresServer alloc] initWithExecutablesDirectory:[[NSBundle mainBundle] pathForAuxiliaryExecutable:@"bin"] databaseDirectory:[[[NSFileManager defaultManager] applicationSupportDirectory] stringByAppendingPathComponent:@"var"]];
});
return _sharedServer;
}
- (id)initWithExecutablesDirectory:(NSString *)executablesDirectory
databaseDirectory:(NSString *)databaseDirectory
{
self = [super init];
if (!self) {
return nil;
}
_binPath = executablesDirectory;
_varPath = databaseDirectory;
_xpc_connection = xpc_connection_create("com.heroku.postgres-service", dispatch_get_main_queue());
xpc_connection_set_event_handler(_xpc_connection, ^(xpc_object_t event) {
xpc_dictionary_apply(event, ^bool(const char *key, xpc_object_t value) {
return true;
});
});
xpc_connection_resume(_xpc_connection);
return self;
}
- (NSUInteger)port {
return [self isRunning] ? _port : NSNotFound;
}
- (BOOL)isRunning {
return _port != 0;
}
- (BOOL)startOnPort:(NSUInteger)port
terminationHandler:(void (^)(NSUInteger status))completionBlock
{
[self stopWithTerminationHandler:nil];
[self willChangeValueForKey:@"isRunning"];
[self willChangeValueForKey:@"port"];
_port = port;
NSString *existingPGVersion = [NSString stringWithContentsOfFile:[_varPath stringByAppendingPathComponent:@"PG_VERSION"] encoding:NSUTF8StringEncoding error:nil];
if (!existingPGVersion) {
[self executeCommandNamed:@"initdb" arguments:[NSArray arrayWithObjects:[NSString stringWithFormat:@"-D%@", _varPath], [NSString stringWithFormat:@"-E%@", @"UTF8"], [NSString stringWithFormat:@"--locale=%@_%@", [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode], [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]], nil] terminationHandler:^(NSUInteger status) {
[self executeCommandNamed:@"pg_ctl" arguments:[NSArray arrayWithObjects:@"start", [NSString stringWithFormat:@"-D%@", _varPath], @"-w", [NSString stringWithFormat:@"-o'-p%ld'", port], nil] terminationHandler:^(NSUInteger status) {
[self executeCommandNamed:@"createdb" arguments:[NSArray arrayWithObjects:[NSString stringWithFormat:@"-p%ld", port], NSUserName(), nil] terminationHandler:^(NSUInteger status) {
if (completionBlock) {
completionBlock(status);
}
}];
}];
}];
} else {
[self executeCommandNamed:@"pg_ctl" arguments:[NSArray arrayWithObjects:@"start", [NSString stringWithFormat:@"-D%@", _varPath], [NSString stringWithFormat:@"-o'-p%ld'", port], nil] terminationHandler:^(NSUInteger status) {
// Kill server and try one more time if server can't be started
if (status != 0) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self stopWithTerminationHandler:^(NSUInteger status) {
[self startOnPort:port terminationHandler:completionBlock];
}];
});
}
if (completionBlock) {
completionBlock(status);
}
}];
}
[self didChangeValueForKey:@"port"];
[self didChangeValueForKey:@"isRunning"];
return YES;
}
- (BOOL)stopWithTerminationHandler:(void (^)(NSUInteger status))terminationHandler {
NSString *pidPath = [_varPath stringByAppendingPathComponent:@"postmaster.pid"];
if ([[NSFileManager defaultManager] fileExistsAtPath:pidPath]) {
NSString *pid = [[[NSString stringWithContentsOfFile:pidPath encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] objectAtIndex:0];
NSLog(@"Pid: %@", pid);
[self executeCommandNamed:@"pg_ctl" arguments:[NSArray arrayWithObjects:@"kill", @"QUIT", pid, nil] terminationHandler:terminationHandler];
[[NSFileManager defaultManager] removeItemAtPath:pidPath error:nil];
}
return YES;
}
- (void)executeCommandNamed:(NSString *)command
arguments:(NSArray *)arguments
terminationHandler:(void (^)(NSUInteger status))terminationHandler
{
xpc_object_t message = xpc_dictionary_create(NULL, NULL, 0);
xpc_dictionary_set_string(message, "command", [[_binPath stringByAppendingPathComponent:command] UTF8String]);
xpc_object_t args = xpc_array_create(NULL, 0);
[arguments enumerateObjectsUsingBlock:^(id argument, NSUInteger idx, BOOL *stop) {
xpc_array_set_value(args, XPC_ARRAY_APPEND, xpc_string_create([argument UTF8String]));
}];
xpc_dictionary_set_value(message, "arguments", args);
xpc_connection_send_message_with_reply(_xpc_connection, message, dispatch_get_main_queue(), ^(xpc_object_t object) {
NSLog(@"%lld %s: Status %lld", xpc_dictionary_get_int64(object, "pid"), xpc_dictionary_get_string(object, "command"), xpc_dictionary_get_int64(object, "status"));
if (terminationHandler) {
terminationHandler(xpc_dictionary_get_int64(object, "status"));
}
});
}
@end