-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathappbatch.py
43 lines (35 loc) · 1.27 KB
/
appbatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from flask import Flask
from flask_apscheduler import APScheduler
from alabar.models import Topic, db
import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///alabar.db'
app.config['JOBS'] = [
{
'id': 'job1',
'func': 'appbatch:job1',
'args': ()
#'trigger': 'interval',
#'trigger': 'cron', 'hour': 12, 'minute': 0, 'second': 0,
#'seconds': 10
}
]
app.config['SCHEDULER_API_ENABLED'] = True
db.init_app(app)
def job1():
# Perform your batch operation here
with app.app_context():
with db.session.begin():
current_date = datetime.datetime.now()
print("current_date ", current_date)
#db.session.execute(db.update(Topic).where(Topic.end_date < current_date)
# .where(Topic.status == True)
# .values(status=False,end_date=datetime.datetime.now()))
db.session.query(Topic).filter(Topic.end_date < current_date, Topic.status == True).update(
{"status": False, "end_date": datetime.datetime.now()})
db.session.commit()
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
if __name__ == '__main__':
app.run()