Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
elp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
崔为之
elp
Commits
9bdb96fb
Commit
9bdb96fb
authored
Nov 07, 2023
by
崔为之
💪🏽
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update project
parent
d1a29e12
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
87 additions
and
48 deletions
+87
-48
application/__init__.py
application/__init__.py
+19
-0
application/extensions/__init__.py
application/extensions/__init__.py
+2
-2
application/extensions/init_celery.py
application/extensions/init_celery.py
+11
-2
application/libs/__init__.py
application/libs/__init__.py
+0
-1
application/libs/tasks/__init__.py
application/libs/tasks/__init__.py
+0
-2
application/libs/tasks/task.py
application/libs/tasks/task.py
+6
-41
application/utils/__init__.py
application/utils/__init__.py
+1
-0
application/utils/celery/__init__.py
application/utils/celery/__init__.py
+13
-0
application/utils/celery/celery.py
application/utils/celery/celery.py
+35
-0
No files found.
application/__init__.py
View file @
9bdb96fb
...
...
@@ -17,6 +17,8 @@ from flask import Flask
from
application.extensions
import
init_plugs
from
application.views
import
init_views
from
application.script
import
init_script
from
application.utils
import
make_celery
from
celery.schedules
import
crontab
def
create_app
()
->
Flask
:
...
...
@@ -36,6 +38,23 @@ def create_app() -> Flask:
# This could include things like database connectors, authentication systems, etc.
init_plugs
(
app
)
celery
=
make_celery
(
app
)
celery
.
conf
.
update
(
app
.
config
)
@
celery
.
task
(
bind
=
True
)
def
update_database
(
self
):
# Add your database operations here
print
(
"update database"
)
celery
.
conf
.
beat_schedule
=
{
'update-database-every-minute'
:
{
'task'
:
'application.__init__.update_database'
,
'schedule'
:
crontab
(
minute
=
'*'
)
# Execute every minute
}
}
app
.
celery
=
celery
# Register the routes that this application will respond to.
# This includes both the route URLs and the handlers for each route.
init_views
(
app
)
...
...
application/extensions/__init__.py
View file @
9bdb96fb
...
...
@@ -21,7 +21,7 @@ from .init_migrate import init_migrate
from
.init_apispec
import
init_apispec
from
.init_marshmallow
import
init_marshmallow
from
.init_elasticsearch
import
init_elasticsearch
from
.init_celery
import
init_celery
#
from .init_celery import init_celery
def
init_plugs
(
app
:
Flask
)
->
None
:
...
...
@@ -34,4 +34,4 @@ def init_plugs(app: Flask) -> None:
init_marshmallow
(
app
)
init_elasticsearch
(
app
)
init_cors
(
app
)
init_celery
(
app
)
#
init_celery(app)
application/extensions/init_celery.py
View file @
9bdb96fb
...
...
@@ -11,9 +11,18 @@
"""
from
flask
import
Flask
from
celery.schedules
import
crontab
from
application.
lib
s
import
make_celery
from
application.
util
s
import
make_celery
def
init_celery
(
app
:
Flask
)
->
None
:
app
.
celery
=
make_celery
(
app
)
celery
=
make_celery
(
app
)
app
.
celery
=
celery
celery
.
conf
.
beat_schedule
=
{
'update-database-every-day'
:
{
'task'
:
'application.libs.tasks.task.update_database'
,
'schedule'
:
crontab
(
minute
=
'*'
)
# Execute every minute
}
}
application/libs/__init__.py
View file @
9bdb96fb
...
...
@@ -14,4 +14,3 @@ from .config import ConsulConfig, LocalConfig
from
.error
import
ConfigKeyError
from
.flask_elasticsearch
import
FlaskElasticsearch
from
.flask_loguru
import
FlaskLoguru
from
.tasks
import
make_celery
application/libs/tasks/__init__.py
View file @
9bdb96fb
...
...
@@ -9,5 +9,3 @@
# @File : __init__.py.py
# @Description :
"""
from
.task
import
make_celery
application/libs/tasks/task.py
View file @
9bdb96fb
...
...
@@ -11,46 +11,11 @@
"""
from
celery
import
Celery
from
celery.schedules
import
crontab
from
flask
import
Flask
from
flask
import
current_app
from
application.utils
import
RedisUri
def
make_celery
(
app
:
Flask
)
->
Celery
:
cfg
=
app
.
config
.
Redis
uri
=
RedisUri
(
username
=
cfg
.
Username
,
password
=
cfg
.
Password
,
host
=
cfg
.
Host
,
port
=
int
(
cfg
.
Port
),
db
=
int
(
cfg
.
DB
),
)
print
(
uri
)
celery
=
Celery
(
app
.
import_name
,
backend
=
'redis://localhost:6379/0'
,
broker
=
'redis://localhost:6379/0'
)
celery
.
conf
.
update
(
app
.
config
)
class
ContextTask
(
celery
.
Task
):
def
__call__
(
self
,
*
args
,
**
kwargs
):
with
app
.
app_context
():
return
self
.
run
(
*
args
,
**
kwargs
)
@
celery
.
task
(
base
=
ContextTask
)
def
update_database
():
# Add your database operations here
print
(
1232141241421
)
celery
.
conf
.
beat_schedule
=
{
'update-database-every-day'
:
{
'task'
:
'update_database'
,
'schedule'
:
crontab
(
minute
=
'*'
)
# Execute every minute
}
}
return
celery
@
current_app
.
celery
.
task
def
update_database
():
# Add your database operations here
print
(
123312312
)
pass
application/utils/__init__.py
View file @
9bdb96fb
...
...
@@ -12,3 +12,4 @@
from
.dsn
import
DatabaseUri
,
RedisUri
from
.elasticsearch
import
ElasticsearchUtil
from
.celery
import
make_celery
application/utils/celery/__init__.py
0 → 100644
View file @
9bdb96fb
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version : Python 3.11.4
# @Software : Sublime Text 4
# @Author : StudentCWZ
# @Email : StudentCWZ@outlook.com
# @Date : 2023/11/7 17:15
# @File : __init__.py.py
# @Description :
"""
from
.celery
import
make_celery
application/utils/celery/celery.py
0 → 100644
View file @
9bdb96fb
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
# @Version : Python 3.11.4
# @Software : Sublime Text 4
# @Author : StudentCWZ
# @Email : StudentCWZ@outlook.com
# @Date : 2023/11/7 17:15
# @File : celery.py
# @Description :
"""
from
celery
import
Celery
,
Task
from
flask
import
Flask
def
make_celery
(
app
:
Flask
):
celery
=
Celery
(
app
.
import_name
,
backend
=
'redis://localhost:6379/13'
,
broker
=
'redis://localhost:6379/13'
)
celery
.
conf
.
update
(
app
.
config
)
class
ContextTask
(
celery
.
Task
):
def
__call__
(
self
,
*
args
,
**
kwargs
):
with
app
.
app_context
():
return
self
.
run
(
*
args
,
**
kwargs
)
celery
.
conf
.
update
({
'task_cls'
:
ContextTask
,
})
return
celery
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