Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Syng
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
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
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christoph Stahl
Syng
Commits
c12c6776
Commit
c12c6776
authored
2 years ago
by
Christoph Stahl
Browse files
Options
Downloads
Patches
Plain Diff
clients is no longer a global variable
parent
5ca9b3f9
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
syng/server.py
+43
-35
43 additions, 35 deletions
syng/server.py
with
43 additions
and
35 deletions
syng/server.py
+
43
−
35
View file @
c12c6776
...
@@ -119,9 +119,6 @@ class State:
...
@@ -119,9 +119,6 @@ class State:
)
)
clients
:
dict
[
str
,
State
]
=
{}
async
def
send_state
(
state
:
State
,
sid
:
str
)
->
None
:
async
def
send_state
(
state
:
State
,
sid
:
str
)
->
None
:
"""
"""
Send the current state (queue and recent-list) to sid.
Send the current state (queue and recent-list) to sid.
...
@@ -160,7 +157,7 @@ async def handle_state(sid: str) -> None:
...
@@ -160,7 +157,7 @@ async def handle_state(sid: str) -> None:
"""
"""
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
await
send_state
(
state
,
sid
)
await
send_state
(
state
,
sid
)
...
@@ -198,7 +195,7 @@ async def handle_append(sid: str, data: dict[str, Any]) -> None:
...
@@ -198,7 +195,7 @@ async def handle_append(sid: str, data: dict[str, Any]) -> None:
"""
"""
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
source_obj
=
state
.
config
.
sources
[
data
[
"
source
"
]]
source_obj
=
state
.
config
.
sources
[
data
[
"
source
"
]]
entry
=
await
source_obj
.
get_entry
(
data
[
"
performer
"
],
data
[
"
ident
"
])
entry
=
await
source_obj
.
get_entry
(
data
[
"
performer
"
],
data
[
"
ident
"
])
...
@@ -239,7 +236,7 @@ async def handle_append(sid: str, data: dict[str, Any]) -> None:
...
@@ -239,7 +236,7 @@ async def handle_append(sid: str, data: dict[str, Any]) -> None:
await
sio
.
emit
(
await
sio
.
emit
(
"
get-meta-info
"
,
"
get-meta-info
"
,
entry
,
entry
,
room
=
clients
[
room
].
sid
,
room
=
app
[
"
clients
"
]
[
room
].
sid
,
)
)
...
@@ -263,7 +260,7 @@ async def handle_meta_info(sid: str, data: dict[str, Any]) -> None:
...
@@ -263,7 +260,7 @@ async def handle_meta_info(sid: str, data: dict[str, Any]) -> None:
"""
"""
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
state
.
queue
.
update
(
state
.
queue
.
update
(
data
[
"
uuid
"
],
data
[
"
uuid
"
],
...
@@ -294,7 +291,7 @@ async def handle_get_first(sid: str) -> None:
...
@@ -294,7 +291,7 @@ async def handle_get_first(sid: str) -> None:
"""
"""
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
current
=
await
state
.
queue
.
peek
()
current
=
await
state
.
queue
.
peek
()
current
.
started_at
=
datetime
.
datetime
.
now
().
timestamp
()
current
.
started_at
=
datetime
.
datetime
.
now
().
timestamp
()
...
@@ -323,7 +320,7 @@ async def handle_pop_then_get_next(sid: str) -> None:
...
@@ -323,7 +320,7 @@ async def handle_pop_then_get_next(sid: str) -> None:
"""
"""
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
if
sid
!=
state
.
sid
:
if
sid
!=
state
.
sid
:
return
return
...
@@ -386,7 +383,7 @@ async def handle_register_client(sid: str, data: dict[str, Any]) -> None:
...
@@ -386,7 +383,7 @@ async def handle_register_client(sid: str, data: dict[str, Any]) -> None:
client_id
=
""
.
join
(
client_id
=
""
.
join
(
[
random
.
choice
(
string
.
ascii_letters
)
for
_
in
range
(
length
)]
[
random
.
choice
(
string
.
ascii_letters
)
for
_
in
range
(
length
)]
)
)
if
client_id
in
clients
:
if
client_id
in
app
[
"
clients
"
]
:
client_id
=
gen_id
(
length
+
1
)
client_id
=
gen_id
(
length
+
1
)
return
client_id
return
client_id
...
@@ -394,8 +391,8 @@ async def handle_register_client(sid: str, data: dict[str, Any]) -> None:
...
@@ -394,8 +391,8 @@ async def handle_register_client(sid: str, data: dict[str, Any]) -> None:
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
session
[
"
room
"
]
=
room
session
[
"
room
"
]
=
room
if
room
in
clients
:
if
room
in
app
[
"
clients
"
]
:
old_state
:
State
=
clients
[
room
]
old_state
:
State
=
app
[
"
clients
"
]
[
room
]
if
data
[
"
secret
"
]
==
old_state
.
secret
:
if
data
[
"
secret
"
]
==
old_state
.
secret
:
logger
.
info
(
"
Got new client connection for %s
"
,
room
)
logger
.
info
(
"
Got new client connection for %s
"
,
room
)
old_state
.
sid
=
sid
old_state
.
sid
=
sid
...
@@ -408,7 +405,7 @@ async def handle_register_client(sid: str, data: dict[str, Any]) -> None:
...
@@ -408,7 +405,7 @@ async def handle_register_client(sid: str, data: dict[str, Any]) -> None:
await
sio
.
emit
(
await
sio
.
emit
(
"
client-registered
"
,
{
"
success
"
:
True
,
"
room
"
:
room
},
room
=
sid
"
client-registered
"
,
{
"
success
"
:
True
,
"
room
"
:
room
},
room
=
sid
)
)
await
send_state
(
clients
[
room
],
sid
)
await
send_state
(
app
[
"
clients
"
]
[
room
],
sid
)
else
:
else
:
logger
.
warning
(
"
Got wrong secret for %s
"
,
room
)
logger
.
warning
(
"
Got wrong secret for %s
"
,
room
)
await
sio
.
emit
(
await
sio
.
emit
(
...
@@ -419,7 +416,7 @@ async def handle_register_client(sid: str, data: dict[str, Any]) -> None:
...
@@ -419,7 +416,7 @@ async def handle_register_client(sid: str, data: dict[str, Any]) -> None:
initial_entries
=
[
Entry
(
**
entry
)
for
entry
in
data
[
"
queue
"
]]
initial_entries
=
[
Entry
(
**
entry
)
for
entry
in
data
[
"
queue
"
]]
initial_recent
=
[
Entry
(
**
entry
)
for
entry
in
data
[
"
recent
"
]]
initial_recent
=
[
Entry
(
**
entry
)
for
entry
in
data
[
"
recent
"
]]
clients
[
room
]
=
State
(
app
[
"
clients
"
]
[
room
]
=
State
(
secret
=
data
[
"
secret
"
],
secret
=
data
[
"
secret
"
],
queue
=
Queue
(
initial_entries
),
queue
=
Queue
(
initial_entries
),
recent
=
initial_recent
,
recent
=
initial_recent
,
...
@@ -430,7 +427,7 @@ async def handle_register_client(sid: str, data: dict[str, Any]) -> None:
...
@@ -430,7 +427,7 @@ async def handle_register_client(sid: str, data: dict[str, Any]) -> None:
await
sio
.
emit
(
await
sio
.
emit
(
"
client-registered
"
,
{
"
success
"
:
True
,
"
room
"
:
room
},
room
=
sid
"
client-registered
"
,
{
"
success
"
:
True
,
"
room
"
:
room
},
room
=
sid
)
)
await
send_state
(
clients
[
room
],
sid
)
await
send_state
(
app
[
"
clients
"
]
[
room
],
sid
)
@sio.on
(
"
sources
"
)
@sio.on
(
"
sources
"
)
...
@@ -457,7 +454,7 @@ async def handle_sources(sid: str, data: dict[str, Any]) -> None:
...
@@ -457,7 +454,7 @@ async def handle_sources(sid: str, data: dict[str, Any]) -> None:
"""
"""
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
if
sid
!=
state
.
sid
:
if
sid
!=
state
.
sid
:
return
return
...
@@ -494,7 +491,7 @@ async def handle_config_chunk(sid: str, data: dict[str, Any]) -> None:
...
@@ -494,7 +491,7 @@ async def handle_config_chunk(sid: str, data: dict[str, Any]) -> None:
"""
"""
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
if
sid
!=
state
.
sid
:
if
sid
!=
state
.
sid
:
return
return
...
@@ -526,7 +523,7 @@ async def handle_config(sid: str, data: dict[str, Any]) -> None:
...
@@ -526,7 +523,7 @@ async def handle_config(sid: str, data: dict[str, Any]) -> None:
"""
"""
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
if
sid
!=
state
.
sid
:
if
sid
!=
state
.
sid
:
return
return
...
@@ -551,11 +548,11 @@ async def handle_register_web(sid: str, data: dict[str, Any]) -> bool:
...
@@ -551,11 +548,11 @@ async def handle_register_web(sid: str, data: dict[str, Any]) -> bool:
:returns: True, if the room exist, False otherwise
:returns: True, if the room exist, False otherwise
:rtype: bool
:rtype: bool
"""
"""
if
data
[
"
room
"
]
in
clients
:
if
data
[
"
room
"
]
in
app
[
"
clients
"
]
:
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
session
[
"
room
"
]
=
data
[
"
room
"
]
session
[
"
room
"
]
=
data
[
"
room
"
]
sio
.
enter_room
(
sid
,
session
[
"
room
"
])
sio
.
enter_room
(
sid
,
session
[
"
room
"
])
state
=
clients
[
session
[
"
room
"
]]
state
=
app
[
"
clients
"
]
[
session
[
"
room
"
]]
await
send_state
(
state
,
sid
)
await
send_state
(
state
,
sid
)
return
True
return
True
return
False
return
False
...
@@ -578,7 +575,7 @@ async def handle_register_admin(sid: str, data: dict[str, Any]) -> bool:
...
@@ -578,7 +575,7 @@ async def handle_register_admin(sid: str, data: dict[str, Any]) -> bool:
"""
"""
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
is_admin
:
bool
=
data
[
"
secret
"
]
==
state
.
secret
is_admin
:
bool
=
data
[
"
secret
"
]
==
state
.
secret
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
...
@@ -602,12 +599,14 @@ async def handle_skip_current(sid: str) -> None:
...
@@ -602,12 +599,14 @@ async def handle_skip_current(sid: str) -> None:
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
is_admin
=
session
[
"
admin
"
]
is_admin
=
session
[
"
admin
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
if
is_admin
:
if
is_admin
:
old_entry
=
await
state
.
queue
.
popleft
()
old_entry
=
await
state
.
queue
.
popleft
()
state
.
recent
.
append
(
old_entry
)
state
.
recent
.
append
(
old_entry
)
await
sio
.
emit
(
"
skip-current
"
,
old_entry
,
room
=
clients
[
room
].
sid
)
await
sio
.
emit
(
"
skip-current
"
,
old_entry
,
room
=
app
[
"
clients
"
][
room
].
sid
)
await
send_state
(
state
,
room
)
await
send_state
(
state
,
room
)
...
@@ -628,7 +627,7 @@ async def handle_move_up(sid: str, data: dict[str, Any]) -> None:
...
@@ -628,7 +627,7 @@ async def handle_move_up(sid: str, data: dict[str, Any]) -> None:
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
is_admin
=
session
[
"
admin
"
]
is_admin
=
session
[
"
admin
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
if
is_admin
:
if
is_admin
:
await
state
.
queue
.
move_up
(
data
[
"
uuid
"
])
await
state
.
queue
.
move_up
(
data
[
"
uuid
"
])
await
send_state
(
state
,
room
)
await
send_state
(
state
,
room
)
...
@@ -651,7 +650,7 @@ async def handle_skip(sid: str, data: dict[str, Any]) -> None:
...
@@ -651,7 +650,7 @@ async def handle_skip(sid: str, data: dict[str, Any]) -> None:
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
is_admin
=
session
[
"
admin
"
]
is_admin
=
session
[
"
admin
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
if
is_admin
:
if
is_admin
:
entry
=
state
.
queue
.
find_by_uuid
(
data
[
"
uuid
"
])
entry
=
state
.
queue
.
find_by_uuid
(
data
[
"
uuid
"
])
...
@@ -699,7 +698,7 @@ async def handle_search(sid: str, data: dict[str, Any]) -> None:
...
@@ -699,7 +698,7 @@ async def handle_search(sid: str, data: dict[str, Any]) -> None:
"""
"""
async
with
sio
.
session
(
sid
)
as
session
:
async
with
sio
.
session
(
sid
)
as
session
:
room
=
session
[
"
room
"
]
room
=
session
[
"
room
"
]
state
=
clients
[
room
]
state
=
app
[
"
clients
"
]
[
room
]
query
=
data
[
"
query
"
]
query
=
data
[
"
query
"
]
results_list
=
await
asyncio
.
gather
(
results_list
=
await
asyncio
.
gather
(
...
@@ -729,17 +728,19 @@ async def cleanup() -> None:
...
@@ -729,17 +728,19 @@ async def cleanup() -> None:
logger
.
info
(
"
Start Cleanup
"
)
logger
.
info
(
"
Start Cleanup
"
)
to_remove
:
list
[
str
]
=
[]
to_remove
:
list
[
str
]
=
[]
for
sid
,
state
in
clients
.
items
():
for
sid
,
state
in
app
[
"
clients
"
]
.
items
():
logger
.
info
(
"
Client %s, last seen: %s
"
,
sid
,
str
(
state
.
last_seen
))
logger
.
info
(
"
Client %s, last seen: %s
"
,
sid
,
str
(
state
.
last_seen
))
if
state
.
last_seen
+
datetime
.
timedelta
(
hours
=
4
)
<
datetime
.
datetime
.
now
():
if
(
state
.
last_seen
+
datetime
.
timedelta
(
hours
=
4
)
<
datetime
.
datetime
.
now
()
):
logger
.
info
(
"
No activity for 4 hours, removing %s
"
,
sid
)
logger
.
info
(
"
No activity for 4 hours, removing %s
"
,
sid
)
to_remove
.
append
(
sid
)
to_remove
.
append
(
sid
)
for
sid
in
to_remove
:
for
sid
in
to_remove
:
await
sio
.
disconnect
(
sid
)
await
sio
.
disconnect
(
sid
)
del
clients
[
sid
]
del
app
[
"
clients
"
]
[
sid
]
logger
.
info
(
"
End Cleanup
"
)
logger
.
info
(
"
End Cleanup
"
)
# The internal loop counter does not use a regular timestamp, so we need to convert between
# The internal loop counter does not use a regular timestamp, so we need to convert between
# regular datetime and the async loop time
# regular datetime and the async loop time
now
=
datetime
.
datetime
.
now
()
now
=
datetime
.
datetime
.
now
()
...
@@ -751,9 +752,14 @@ async def cleanup() -> None:
...
@@ -751,9 +752,14 @@ async def cleanup() -> None:
loop_next
=
asyncio
.
get_event_loop
().
time
()
+
offset
loop_next
=
asyncio
.
get_event_loop
().
time
()
+
offset
logger
.
info
(
"
Next Cleanup at %s
"
,
str
(
next
))
logger
.
info
(
"
Next Cleanup at %s
"
,
str
(
next
))
asyncio
.
get_event_loop
().
call_at
(
loop_next
,
lambda
:
asyncio
.
create_task
(
cleanup
()))
asyncio
.
get_event_loop
().
call_at
(
loop_next
,
lambda
:
asyncio
.
create_task
(
cleanup
())
)
async
def
background_tasks
(
iapp
:
web
.
Application
)
->
AsyncGenerator
[
None
,
None
]:
async
def
background_tasks
(
iapp
:
web
.
Application
,
)
->
AsyncGenerator
[
None
,
None
]:
"""
Create all the background tasks
"""
Create all the background tasks
For now, this is only the cleanup task
For now, this is only the cleanup task
...
@@ -766,6 +772,7 @@ async def background_tasks(iapp: web.Application) -> AsyncGenerator[None, None]:
...
@@ -766,6 +772,7 @@ async def background_tasks(iapp: web.Application) -> AsyncGenerator[None, None]:
iapp
[
"
repeated_cleanup
"
].
cancel
()
iapp
[
"
repeated_cleanup
"
].
cancel
()
await
iapp
[
"
repeated_cleanup
"
]
await
iapp
[
"
repeated_cleanup
"
]
def
main
()
->
None
:
def
main
()
->
None
:
"""
"""
Configure and start the server.
Configure and start the server.
...
@@ -785,6 +792,7 @@ def main() -> None:
...
@@ -785,6 +792,7 @@ def main() -> None:
app
.
router
.
add_route
(
"
*
"
,
"
/{room}
"
,
root_handler
)
app
.
router
.
add_route
(
"
*
"
,
"
/{room}
"
,
root_handler
)
app
.
router
.
add_route
(
"
*
"
,
"
/{room}/
"
,
root_handler
)
app
.
router
.
add_route
(
"
*
"
,
"
/{room}/
"
,
root_handler
)
app
[
"
clients
"
]
=
{}
app
.
cleanup_ctx
.
append
(
background_tasks
)
app
.
cleanup_ctx
.
append
(
background_tasks
)
web
.
run_app
(
app
,
host
=
args
.
host
,
port
=
args
.
port
)
web
.
run_app
(
app
,
host
=
args
.
host
,
port
=
args
.
port
)
...
...
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