@@ -12,6 +12,8 @@ import { v4 as uuidv4 } from "uuid";
1212import crypto from "crypto" ;
1313import Docker from "dockerode" ;
1414
15+ import Stomp from "stompjs" ;
16+
1517import path from "path" ;
1618
1719import os from "os" ;
@@ -558,16 +560,16 @@ async function createContainer(image, name, network) {
558560 } ) ;
559561}
560562
561- // var mq_client = Stomp.overTCP("rabbitmq", 61613);
562-
563563class MyMqSocket {
564564 queue ;
565- constructor ( queue ) {
565+ mq_client ;
566+ constructor ( queue , mq_client ) {
566567 this . queue = queue ;
568+ this . mq_client = mq_client
567569 }
568570 send ( obj ) {
569571 // FIXME need to make sure it is connected
570- // mq_client.send(this.queue, {}, obj);
572+ this . mq_client . send ( this . queue , { } , obj ) ;
571573 }
572574}
573575
@@ -718,6 +720,7 @@ function handleIOPub_stream({ msgs, socket }) {
718720 }
719721}
720722
723+ let mq_client = null ;
721724export class CodePodKernel {
722725 lang ;
723726 startupFile ;
@@ -727,6 +730,8 @@ export class CodePodKernel {
727730 sessionId ;
728731 wire ;
729732 socket ;
733+ mq_socket ;
734+ useMQ ;
730735 mapEval ( { code, namespace } ) { }
731736 mapAddImport ( {
732737 from,
@@ -749,9 +754,10 @@ export class CodePodKernel {
749754 name : string ;
750755 } ) { }
751756 constructor ( ) { }
752- async init ( { sessionId, socket } ) {
757+ async init ( { sessionId, socket, useMQ } ) {
753758 console . log ( "=== INIT!!" ) ;
754759 this . sessionId = sessionId ;
760+ this . useMQ = useMQ ;
755761 let network = process . env [ "KERNEL_NETWORK" ] || "codepod" ;
756762 let name = `cpkernel_${ network } _${ sessionId } _${ this . lang } ` ;
757763 // await removeContainer(name);
@@ -774,7 +780,26 @@ export class CodePodKernel {
774780 console . log ( "connecting to zmq .." ) ;
775781 // this.wire = new ZmqWire(JSON.parse(readFileSync(this.fname)), ip);
776782 this . wire = new ZmqWire ( spec , "127.0.0.1" ) ;
777- // this.mq_socket = new MyMqSocket(sessionId);
783+
784+ if ( useMQ ) {
785+ if ( ! mq_client ) {
786+ mq_client = Stomp . overTCP ( "localhost" , 61613 ) ;
787+ mq_client . connect (
788+ "guest" ,
789+ "guest" ,
790+ function ( ) {
791+ console . log ( "connected" ) ;
792+ } ,
793+ function ( ) {
794+ console . log ( "error" ) ;
795+ throw new Error ( "Cannot connect to RabbitMQ server" ) ;
796+ } ,
797+ "/"
798+ ) ;
799+ }
800+ this . mq_socket = new MyMqSocket ( sessionId , mq_client ) ;
801+ }
802+
778803 if ( socket ) {
779804 // listen to IOPub here
780805 this . addSocket ( socket ) ;
@@ -809,7 +834,9 @@ export class CodePodKernel {
809834 }
810835 this . socket = socket ;
811836 // DEBUG
812- // socket = this.mq_socket;
837+ if ( this . useMQ ) {
838+ socket = this . mq_socket ;
839+ }
813840 this . wire . setOnIOPub ( ( topic , msgs ) => {
814841 // console.log("-----", topic, msgs);
815842 // iracket's topic seems to be an ID. I should use msg type instead
@@ -846,7 +873,7 @@ export class CodePodKernel {
846873 this . wire . setOnShell ( ( msgs ) => {
847874 // DEBUG
848875 // socket = this.mq_socket;
849- socket = this . socket ;
876+ // socket = this.socket;
850877 switch ( msgs . header . msg_type ) {
851878 case "execute_reply" :
852879 {
@@ -1145,22 +1172,24 @@ export async function createKernel({
11451172 lang,
11461173 sessionId,
11471174 socket,
1175+ useMQ
11481176} : {
11491177 lang : string ;
11501178 sessionId : string ;
11511179 socket : any ;
1180+ useMQ : boolean ;
11521181} ) {
11531182 let kernels = detectKernels ( )
11541183 console . log ( "===" , "createKernel" , lang ) ;
11551184 switch ( lang ) {
11561185 case "julia" :
1157- return await new JuliaKernel ( { kernelJson : kernels [ "julia" ] } ) . init ( { sessionId, socket } ) ;
1186+ return await new JuliaKernel ( { kernelJson : kernels [ "julia" ] } ) . init ( { sessionId, socket, useMQ } ) ;
11581187 case "javascript" :
1159- return await new JavascriptKernel ( { kernelJson : kernels [ "javascript" ] } ) . init ( { sessionId, socket } ) ;
1188+ return await new JavascriptKernel ( { kernelJson : kernels [ "javascript" ] } ) . init ( { sessionId, socket, useMQ } ) ;
11601189 case "racket" :
1161- return await new RacketKernel ( { kernelJson : kernels [ "racket" ] } ) . init ( { sessionId, socket } ) ;
1190+ return await new RacketKernel ( { kernelJson : kernels [ "racket" ] } ) . init ( { sessionId, socket, useMQ } ) ;
11621191 case "python" :
1163- return await new PythonKernel ( { kernelJson : kernels [ "python" ] } ) . init ( { sessionId, socket } ) ;
1192+ return await new PythonKernel ( { kernelJson : kernels [ "python" ] } ) . init ( { sessionId, socket, useMQ } ) ;
11641193 default :
11651194 console . log ( "ERROR: language not implemented" , lang ) ;
11661195 // throw new Error(`Language not valid: ${lang}`);
0 commit comments