Skip to content

Commit

Permalink
Merge pull request #153 from metalabdesign/pauld/fixup
Browse files Browse the repository at this point in the history
Get luminol working with ml.co
  • Loading branch information
izaakschroeder committed Jul 20, 2019
2 parents 697c6dc + 568e662 commit 060e739
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 19 deletions.
7 changes: 5 additions & 2 deletions src/bin/luminol.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,12 @@ yargs
require: argv.require,
add: argv.add,
clipboard: argv.clipboard,
open: argv.open,
});
logListener(server.client, (log) => {
console.log(log.message);
server.on('ready', () => {
logListener(server.client, (log) => {
console.log(log.message);
});
});
for (const sig of ['SIGINT', 'SIGTERM']) {
process.on(sig, () => {
Expand Down
13 changes: 12 additions & 1 deletion src/internal/ProcessManager/ManagedProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ const USAGE_MUTATION = gql`
}
`;

type Options = {
url: string,
baseUrl: string,
};

class ManagedProcess {
rip: boolean = false;
child = null;
Expand All @@ -50,6 +55,8 @@ class ManagedProcess {
backoff = new Backoff({min: 1000, max: 1000 * 5});
config: Config;
client: Client;
url: string;
baseUrl: string;
lastCode: ?number;
detonateOnError = process.env.NODE_ENV === 'test';

Expand All @@ -58,9 +65,11 @@ class ManagedProcess {
this._kill();
};

constructor(client: Client, config: Config) {
constructor(client: Client, config: Config, options: Options = {}) {
this.client = client;
this.config = config;
this.url = options.url;
this.baseUrl = options.baseUrl;
this.debug = createDebug(`process:${this.config.id}`);
process.once('beforeExit', this._close);
process.once('exit', this._close);
Expand Down Expand Up @@ -131,6 +140,8 @@ class ManagedProcess {
const processId = this.config.id;
const env = {
LUMINOL_PROCESS_ID: processId,
LUMINOL_URL: this.baseUrl,
LUMINOL_API_URL: this.url,
...process.env,
};
(this.config.env || []).forEach(({key, value}) => {
Expand Down
20 changes: 16 additions & 4 deletions src/internal/ProcessManager/ProcessManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,21 @@ type Process = {
env: Array<{key: string, value: string}>,
};

type Options = {
url: string,
baseUrl: string,
};

class ProcessManager {
instances: {[string]: ManagedProcess} = {};
client: Client;
url: string;
baseUrl: string;

constructor(client: Client) {
constructor(client: Client, options: Options = {}) {
this.client = client;
this.url = options.url;
this.baseUrl = options.baseUrl;
const query = client.watchQuery({
query: PROCESS_QUERY,
});
Expand All @@ -73,7 +82,7 @@ class ProcessManager {
return {
...prev,
processes: [
...prev.processes,
...(prev.processes || []),
subscriptionData.data.processRegistered,
],
};
Expand All @@ -88,7 +97,7 @@ class ProcessManager {
const id = subscriptionData.data.processUnregistered.id;
return {
...prev,
processes: prev.processes.filter((config) => {
processes: (prev.processes || []).filter((config) => {
return config.id !== id;
}),
};
Expand All @@ -101,7 +110,10 @@ class ProcessManager {
return;
}
debug(`Loading process ${proc.id} => ${proc.path}`);
this.instances[proc.id] = new ManagedProcess(this.client, proc);
this.instances[proc.id] = new ManagedProcess(this.client, proc, {
url: this.url,
baseUrl: this.baseUrl,
});
}

_unload(id: string) {
Expand Down
23 changes: 14 additions & 9 deletions src/internal/Server/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ const replyGql = (options) =>
query,
});
return send(
200,
{
'Content-Type': 'application/json',
},
result,
result.responseInit.statusCode || 200,
result.responseInit.headers,
result.graphqlResponse,
);
} catch (error) {
if (error.name !== 'HttpQueryError') {
Expand All @@ -106,6 +104,7 @@ class Server {
proxyManager: ?ProxyManager = null;
processManager: ?ProcessManager = null;
url: ?string;
baseUrl: ?string;
client: ?Client = null;
base: ?HTTPServer = null;
q: Array<() => void> = [];
Expand Down Expand Up @@ -164,7 +163,9 @@ class Server {
item(base);
});
this.q = [];
this.url = `http://localhost:${base.address().port}${this.apiPrefix}`;
this.baseUrl = `http://localhost:${base.address().port}`;
this.url = `${this.baseUrl}${this.apiPrefix}`;

debug(`Using server ${this.url}`);
let baseApp = next;
if (typeof this.options.url === 'string') {
Expand Down Expand Up @@ -221,7 +222,10 @@ class Server {
);

// Responsible for managing child processes.
this.processManager = new ProcessManager(this.client);
this.processManager = new ProcessManager(this.client, {
url: this.url || '',
baseUrl: this.baseUrl || '',
});

if (typeof this.options.config !== 'undefined') {
const configs = Array.isArray(this.options.config)
Expand All @@ -246,11 +250,12 @@ class Server {
}

if (this.options.open === true) {
open(this.url);
open(this.baseUrl || '');
}
if (this.options.clipboard !== false) {
clipboardy.writeSync(this.url);
clipboardy.writeSync(this.baseUrl);
}
this.base.emit('ready');
}

load(path: string) {
Expand Down
4 changes: 2 additions & 2 deletions src/internal/apollo/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@ const resolvers = {
const compiler = context.getCompiler(compilerId);
compiler.status = status;
pubsub.publish('compilerUpdated', {compilerUpdated: compiler});
return compiler;
return true;
},
publishCompilerState(_, {compilerId, hash, state}, context) {
const compiler = context.getCompiler(compilerId);
compiler.state = state;
compiler.hash = hash;
pubsub.publish('compilerUpdated', {compilerUpdated: compiler});
return compiler;
return true;
},
},
Subscription: {
Expand Down
5 changes: 5 additions & 0 deletions src/internal/apollo/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const typeDefs = gql`
data: String
}
type Log {
message: String!
}
type EnvVar {
key: String
value: String
Expand Down Expand Up @@ -149,6 +153,7 @@ const typeDefs = gql`
}
type Subscription {
logReceived: Log
compilerUpdated(compilerId: ID): Compiler
requestProcessed: Request
proxyRegistered: Proxy
Expand Down
4 changes: 3 additions & 1 deletion src/internal/logListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const logListener = (client, fn) => {
query: LOG_SUBSCRIPTION,
});
return query.subscribe({
next: (data) => fn(data.logReceived),
next: (response) => {
fn(response.data.logReceived);
},
});
};

Expand Down

0 comments on commit 060e739

Please sign in to comment.