Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
D
douban-api-proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
douban-api-proxy
Commits
508f49df
Commit
508f49df
authored
Aug 24, 2020
by
blitzmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove completed tasks and sort the tasks by date added when calling `.tasks`
parent
bec280c6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
25 deletions
+28
-25
helper.py
cps/helper.py
+1
-1
worker.py
cps/services/worker.py
+27
-24
No files found.
cps/helper.py
View file @
508f49df
...
@@ -723,7 +723,7 @@ def format_runtime(runtime):
...
@@ -723,7 +723,7 @@ def format_runtime(runtime):
# helper function to apply localize status information in tasklist entries
# helper function to apply localize status information in tasklist entries
def
render_task_status
(
tasklist
):
def
render_task_status
(
tasklist
):
renderedtasklist
=
list
()
renderedtasklist
=
list
()
for
user
,
task
in
tasklist
:
for
user
,
added
,
task
in
tasklist
:
if
user
==
current_user
.
nickname
or
current_user
.
role_admin
():
if
user
==
current_user
.
nickname
or
current_user
.
role_admin
():
ret
=
{}
ret
=
{}
if
task
.
start_time
:
if
task
.
start_time
:
...
...
cps/services/worker.py
View file @
508f49df
from
__future__
import
division
,
print_function
,
unicode_literals
from
__future__
import
division
,
print_function
,
unicode_literals
import
threading
import
threading
import
abc
import
uuid
try
:
try
:
import
queue
import
queue
except
ImportError
:
except
ImportError
:
import
Queue
as
queue
import
Queue
as
queue
from
datetime
import
datetime
from
datetime
import
datetime
from
collections
import
namedtuple
from
cps
import
calibre_db
from
cps
import
calibre_db
from
cps
import
logger
from
cps
import
logger
import
abc
log
=
logger
.
create
()
log
=
logger
.
create
()
...
@@ -20,6 +22,11 @@ STAT_FAIL = 1
...
@@ -20,6 +22,11 @@ STAT_FAIL = 1
STAT_STARTED
=
2
STAT_STARTED
=
2
STAT_FINISH_SUCCESS
=
3
STAT_FINISH_SUCCESS
=
3
# Only retain this many tasks in dequeued list
TASK_CLEANUP_TRIGGER
=
20
QueuedTask
=
namedtuple
(
'QueuedTask'
,
'user, added, task'
)
def
_get_main_thread
():
def
_get_main_thread
():
for
t
in
threading
.
enumerate
():
for
t
in
threading
.
enumerate
():
...
@@ -51,10 +58,7 @@ class WorkerThread(threading.Thread):
...
@@ -51,10 +58,7 @@ class WorkerThread(threading.Thread):
def
__init__
(
self
):
def
__init__
(
self
):
threading
.
Thread
.
__init__
(
self
)
threading
.
Thread
.
__init__
(
self
)
self
.
finished
=
list
()
self
.
dequeued
=
list
()
self
.
db_queue
=
queue
.
Queue
()
calibre_db
.
add_queue
(
self
.
db_queue
)
self
.
doLock
=
threading
.
Lock
()
self
.
doLock
=
threading
.
Lock
()
self
.
queue
=
ImprovedQueue
()
self
.
queue
=
ImprovedQueue
()
...
@@ -64,43 +68,41 @@ class WorkerThread(threading.Thread):
...
@@ -64,43 +68,41 @@ class WorkerThread(threading.Thread):
@
classmethod
@
classmethod
def
add
(
cls
,
user
,
task
):
def
add
(
cls
,
user
,
task
):
ins
=
cls
.
getInstance
()
ins
=
cls
.
getInstance
()
ins
.
queue
.
put
((
user
,
task
))
ins
.
queue
.
put
(
QueuedTask
(
user
=
user
,
added
=
datetime
.
now
(),
task
=
task
,
))
@
property
@
property
def
tasks
(
self
):
def
tasks
(
self
):
with
self
.
doLock
:
with
self
.
doLock
:
tasks
=
list
(
self
.
queue
.
to_list
())
+
self
.
finish
ed
tasks
=
list
(
self
.
queue
.
to_list
())
+
self
.
dequeu
ed
return
tasks
# todo: order by data added
return
sorted
(
tasks
,
key
=
lambda
x
:
x
.
added
)
# Main thread loop starting the different tasks
# Main thread loop starting the different tasks
def
run
(
self
):
def
run
(
self
):
main_thread
=
_get_main_thread
()
main_thread
=
_get_main_thread
()
while
main_thread
.
is_alive
():
while
main_thread
.
is_alive
():
user
,
item
=
self
.
queue
.
get
()
item
=
self
.
queue
.
get
()
# add to list so that in-progress tasks show up
# add to list so that in-progress tasks show up
with
self
.
doLock
:
with
self
.
doLock
:
self
.
finished
.
append
((
user
,
item
))
# Remove completed tasks if needed
if
len
(
self
.
dequeued
)
>
TASK_CLEANUP_TRIGGER
:
# sort first (just to be certain), then lob off the extra
self
.
dequeued
=
sorted
(
self
.
dequeued
,
key
=
lambda
x
:
x
.
added
)[
-
1
*
TASK_CLEANUP_TRIGGER
:]
self
.
dequeued
.
append
(
item
)
user
,
added
,
task
=
item
# sometimes tasks (like Upload) don't actually have work to do and are created as already finished
# sometimes tasks (like Upload) don't actually have work to do and are created as already finished
if
item
.
stat
is
STAT_WAITING
:
if
task
.
stat
is
STAT_WAITING
:
# CalibreTask.start() should wrap all exceptions in it's own error handling
# CalibreTask.start() should wrap all exceptions in it's own error handling
item
.
start
(
self
)
task
.
start
(
self
)
self
.
queue
.
task_done
()
self
.
queue
.
task_done
()
def
_delete_completed_tasks
(
self
):
raise
NotImplementedError
()
# for index, task in reversed(list(enumerate(self.UIqueue))):
# if task['progress'] == "100 %":
# # delete tasks
# self.queue.pop(index)
# self.UIqueue.pop(index)
# # if we are deleting entries before the current index, adjust the index
# if index <= self.current and self.current:
# self.current -= 1
# self.last = len(self.queue)
class
CalibreTask
:
class
CalibreTask
:
__metaclass__
=
abc
.
ABCMeta
__metaclass__
=
abc
.
ABCMeta
...
@@ -111,6 +113,7 @@ class CalibreTask:
...
@@ -111,6 +113,7 @@ class CalibreTask:
self
.
start_time
=
None
self
.
start_time
=
None
self
.
end_time
=
None
self
.
end_time
=
None
self
.
message
=
message
self
.
message
=
message
self
.
id
=
uuid
.
uuid4
()
@
abc
.
abstractmethod
@
abc
.
abstractmethod
def
run
(
self
,
worker_thread
):
def
run
(
self
,
worker_thread
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment