Skip to content

Commit 3bd34a8

Browse files
committed
Initial commit
0 parents  commit 3bd34a8

13 files changed

Lines changed: 407 additions & 0 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/go/.devcontainer/base.Dockerfile
2+
3+
# [Choice] Go version (use -bullseye variants on local arm64/Apple Silicon): 1, 1.19, 1.18, 1-bullseye, 1.19-bullseye, 1.18-bullseye, 1-buster, 1.19-buster, 1.18-buster
4+
ARG VARIANT="1.19-bullseye"
5+
FROM mcr.microsoft.com/vscode/devcontainers/go:0-${VARIANT}
6+
7+
# [Choice] Node.js version: none, lts/*, 18, 16, 14
8+
ARG NODE_VERSION="none"
9+
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
10+
11+
# [Optional] Uncomment this section to install additional OS packages.
12+
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
13+
# && apt-get -y install --no-install-recommends <your-package-list-here>
14+
15+
# [Optional] Uncomment the next lines to use go get to install anything else you need
16+
# USER vscode
17+
# RUN go get -x <your-dependency-or-tool>
18+
19+
# [Optional] Uncomment this line to install global node packages.
20+
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1

.devcontainer/devcontainer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2+
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/go
3+
{
4+
"name": "Go",
5+
"build": {
6+
"dockerfile": "Dockerfile",
7+
"args": {
8+
// Update the VARIANT arg to pick a version of Go: 1, 1.19, 1.18
9+
// Append -bullseye or -buster to pin to an OS version.
10+
// Use -bullseye variants on local arm64/Apple Silicon.
11+
"VARIANT": "1-bullseye",
12+
// Options
13+
"NODE_VERSION": "none"
14+
}
15+
},
16+
"runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
17+
18+
// Configure tool-specific properties.
19+
"customizations": {
20+
// Configure properties specific to VS Code.
21+
"vscode": {
22+
// Set *default* container specific settings.json values on container create.
23+
"settings": {
24+
"go.toolsManagement.checkForUpdates": "local",
25+
"go.useLanguageServer": true,
26+
"go.gopath": "/go"
27+
},
28+
29+
// Add the IDs of extensions you want installed when the container is created.
30+
"extensions": [
31+
"golang.Go"
32+
]
33+
}
34+
},
35+
36+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
37+
// "forwardPorts": [],
38+
39+
// Use 'postCreateCommand' to run commands after the container is created.
40+
// "postCreateCommand": "go version",
41+
42+
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
43+
"remoteUser": "vscode"
44+
}

.github/workflows/ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: CI
2+
3+
on:
4+
- pull_request
5+
6+
jobs:
7+
continuous-integration:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Check out code
11+
uses: actions/checkout@v2
12+
- name: Set up Go
13+
uses: actions/setup-go@v3
14+
with:
15+
go-version-file: './go.mod'
16+
- name: Install dependencies
17+
run: go get .
18+
- name: Test with Go
19+
run: go test

.github/workflows/release.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Release
2+
3+
on:
4+
release:
5+
types: [ published ]
6+
7+
jobs:
8+
release:
9+
strategy:
10+
matrix:
11+
os: [ linux ]
12+
arch: [ 386, amd64, arm, arm64 ]
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Check out code
16+
uses: actions/checkout@v2
17+
- name: Set up Go
18+
uses: actions/setup-go@v3
19+
with:
20+
go-version-file: './go.mod'
21+
- name: Install dependencies
22+
run: go get .
23+
- name: Build for ${{ matrix.os }} ${{ matrix.arch }}
24+
run: go build -o event-queue-worker-${{ matrix.os }}-${{ matrix.arch }} .
25+
- name: Release
26+
uses: softprops/action-gh-release@v1
27+
with:
28+
files: event-queue-worker-${{ matrix.os }}-${{ matrix.arch }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.devcontainer

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/jellyfishphp/event-queue-worker
2+
3+
go 1.19

internal/event/event.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package event
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/jellyfishphp/event-queue-worker/internal/process"
8+
)
9+
10+
type Listener struct {
11+
Type string `json:"type"`
12+
Identifier string `json:"identifier"`
13+
EventName string
14+
}
15+
16+
type ListenerReaderInterface interface {
17+
GetAll() []Listener
18+
}
19+
20+
type ListenerReader struct {
21+
command string
22+
}
23+
24+
func (listenerReader *ListenerReader) GetAll() []Listener {
25+
var ungroupedListeners []Listener
26+
27+
process := process.NewProcess(listenerReader.command)
28+
output, error := process.Start()
29+
30+
if error != nil {
31+
return ungroupedListeners
32+
}
33+
34+
var groupedListeners map[string][]Listener
35+
error = json.Unmarshal(output, &groupedListeners)
36+
37+
if error != nil {
38+
return ungroupedListeners
39+
}
40+
41+
for eventName, listeners := range groupedListeners {
42+
for _, listener := range listeners {
43+
listener.EventName = eventName
44+
ungroupedListeners = append(ungroupedListeners, listener)
45+
}
46+
}
47+
48+
return ungroupedListeners
49+
}
50+
51+
func NewListenerReader(command string) ListenerReaderInterface {
52+
return &ListenerReader{
53+
command: command,
54+
}
55+
}
56+
57+
type ListenerConverterInterface interface {
58+
ConvertToProcess(listener Listener) process.ProcessInterface
59+
ConvertMultipleToProcesses(listeners []Listener) []process.ProcessInterface
60+
}
61+
62+
type ListenerConverter struct {
63+
command string
64+
}
65+
66+
func (listenerConverter *ListenerConverter) ConvertToProcess(listener Listener) process.ProcessInterface {
67+
return process.NewProcess(
68+
fmt.Sprintf(
69+
"%s %s %s",
70+
listenerConverter.command,
71+
listener.EventName,
72+
listener.Identifier,
73+
),
74+
)
75+
}
76+
77+
func (listenerConverter *ListenerConverter) ConvertMultipleToProcesses(listeners []Listener) []process.ProcessInterface {
78+
var processes []process.ProcessInterface
79+
80+
for _, listener := range listeners {
81+
processes = append(processes, listenerConverter.ConvertToProcess(listener))
82+
}
83+
84+
return processes
85+
}
86+
87+
func NewListenerConverter(command string) ListenerConverterInterface {
88+
return &ListenerConverter{
89+
command: command,
90+
}
91+
}

internal/event/event_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package event
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestGetAll(t *testing.T) {
9+
listenerReader := NewListenerReader("cat ./grouped-listeners.json")
10+
listeners := listenerReader.GetAll()
11+
12+
if len(listeners) != 2 {
13+
t.Errorf("Number of listeners is incorect. Acutal: %d, Expected: %d", len(listeners), 2)
14+
}
15+
16+
for index, listener := range listeners {
17+
expectedEventName := fmt.Sprintf("event%d", index+1)
18+
expectedIdentifier := fmt.Sprintf("identifier%d", index+1)
19+
expectedType := "async"
20+
21+
if listener.EventName != expectedEventName {
22+
t.Errorf("Event name of listener is incorect. Acutal: %s, Expected: %s", listener.EventName, expectedEventName)
23+
}
24+
25+
if listener.Identifier != expectedIdentifier {
26+
t.Errorf("Identifier of listener is incorect. Acutal: %s, Expected: %s", listener.Identifier, expectedIdentifier)
27+
}
28+
29+
if listener.Type != expectedType {
30+
t.Errorf("Type of the first listener is incorect. Acutal: %s, Expected: %s", listener.Type, expectedType)
31+
}
32+
}
33+
34+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"event1": [
3+
{
4+
"identifier": "identifier1",
5+
"type": "async"
6+
}
7+
],
8+
"event2": [
9+
{
10+
"identifier": "identifier2",
11+
"type": "async"
12+
}
13+
]
14+
}

internal/process/process.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package process
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
)
7+
8+
type ProcessInterface interface {
9+
Start() ([]byte, error)
10+
IsRunning() bool
11+
}
12+
13+
type Process struct {
14+
command string
15+
isRunning bool
16+
}
17+
18+
func (process *Process) Start() ([]byte, error) {
19+
process.isRunning = true
20+
21+
commandParts := strings.Split(process.command, " ")
22+
23+
command := exec.Command(commandParts[0], commandParts[1:]...)
24+
output, error := command.Output()
25+
26+
process.isRunning = false
27+
28+
return output, error
29+
}
30+
31+
func (process *Process) IsRunning() bool {
32+
return process.isRunning
33+
}
34+
35+
func NewProcess(command string) ProcessInterface {
36+
return &Process{
37+
command: command,
38+
isRunning: false,
39+
}
40+
}

0 commit comments

Comments
 (0)