-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsync
More file actions
executable file
·231 lines (195 loc) · 6.26 KB
/
Copy pathsync
File metadata and controls
executable file
·231 lines (195 loc) · 6.26 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env php
<?php
/**
* Sync Engine
*
* This is the bootstrap file for the email syncing engine, called
* from the CLI or managed via a supervisor. It works by checking
* a list of saved IMAP credentials and runs through a flow of tasks.
*/
use App\Console\SyncConsole;
use App\Diagnostics;
use App\Exceptions\Stop as StopException;
use App\Exceptions\Terminate as TerminateException;
use App\Log;
use App\Message;
use App\Model;
use App\Startup;
use App\Stats;
use App\Sync;
use App\Sync\Threads as ThreadSync;
use Pb\PDO\Database;
use Pimple\Container;
// Update internals
set_time_limit(0);
mb_internal_encoding('UTF-8');
date_default_timezone_set('UTC');
// Load the vendor libraries
require __DIR__.'/vendor/autoload.php';
// Load application constants
require __DIR__.'/config/constants.php';
// Load configuration files and parse the CLI arguments
$default = parse_ini_file(BASEPATH.'/config/default.ini', true);
$local = parse_ini_file(BASEPATH.'/config/local.ini', true);
$config = array_replace_recursive($default, $local);
// Set the memory limit from the config
ini_set('memory_limit', $config['app']['memory']);
ini_set('mysql.connect_timeout', $config['sql']['timeout']);
ini_set('default_socket_timeout', $config['sql']['timeout']);
// Set up dependency container and register all services
$di = new Container();
// Store the configuration as a service
$di['config'] = $config;
// Console/CLI service
$di['console'] = new SyncConsole($config);
$di['cli'] = function ($container) {
return $container['console']->getCLI();
};
// Diagnostic test service
$di['diagnostics'] = function ($container) {
return new Diagnostics($container);
};
// Statistics logging and printing service
$di['stats'] = function ($container) {
return new Stats($container['console']);
};
// Logging service
$di['log'] = function ($container) {
return new Log(
$container['cli'],
$container['config']['log'],
true === $container['console']->interactive
);
};
// Initialize the error and exception handlers
$di['log']->init();
// Store a static reference to the threading system. This should be
// saved in the container because the threading operation needs to
// query and organize a large amount of data during the first pass.
$di['threader'] = function ($container) {
return new ThreadSync(
$container['log']->getLogger(),
$container['cli'],
true === $container['console']->interactive
);
};
// Set up the signal handler to shutdown
$HALT = function ($signo) use ($di) {
if (isset($di['sync']) && $di['sync']) {
$di['sync']->halt();
} else {
throw new TerminateException();
}
};
pcntl_signal(SIGHUP, $HALT);
pcntl_signal(SIGINT, $HALT);
pcntl_signal(SIGTERM, $HALT);
pcntl_signal(SIGQUIT, $HALT);
// No-op, continue sync if asleep
pcntl_signal(SIGCONT, function ($signo) use ($di) {
$di['log']->getLogger()->addInfo(
'SIGCONT received, restarting sync'
);
if (isset($di['sync']) && $di['sync']) {
$di['sync']->wake();
}
});
// Return back statistics on the current sync
pcntl_signal(SIGUSR1, function ($signo) use ($di) {
$di['log']->getLogger()->addDebug(
'SIGUSR1 received, printing text stats'
);
if (isset($di['stats']) && $di['stats']) {
$di['stats']->text();
}
});
// SIGUSR2 returns JSON
pcntl_signal(SIGUSR2, function ($signo) use ($di) {
$di['log']->getLogger()->addDebug(
'SIGUSR2 received, printing JSON stats'
);
if (isset($di['stats']) && $di['stats']) {
$di['stats']->json();
}
});
// SIGURG stops the syncing
pcntl_signal(SIGURG, function ($signo) use ($di) {
$di['log']->getLogger()->addInfo(
'SIGURG received, stopping sync'
);
if (isset($di['sync']) && $di['sync']) {
$di['sync']->stop();
}
});
// PDO factory, this uses a wrapper around PDO
$di['db_factory'] = $di->factory(function ($container, $config = null) {
$dbConfig = $config ?: $container['config']['sql'];
$dsn = sprintf(
'mysql:host=%s;dbname=%s;charset=%s',
$dbConfig['hostname'],
$dbConfig['database'],
$dbConfig['charset']
);
try {
$db = new Database(
$dsn,
$dbConfig['username'],
$dbConfig['password']
);
$db->query('SET SESSION wait_timeout = 28800;');
$db->query('SET NAMES '.$dbConfig['charset'].';');
return $db;
} catch (PDOException $e) {
$message = sprintf('%s. %s: %s',
'There was a problem connecting to the database',
'Are you sure it exists? Here are the details',
$e->getMessage()
);
throw new TerminateException($message);
}
});
// Run the diagnostic tests. If specified from the CLI, then
// output the results to the console and exit.
$di['diagnostics']->run();
// PDO connection. This attempts to connect to the database.
// If it fails here, the script will halt.
$di['db'] = $di['db_factory'];
// Statically set the services in the base model
Model::setDb($di['db']);
Model::setCLI($di['cli']);
Model::setConfig($di['config']);
Model::setLog($di['log']->getLogger());
// Statically set Message logging service
Message::setLog($di['log']->getLogger());
// Parse the CLI
$di['console']->init();
// Run initialization checks, like if the database exists or if there
// are email accounts saved. This may prompt the user to add an account
// if we're running in interactive mode.
try {
$startup = new Startup($di);
$startup->run();
runSyncLoop:
$di['sync'] = new Sync($di);
$di['sync']->loop();
} catch (PDOException $e) {
// If the database connection dropped, kill the database service and
// try to reconnect; otherwise terminate
isset($di['sync']) && $di['sync']->disconnect(true);
$di['sync'] = null;
$sec = $config['app']['db']['sleep_minutes'] * 60;
Diagnostics::checkDatabaseException($di, $e, true, $sec);
goto runSyncLoop;
} catch (StopException $e) {
// Stop the current sync and re-run it
$di['sync'] = null;
sleep(5);
goto runSyncLoop;
} catch (TerminateException $e) {
// Gracefully exit if we're terminated
$di['log']->getLogger()->addInfo($e->getMessage());
exit(0);
} catch (Exception $e) {
// Unhandled exceptions
$di['log']->displayError($e);
}