From d513f302f562cab2849958bf608aa4091ae24246 Mon Sep 17 00:00:00 2001 From: tomczoink Date: Fri, 30 Jul 2021 14:23:32 +0100 Subject: [PATCH] Update usage of instances to processes This is to help differentiate between AblyD instances and the processes it spools off. --- README.md | 8 +++---- asyncapispec.yml | 4 ++-- examples/bash/count.html | 2 +- libablyd/ablyd_instance_state.go | 9 -------- ...sage.go => ablyd_process_start_message.go} | 2 +- libablyd/ablyd_process_state.go | 9 ++++++++ libablyd/handler.go | 21 ++++++++++--------- ...ance_message.go => new_process_message.go} | 2 +- 8 files changed, 29 insertions(+), 28 deletions(-) delete mode 100644 libablyd/ablyd_instance_state.go rename libablyd/{ablyd_instance_start_message.go => ablyd_process_start_message.go} (77%) create mode 100644 libablyd/ablyd_process_state.go rename libablyd/{new_instance_message.go => new_process_message.go} (75%) diff --git a/README.md b/README.md index b8a97f6..d37b5c9 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Once the server receives a message in the `command` channel, it will start up an } ``` -The client can identify the instance which has started for them by the `MessageID`, then use the `Prefix` to connect to an input and an output channel for the process. These will be of structure `{Prefix}{Pid}:serverinput` and `{Prefix}{Pid}:serveroutput`. +The client can identify the process which has started for them by the `MessageID`, then use the `Prefix` to connect to an input and an output channel for the process. These will be of structure `{Prefix}{Pid}:serverinput` and `{Prefix}{Pid}:serveroutput`. Subscribing to the `serveroutput` channel will allow the client to receive any stdout messages from the server. The client can also publish messages into the `serverinput` channel which will be passed into the stdin of the process. @@ -40,14 +40,14 @@ This will continue until the program naturally terminates, resulting in the proc ## Checking Current State of Processes and an AblyD Instance -AblyD makes use of Ably Presence to identify what AblyD instances exist, and what processes are running on each instance. If you check the presence set of the `command` channel, you'll see each currently active instance present with the following attached data: +AblyD makes use of Ably Presence to identify what AblyD instances exist, and what processes are running on each instance. If you check the presence set of the `command` channel, you'll see each currently active process present with the following attached data: ```json { "ServerID": "my-server-id", "Namespace": "ablyd", - "MaxInstances": 20, - "Instances": { + "MaxProcesses": 20, + "Processes": { "3490348": "Running", "Another PID": "Running" } diff --git a/asyncapispec.yml b/asyncapispec.yml index ab37873..310fb6b 100644 --- a/asyncapispec.yml +++ b/asyncapispec.yml @@ -210,7 +210,7 @@ components: payload: type: object example: - name: new-instance + name: new-process data: MessageID: 384n92923n Pid: "83483992" @@ -228,7 +228,7 @@ components: type: string description: Indicates the action the server is peforming enum: - - new-instance + - new-process data: type: object description: Object containing details on process that has started. diff --git a/examples/bash/count.html b/examples/bash/count.html index bddd543..b738c63 100644 --- a/examples/bash/count.html +++ b/examples/bash/count.html @@ -28,7 +28,7 @@

AblyD Example

log(`Presence data of ${msg.clientId}`, msg.data); }) - commandChannel.subscribe("new-instance", (msg) => { + commandChannel.subscribe("new-process", (msg) => { if (msg.data.MessageID == MESSAGEID) { let inputChannel = ably.channels.get(`${msg.data.ChannelPrefix}${msg.data.Pid}:serverinput`); let outputChannel = ably.channels.get(`[?rewind=20]${msg.data.ChannelPrefix}${msg.data.Pid}:serveroutput`); diff --git a/libablyd/ablyd_instance_state.go b/libablyd/ablyd_instance_state.go deleted file mode 100644 index 310b620..0000000 --- a/libablyd/ablyd_instance_state.go +++ /dev/null @@ -1,9 +0,0 @@ -package libablyd - -type AblyDInstanceState struct { - ServerID string - Namespace string - - MaxInstances int - Instances map[int]string -} \ No newline at end of file diff --git a/libablyd/ablyd_instance_start_message.go b/libablyd/ablyd_process_start_message.go similarity index 77% rename from libablyd/ablyd_instance_start_message.go rename to libablyd/ablyd_process_start_message.go index 05b10af..e7707f6 100644 --- a/libablyd/ablyd_instance_start_message.go +++ b/libablyd/ablyd_process_start_message.go @@ -1,6 +1,6 @@ package libablyd -type AblyDInstanceStartMessage struct { +type AblyDProcessStartMessage struct { MessageID string `json:"messageId"` ServerID string `json:"serverID"` Args [] string `json:"args"` diff --git a/libablyd/ablyd_process_state.go b/libablyd/ablyd_process_state.go new file mode 100644 index 0000000..7becf08 --- /dev/null +++ b/libablyd/ablyd_process_state.go @@ -0,0 +1,9 @@ +package libablyd + +type AblyDProcessState struct { + ServerID string + Namespace string + + MaxProcesses int + Processes map[int]string +} \ No newline at end of file diff --git a/libablyd/handler.go b/libablyd/handler.go index a7d6ee6..da974eb 100644 --- a/libablyd/handler.go +++ b/libablyd/handler.go @@ -14,7 +14,7 @@ type AblyDHandler struct { ablyRealtime *ably.Realtime // Ably Realtime Instance ablyCommandChannel *ably.RealtimeChannel - ablyDState AblyDInstanceState // Active instances running + ablyDState AblyDProcessState // Active instances running log *LogScope @@ -26,7 +26,7 @@ func NewAblyDHandler(ablyRealtime *ably.Realtime, config *ProcessConfig, newLog ablyDHandler.command = config.CommandName ablyDHandler.startUpCommandChannel(ablyRealtime) - ablyDHandler.ablyDState = AblyDInstanceState{config.ServerID, config.ChannelNamespace, config.MaxForks, make(map[int]string)} + ablyDHandler.ablyDState = AblyDProcessState{config.ServerID, config.ChannelNamespace, config.MaxForks, make(map[int]string)} ablyDHandler.enterPresence() @@ -50,15 +50,15 @@ func (ablyDHandler *AblyDHandler) ListenForCommands(wg *sync.WaitGroup) { // Subscribe to messages sent on the channel ablyDHandler.ablyCommandChannel.Subscribe(context.Background(), "start", func(msg *ably.Message) { stringData := msg.Data.(string) - data := AblyDInstanceStartMessage{} + data := AblyDProcessStartMessage{} json.Unmarshal([]byte(stringData), &data) if data.MessageID != "" { if data.ServerID != "" && data.ServerID != ablyDHandler.config.ServerID { return } - if (ablyDHandler.ablyDState.MaxInstances <= len(ablyDHandler.ablyDState.Instances)) { - ablyDHandler.ablyCommandChannel.Publish(context.Background(), "Error", "Failed to create new instance: Max instances reached") + if (ablyDHandler.ablyDState.MaxProcesses <= len(ablyDHandler.ablyDState.Processes)) { + ablyDHandler.ablyCommandChannel.Publish(context.Background(), "Error", "Failed to create new process: Max processes reached") } else { // TODO: Should use msg ID not data, // but this does not currently work https://github.com/ably/ably-go/issues/58 @@ -97,21 +97,22 @@ func (ablyDHandler *AblyDHandler) Accept(messageID string, args []string) { // Enter presence of serverinput channelInput.Presence.Enter(context.Background(), "") + channelOutput.Presence.Enter(context.Background(), "") ablyEndpoint := NewAblyEndpoint(channelInput, channelOutput, log) - newInstanceMessage := &NewInstanceMessage{MessageID: messageID, Pid: pid, + newProcessMessage := &NewProcessMessage{MessageID: messageID, Pid: pid, Namespace: ablyDHandler.config.ChannelNamespace, ChannelPrefix: ablyDHandler.config.ChannelPrefix} - ablyDHandler.ablyCommandChannel.Publish(context.Background(), "new-instance", newInstanceMessage) + ablyDHandler.ablyCommandChannel.Publish(context.Background(), "new-process", newProcessMessage) - // Add to our list of active instances - ablyDHandler.ablyDState.Instances[launched.cmd.Process.Pid] = "Running" + // Add to our list of active processes + ablyDHandler.ablyDState.Processes[launched.cmd.Process.Pid] = "Running" ablyDHandler.ablyCommandChannel.Presence.Update(context.Background(), ablyDHandler.ablyDState) PipeEndpoints(process, ablyEndpoint) - delete(ablyDHandler.ablyDState.Instances, launched.cmd.Process.Pid) + delete(ablyDHandler.ablyDState.Processes, launched.cmd.Process.Pid) ablyDHandler.ablyCommandChannel.Presence.Update(context.Background(), ablyDHandler.ablyDState) channelInput.Detach(context.Background()) channelOutput.Detach(context.Background()) diff --git a/libablyd/new_instance_message.go b/libablyd/new_process_message.go similarity index 75% rename from libablyd/new_instance_message.go rename to libablyd/new_process_message.go index d4cd373..b0bdb4d 100644 --- a/libablyd/new_instance_message.go +++ b/libablyd/new_process_message.go @@ -1,6 +1,6 @@ package libablyd -type NewInstanceMessage struct { +type NewProcessMessage struct { MessageID string Pid string Namespace string