Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
TJTS5901 K23 Template
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
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
Startuplab
Courses
TJTS5901 Continuous Software Engineering
TJTS5901 K23 Template
Commits
42896375
Commit
42896375
authored
2 years ago
by
Teemu Autto
Browse files
Options
Downloads
Patches
Plain Diff
Batch job to close unclosed items.
parent
d3a7b3a8
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/tjts5901/scheduler.py
+38
-2
38 additions, 2 deletions
src/tjts5901/scheduler.py
with
38 additions
and
2 deletions
src/tjts5901/scheduler.py
+
38
−
2
View file @
42896375
...
...
@@ -4,11 +4,11 @@ This module contains the APScheduler extension.
This extension is used to schedule background tasks.
"""
from
datetime
import
timedelta
from
datetime
import
datetime
,
timedelta
import
logging
from
flask_apscheduler
import
APScheduler
from
mongoengine
import
signals
from
mongoengine
import
signals
,
Q
from
.models
import
Item
from
.items
import
handle_item_closing
...
...
@@ -33,6 +33,12 @@ def init_scheduler(app):
# ends.
signals
.
post_save
.
connect
(
_schedule_item_closing_task
,
sender
=
Item
)
# Add a batch task to close expired bids every 15 minutes. This is to ensure
# that the bids are closed even if the server is restarted.
scheduler
.
add_job
(
trigger
=
'
interval
'
,
minutes
=
15
,
func
=
_close_items
,
id
=
'
close-items
'
)
with
app
.
app_context
():
# Start the scheduler.
scheduler
.
start
()
...
...
@@ -84,3 +90,33 @@ def _schedule_item_closing_task(sender, document, **kwargs): # pylint: disable=
run_date
=
document
.
closes_at
+
timedelta
(
seconds
=
1
),
id
=
f
'
close-item-
{
document
.
id
}
'
,
)
def
_close_items
():
"""
Close expired bids.
This function is meant to be run by the APScheduler, and is not meant to be
called directly.
"""
with
scheduler
.
app
.
app_context
():
logger
.
info
(
"
Running scheduled task
'
close-items
'"
)
# Get items that are past the closing date, and are not already closed
closes_before
=
datetime
.
utcnow
()
+
timedelta
(
seconds
=
2
)
items
=
Item
.
objects
(
Q
(
closed
=
None
)
|
Q
(
closed
=
False
),
closes_at__lt
=
closes_before
).
all
()
logger
.
debug
(
"
Closing %d items
"
,
len
(
items
))
# Close each item
for
item
in
items
:
try
:
# Make sure item is not already closed
if
item
.
closed
:
continue
handle_item_closing
(
item
)
except
Exception
as
exc
:
logger
.
error
(
"
Error closing items: %s
"
,
exc
,
exc_info
=
True
,
extra
=
{
'
item_id
'
:
item
.
id
,
})
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