Skip to content

Commit

Permalink
Improve ability of downstream apps to control file watching
Browse files Browse the repository at this point in the history
Signed-off-by: Nigel Westbury <nigelipse@miegel.org>
  • Loading branch information
westbury committed Mar 17, 2021
1 parent be686ee commit ea30fb7
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 63 deletions.
144 changes: 86 additions & 58 deletions packages/filesystem/src/node/filesystem-backend-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ContainerModule, interfaces } from 'inversify';
import { ConnectionHandler, JsonRpcConnectionHandler, ILogger } from '@theia/core/lib/common';
import { FileSystemWatcherServer, FileSystemWatcherService } from '../common/filesystem-watcher-protocol';
import { FileSystemWatcherServerClient } from './filesystem-watcher-client';
import { NsfwFileSystemWatcherService } from './nsfw-watcher/nsfw-filesystem-service';
import { NsfwFileSystemWatcherService, NsfwFileSystemWatcherServerOptions } from './nsfw-watcher/nsfw-filesystem-service';
import { MessagingService } from '@theia/core/lib/node/messaging/messaging-service';
import { NodeFileUploadService } from './node-file-upload-service';
import { NsfwOptions } from './nsfw-watcher/nsfw-options';
Expand All @@ -33,64 +33,18 @@ import { IPCConnectionProvider } from '@theia/core/lib/node';
import { JsonRpcProxyFactory, ConnectionErrorHandler } from '@theia/core';
import { FileSystemWatcherServiceDispatcher } from './filesystem-watcher-dispatcher';

const SINGLE_THREADED = process.argv.indexOf('--no-cluster') !== -1;
const NSFW_WATCHER_VERBOSE = process.argv.indexOf('--nsfw-watcher-verbose') !== -1;
export const NSFW_SINGLE_THREADED = process.argv.includes('--no-cluster');
export const NSFW_WATCHER_VERBOSE = process.argv.includes('--nsfw-watcher-verbose');

export function bindFileSystemWatcherServer(bind: interfaces.Bind, { singleThreaded }: { singleThreaded: boolean } = { singleThreaded: SINGLE_THREADED }): void {
bind<NsfwOptions>(NsfwOptions).toConstantValue({});

bind(FileSystemWatcherServiceDispatcher).toSelf().inSingletonScope();

bind(FileSystemWatcherServerClient).toSelf();
bind(FileSystemWatcherServer).toService(FileSystemWatcherServerClient);

if (singleThreaded) {
// Bind and run the watch server in the current process:
bind<FileSystemWatcherService>(FileSystemWatcherService).toDynamicValue(ctx => {
const logger = ctx.container.get<ILogger>(ILogger);
const nsfwOptions = ctx.container.get<NsfwOptions>(NsfwOptions);
const dispatcher = ctx.container.get<FileSystemWatcherServiceDispatcher>(FileSystemWatcherServiceDispatcher);
const server = new NsfwFileSystemWatcherService({
nsfwOptions,
verbose: NSFW_WATCHER_VERBOSE,
info: (message, ...args) => logger.info(message, ...args),
error: (message, ...args) => logger.error(message, ...args)
});
server.setClient(dispatcher);
return server;
}).inSingletonScope();
} else {
// Run the watch server in a child process.
// Bind to a proxy forwarding calls to the child process.
bind<FileSystemWatcherService>(FileSystemWatcherService).toDynamicValue(ctx => {
const serverName = 'nsfw-watcher';
const logger = ctx.container.get<ILogger>(ILogger);
const nsfwOptions = ctx.container.get<NsfwOptions>(NsfwOptions);
const ipcConnectionProvider = ctx.container.get<IPCConnectionProvider>(IPCConnectionProvider);
const dispatcher = ctx.container.get<FileSystemWatcherServiceDispatcher>(FileSystemWatcherServiceDispatcher);
const proxyFactory = new JsonRpcProxyFactory<FileSystemWatcherService>();
const serverProxy = proxyFactory.createProxy();
// We need to call `.setClient` before listening, else the JSON-RPC calls won't go through.
serverProxy.setClient(dispatcher);
const args: string[] = [
`--nsfwOptions=${JSON.stringify(nsfwOptions)}`
];
if (NSFW_WATCHER_VERBOSE) {
args.push('--verbose');
}
ipcConnectionProvider.listen({
serverName,
entryPoint: path.resolve(__dirname, serverName),
errorHandler: new ConnectionErrorHandler({
serverName,
logger,
}),
env: process.env,
args,
}, connection => proxyFactory.listen(connection));
return serverProxy;
}).inSingletonScope();
}
export const NsfwFileSystemWatcherServiceProcessOptions = Symbol('NsfwFileSystemWatcherServiceProcessOptions');
/**
* Options to control the way the `NsfwFileSystemWatcherService` process is spawned.
*/
export interface NsfwFileSystemWatcherServiceProcessOptions {
/**
* Path to the script that will run the `NsfwFileSystemWatcherService` in a new process.
*/
entryPoint: string;
}

export default new ContainerModule(bind => {
Expand All @@ -111,3 +65,77 @@ export default new ContainerModule(bind => {
bind(NodeFileUploadService).toSelf().inSingletonScope();
bind(MessagingService.Contribution).toService(NodeFileUploadService);
});

export function bindFileSystemWatcherServer(bind: interfaces.Bind, { singleThreaded = NSFW_SINGLE_THREADED }: { singleThreaded?: boolean } = {}): void {
bind<NsfwOptions>(NsfwOptions).toConstantValue({});

bind(FileSystemWatcherServiceDispatcher).toSelf().inSingletonScope();

bind(FileSystemWatcherServerClient).toSelf();
bind(FileSystemWatcherServer).toService(FileSystemWatcherServerClient);

bind<NsfwFileSystemWatcherServiceProcessOptions>(NsfwFileSystemWatcherServiceProcessOptions).toConstantValue({
entryPoint: path.resolve(__dirname, 'nsfw-watcher'),
});
bind<NsfwFileSystemWatcherServerOptions>(NsfwFileSystemWatcherServerOptions).toDynamicValue(ctx => {
const logger = ctx.container.get<ILogger>(ILogger);
const nsfwOptions = ctx.container.get<NsfwOptions>(NsfwOptions);
return {
nsfwOptions,
verbose: NSFW_WATCHER_VERBOSE,
info: (message, ...args) => logger.info(message, ...args),
error: (message, ...args) => logger.error(message, ...args),
};
}).inSingletonScope();

bind<FileSystemWatcherService>(FileSystemWatcherService).toDynamicValue(
ctx => singleThreaded
? createNsfwFileSystemWatcherService(ctx)
: spawnNsfwFileSystemWatcherServiceProcess(ctx)
).inSingletonScope();
}

/**
* Run the watch server in the current process.
*/
export function createNsfwFileSystemWatcherService(ctx: interfaces.Context): FileSystemWatcherService {
const options = ctx.container.get<NsfwFileSystemWatcherServerOptions>(NsfwFileSystemWatcherServerOptions);
const dispatcher = ctx.container.get<FileSystemWatcherServiceDispatcher>(FileSystemWatcherServiceDispatcher);
const server = new NsfwFileSystemWatcherService(options);
server.setClient(dispatcher);
return server;
}

/**
* Run the watch server in a child process.
* Return a proxy forwarding calls to the child process.
*/
export function spawnNsfwFileSystemWatcherServiceProcess(ctx: interfaces.Context): FileSystemWatcherService {
const options = ctx.container.get<NsfwFileSystemWatcherServiceProcessOptions>(NsfwFileSystemWatcherServiceProcessOptions);
const dispatcher = ctx.container.get<FileSystemWatcherServiceDispatcher>(FileSystemWatcherServiceDispatcher);
const serverName = 'nsfw-watcher';
const logger = ctx.container.get<ILogger>(ILogger);
const nsfwOptions = ctx.container.get<NsfwOptions>(NsfwOptions);
const ipcConnectionProvider = ctx.container.get<IPCConnectionProvider>(IPCConnectionProvider);
const proxyFactory = new JsonRpcProxyFactory<FileSystemWatcherService>();
const serverProxy = proxyFactory.createProxy();
// We need to call `.setClient` before listening, else the JSON-RPC calls won't go through.
serverProxy.setClient(dispatcher);
const args: string[] = [
`--nsfwOptions=${JSON.stringify(nsfwOptions)}`
];
if (NSFW_WATCHER_VERBOSE) {
args.push('--verbose');
}
ipcConnectionProvider.listen({
serverName,
entryPoint: options.entryPoint,
errorHandler: new ConnectionErrorHandler({
serverName,
logger,
}),
env: process.env,
args,
}, connection => proxyFactory.listen(connection));
return serverProxy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface NsfwWatcherOptions {
ignored: IMinimatch[]
}

export const NsfwFileSystemWatcherServerOptions = Symbol('NsfwFileSystemWatcherServerOptions');
export interface NsfwFileSystemWatcherServerOptions {
verbose: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -422,11 +423,7 @@ export class NsfwFileSystemWatcherService implements FileSystemWatcherService {
let watcher = this.watchers.get(watcherKey);
if (watcher === undefined) {
const fsPath = FileUri.fsPath(uri);
const watcherOptions: NsfwWatcherOptions = {
ignored: resolvedOptions.ignored
.map(pattern => new Minimatch(pattern, { dot: true })),
};
watcher = new NsfwWatcher(clientId, fsPath, watcherOptions, this.options, this.maybeClient);
watcher = this.createWatcher(clientId, fsPath, resolvedOptions);
watcher.whenDisposed.then(() => this.watchers.delete(watcherKey));
this.watchers.set(watcherKey, watcher);
} else {
Expand All @@ -438,6 +435,14 @@ export class NsfwFileSystemWatcherService implements FileSystemWatcherService {
return watcherId;
}

protected createWatcher(clientId: number, fsPath: string, options: WatchOptions): NsfwWatcher {
const watcherOptions: NsfwWatcherOptions = {
ignored: options.ignored
.map(pattern => new Minimatch(pattern, { dot: true })),
};
return new NsfwWatcher(clientId, fsPath, watcherOptions, this.options, this.maybeClient);
}

async unwatchFileChanges(watcherId: number): Promise<void> {
const handle = this.watcherHandles.get(watcherId);
if (handle === undefined) {
Expand Down

0 comments on commit ea30fb7

Please sign in to comment.