Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hub
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package registry
Operate
Terraform modules
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
thomasDOTwtf
hub
Commits
06e7cc2a
Commit
06e7cc2a
authored
Jul 14, 2023
by
HeJ
Browse files
Options
Downloads
Patches
Plain Diff
housekeeping: --forever mode & schedule importing
parent
9564ef00
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
src/core/management/commands/housekeeping.py
+67
-3
67 additions, 3 deletions
src/core/management/commands/housekeeping.py
with
67 additions
and
3 deletions
src/core/management/commands/housekeeping.py
+
67
−
3
View file @
06e7cc2a
import
time
from
django.core.management.base
import
BaseCommand
from
django.utils
import
timezone
from
...models.messages
import
DirectMessage
from
...models.schedules
import
ScheduleSource
,
ScheduleSourceImport
from
...models.voucher
import
Voucher
class
Command
(
BaseCommand
):
def
handle
(
self
,
*
args
,
**
options
):
def
add_arguments
(
self
,
parser
):
parser
.
add_argument
(
'
--forever
'
,
action
=
'
store_true
'
,
help
=
'
repeat the housekeeping forever (until Ctrl+C is pressed)
'
)
parser
.
add_argument
(
'
--forever-delay
'
,
type
=
int
,
default
=
300
,
help
=
'
seconds to wait between housekeeping runs
'
)
def
_do_housekeeping
(
self
):
# clear all direct messages which are after their expiry date
print
(
'
Deleting messages ...
'
,
end
=
''
,
flush
=
True
)
deleted_msgs_count
,
_
=
DirectMessage
.
objects
.
filter
(
autodelete_after__isnull
=
False
,
autodelete_after__lte
=
timezone
.
now
()).
delete
()
print
(
f
'
Deleted
{
deleted_msgs_count
}
messages.
'
)
print
(
deleted_msgs_count
)
# do auto-assignments
print
(
'
Auto-assigning vouchers ...
'
,
end
=
''
,
flush
=
True
)
vouchers_assigned
=
Voucher
.
do_auto_assignments
()
print
(
f
'
Auto-assigned
{
vouchers_assigned
}
vouchers.
'
)
print
(
vouchers_assigned
)
# schedules
print
(
'
Schedule imports ...
'
,
end
=
''
,
flush
=
True
)
schedule_results
=
{}
schedule_failure
,
schedule_success
,
schedule_skipped
=
0
,
0
,
0
for
schedule
in
ScheduleSource
.
objects
.
all
():
# skip schedules which aren't due yet
if
not
schedule
.
is_due
:
continue
# skip schedules where an import is already running
if
schedule
.
has_running_import
:
schedule_results
[
str
(
schedule
)]
=
None
schedule_skipped
+=
1
continue
# create import job
job
=
ScheduleSourceImport
(
schedule_source
=
schedule
,
state
=
ScheduleSourceImport
.
State
.
PREPARED
)
job
.
save
()
# execute job
try
:
result
=
job
.
do_import
()
schedule_results
[
str
(
schedule
)]
=
job
.
summary
if
result
:
schedule_success
+=
1
else
:
schedule_failure
+=
1
except
Exception
as
err
:
schedule_results
[
str
(
schedule
)]
=
'
EXCEPTION:
'
+
str
(
err
)
schedule_failure
+=
1
# print schedule import results
print
(
schedule_success
,
'
/
'
,
len
(
schedule_results
),
'
(
'
,
schedule_skipped
,
'
skipped)
'
,
sep
=
''
)
for
k
,
v
in
schedule_results
.
items
():
print
(
'
'
,
k
,
'
=>
'
,
v
,
sep
=
''
)
def
handle
(
self
,
*
args
,
**
options
):
# call _do_housekeeping repeatedly (unless --forever is not set)
forever
=
options
.
get
(
'
forever
'
)
while
True
:
if
forever
:
print
(
timezone
.
now
().
isoformat
())
self
.
_do_housekeeping
()
if
forever
:
print
()
# empty line
try
:
time
.
sleep
(
options
.
get
(
'
forever_delay
'
))
except
KeyboardInterrupt
:
print
(
'
Aborted.
'
)
break
else
:
break
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