-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathdaemon.rs
More file actions
282 lines (252 loc) · 8.43 KB
/
daemon.rs
File metadata and controls
282 lines (252 loc) · 8.43 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use crate::{
CliDiagnostic, CliSession, open_transport,
service::{self, ensure_daemon, open_socket, run_daemon},
};
use pglt_console::{ConsoleExt, markup};
use pglt_lsp::ServerFactory;
use pglt_workspace::{TransportError, WorkspaceError, workspace::WorkspaceClient};
use std::{env, fs, path::PathBuf};
use tokio::io;
use tokio::runtime::Runtime;
use tracing::subscriber::Interest;
use tracing::{Instrument, Metadata, debug_span, metadata::LevelFilter};
use tracing_appender::rolling::Rotation;
use tracing_subscriber::{
Layer,
layer::{Context, Filter},
prelude::*,
registry,
};
use tracing_tree::HierarchicalLayer;
pub(crate) fn start(
session: CliSession,
config_path: Option<PathBuf>,
log_path: Option<PathBuf>,
log_file_name_prefix: Option<String>,
) -> Result<(), CliDiagnostic> {
let rt = Runtime::new()?;
let did_spawn = rt.block_on(ensure_daemon(
false,
config_path,
log_path,
log_file_name_prefix,
))?;
if did_spawn {
session.app.console.log(markup! {
"The server was successfully started"
});
} else {
session.app.console.log(markup! {
"The server was already running"
});
}
Ok(())
}
pub(crate) fn stop(session: CliSession) -> Result<(), CliDiagnostic> {
let rt = Runtime::new()?;
match open_transport(rt)? {
Some(transport) => {
let client = WorkspaceClient::new(transport)?;
match client.shutdown() {
// The `ChannelClosed` error is expected since the server can
// shutdown before sending a response
Ok(()) | Err(WorkspaceError::TransportError(TransportError::ChannelClosed)) => {}
Err(err) => return Err(CliDiagnostic::from(err)),
};
session.app.console.log(markup! {
"The server was successfully stopped"
});
}
_ => {
session.app.console.log(markup! {
"The server was not running"
});
}
}
Ok(())
}
pub(crate) fn run_server(
stop_on_disconnect: bool,
config_path: Option<PathBuf>,
log_path: Option<PathBuf>,
log_file_name_prefix: Option<String>,
) -> Result<(), CliDiagnostic> {
setup_tracing_subscriber(log_path, log_file_name_prefix);
let rt = Runtime::new()?;
let factory = ServerFactory::new(stop_on_disconnect);
let cancellation = factory.cancellation();
let span = debug_span!("Running Server", pid = std::process::id());
rt.block_on(async move {
tokio::select! {
res = run_daemon(factory, config_path).instrument(span) => {
match res {
Ok(never) => match never {},
Err(err) => Err(err.into()),
}
}
_ = cancellation.notified() => {
tracing::info!("Received shutdown signal");
Ok(())
}
}
})
}
pub(crate) fn print_socket() -> Result<(), CliDiagnostic> {
let rt = Runtime::new()?;
rt.block_on(service::print_socket())?;
Ok(())
}
pub(crate) fn lsp_proxy(
config_path: Option<PathBuf>,
log_path: Option<PathBuf>,
log_file_name_prefix: Option<String>,
) -> Result<(), CliDiagnostic> {
let rt = Runtime::new()?;
rt.block_on(start_lsp_proxy(
&rt,
config_path,
log_path,
log_file_name_prefix,
))?;
Ok(())
}
/// Start a proxy process.
/// Receives a process via `stdin` and then copy the content to the LSP socket.
/// Copy to the process on `stdout` when the LSP responds to a message
async fn start_lsp_proxy(
rt: &Runtime,
config_path: Option<PathBuf>,
log_path: Option<PathBuf>,
log_file_name_prefix: Option<String>,
) -> Result<(), CliDiagnostic> {
ensure_daemon(true, config_path, log_path, log_file_name_prefix).await?;
match open_socket().await? {
Some((mut owned_read_half, mut owned_write_half)) => {
// forward stdin to socket
let mut stdin = io::stdin();
let input_handle = rt.spawn(async move {
loop {
match io::copy(&mut stdin, &mut owned_write_half).await {
Ok(b) => {
if b == 0 {
return Ok(());
}
}
Err(err) => return Err(err),
};
}
});
// receive socket response to stdout
let mut stdout = io::stdout();
let out_put_handle = rt.spawn(async move {
loop {
match io::copy(&mut owned_read_half, &mut stdout).await {
Ok(b) => {
if b == 0 {
return Ok(());
}
}
Err(err) => return Err(err),
};
}
});
let _ = input_handle.await;
let _ = out_put_handle.await;
Ok(())
}
None => Ok(()),
}
}
pub(crate) fn read_most_recent_log_file(
log_path: Option<PathBuf>,
log_file_name_prefix: String,
) -> io::Result<Option<String>> {
let pglt_log_path = log_path.unwrap_or(default_pglt_log_path());
let most_recent = fs::read_dir(pglt_log_path)?
.flatten()
.filter(|file| file.file_type().is_ok_and(|ty| ty.is_file()))
.filter_map(|file| {
match file
.file_name()
.to_str()?
.split_once(log_file_name_prefix.as_str())
{
Some((_, date_part)) if date_part.split('-').count() == 4 => Some(file.path()),
_ => None,
}
})
.max();
match most_recent {
Some(file) => Ok(Some(fs::read_to_string(file)?)),
None => Ok(None),
}
}
/// Set up the [tracing]-based logging system for the server
/// The events received by the subscriber are filtered at the `info` level,
/// then printed using the [HierarchicalLayer] layer, and the resulting text
/// is written to log files rotated on a hourly basis (in
/// `pglt-logs/server.log.yyyy-MM-dd-HH` files inside the system temporary
/// directory)
fn setup_tracing_subscriber(log_path: Option<PathBuf>, log_file_name_prefix: Option<String>) {
let pglt_log_path = log_path.unwrap_or(pglt_fs::ensure_cache_dir().join("pglt-logs"));
let appender_builder = tracing_appender::rolling::RollingFileAppender::builder();
let file_appender = appender_builder
.filename_prefix(log_file_name_prefix.unwrap_or(String::from("server.log")))
.max_log_files(7)
.rotation(Rotation::HOURLY)
.build(pglt_log_path)
.expect("Failed to start the logger for the daemon.");
registry()
.with(
HierarchicalLayer::default()
.with_indent_lines(true)
.with_indent_amount(2)
.with_bracketed_fields(true)
.with_targets(true)
.with_ansi(false)
.with_writer(file_appender)
.with_filter(LoggingFilter),
)
.init();
}
pub fn default_pglt_log_path() -> PathBuf {
match env::var_os("PGLT_LOG_PATH") {
Some(directory) => PathBuf::from(directory),
None => pglt_fs::ensure_cache_dir().join("pglt-logs"),
}
}
/// Tracing filter enabling:
/// - All spans and events at level info or higher
/// - All spans and events at level debug in crates whose name starts with `pglt`
struct LoggingFilter;
/// Tracing filter used for spans emitted by `pglt*` crates
const SELF_FILTER: LevelFilter = if cfg!(debug_assertions) {
LevelFilter::TRACE
} else {
LevelFilter::DEBUG
};
impl LoggingFilter {
fn is_enabled(&self, meta: &Metadata<'_>) -> bool {
let filter = if meta.target().starts_with("pglt") {
SELF_FILTER
} else {
LevelFilter::INFO
};
meta.level() <= &filter
}
}
impl<S> Filter<S> for LoggingFilter {
fn enabled(&self, meta: &Metadata<'_>, _cx: &Context<'_, S>) -> bool {
self.is_enabled(meta)
}
fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest {
if self.is_enabled(meta) {
Interest::always()
} else {
Interest::never()
}
}
fn max_level_hint(&self) -> Option<LevelFilter> {
Some(SELF_FILTER)
}
}