Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
ems-esp-logger
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jonas Leder
ems-esp-logger
Commits
d49cc442
Commit
d49cc442
authored
1 month ago
by
Jonas Leder
Browse files
Options
Downloads
Patches
Plain Diff
extend the queue, to support elements, to stay a given time in the queue
parent
a69809c6
No related branches found
Branches containing commit
No related tags found
Loading
Pipeline
#54572
passed
1 month ago
Stage: build
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
messageworker/main.go
+2
-2
2 additions, 2 deletions
messageworker/main.go
mqttclient/main.go
+1
-1
1 addition, 1 deletion
mqttclient/main.go
queue/queue.go
+27
-7
27 additions, 7 deletions
queue/queue.go
with
30 additions
and
10 deletions
messageworker/main.go
+
2
−
2
View file @
d49cc442
...
...
@@ -30,7 +30,7 @@ func Run() {
err
=
database
.
Db
.
Where
(
"name = ?"
,
task
.
Instance
)
.
First
(
&
instance
)
.
Error
if
err
!=
nil
{
log
.
Log
.
Error
(
"Failed retreiving instance from database, pushing task back to queue: "
,
err
.
Error
())
queue
.
MainQueue
.
Enqueue
(
taskData
)
queue
.
MainQueue
.
Enqueue
(
taskData
,
60
)
continue
}
...
...
@@ -38,7 +38,7 @@ func Run() {
err
=
json
.
Unmarshal
([]
byte
(
task
.
Data
),
&
jsonData
)
if
err
!=
nil
{
log
.
Log
.
Error
(
"Failed decoding boiler JSON: "
,
err
.
Error
())
queue
.
MainQueue
.
Enqueue
(
taskData
)
queue
.
MainQueue
.
Enqueue
(
taskData
,
60
)
continue
}
for
key
,
value
:=
range
jsonData
{
...
...
This diff is collapsed.
Click to expand it.
mqttclient/main.go
+
1
−
1
View file @
d49cc442
...
...
@@ -36,7 +36,7 @@ var messagePubHandlerBoiler mqtt.MessageHandler = func(client mqtt.Client, msg m
if
err
!=
nil
{
log
.
Log
.
Error
(
"Failed encoding new data task as JSON: "
,
err
.
Error
())
}
queue
.
MainQueue
.
Enqueue
(
string
(
taskData
))
queue
.
MainQueue
.
Enqueue
(
string
(
taskData
)
,
0
)
}
}
...
...
This diff is collapsed.
Click to expand it.
queue/queue.go
+
27
−
7
View file @
d49cc442
...
...
@@ -4,16 +4,23 @@ import (
"encoding/json"
"fmt"
"os"
"time"
)
type
QueueElement
struct
{
Value
string
`json:"value"`
EnqueueTime
time
.
Time
`json:"enqueue_time"`
MinDuration
int
`json:"min_duration"`
// Minimum duration in seconds
}
type
Queue
struct
{
Elements
[]
string
Elements
[]
QueueElement
FilePath
string
}
func
NewQueue
(
filePath
string
)
(
*
Queue
,
error
)
{
q
:=
&
Queue
{
Elements
:
make
([]
string
,
0
),
Elements
:
make
([]
QueueElement
,
0
),
FilePath
:
filePath
,
}
...
...
@@ -30,17 +37,30 @@ func NewQueue(filePath string) (*Queue, error) {
return
q
,
nil
}
func
(
q
*
Queue
)
Enqueue
(
element
string
)
{
q
.
Elements
=
append
(
q
.
Elements
,
element
)
func
(
q
*
Queue
)
Enqueue
(
element
string
,
minDuration
int
)
{
q
.
Elements
=
append
(
q
.
Elements
,
QueueElement
{
Value
:
element
,
EnqueueTime
:
time
.
Now
(),
MinDuration
:
minDuration
,
})
}
func
(
q
*
Queue
)
Dequeue
()
(
string
,
error
)
{
if
len
(
q
.
Elements
)
==
0
{
return
""
,
fmt
.
Errorf
(
"queue is empty"
)
}
element
:=
q
.
Elements
[
0
]
q
.
Elements
=
q
.
Elements
[
1
:
]
return
element
,
nil
currentTime
:=
time
.
Now
()
for
i
,
elem
:=
range
q
.
Elements
{
// Check if the element has satisfied its minimum duration in the queue
if
currentTime
.
Sub
(
elem
.
EnqueueTime
)
.
Seconds
()
>=
float64
(
elem
.
MinDuration
)
{
// Remove the element from the queue
q
.
Elements
=
append
(
q
.
Elements
[
:
i
],
q
.
Elements
[
i
+
1
:
]
...
)
return
elem
.
Value
,
nil
}
}
return
""
,
fmt
.
Errorf
(
"no elements are ready for dequeuing"
)
}
func
(
q
*
Queue
)
Save
()
error
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment