Created
June 12, 2019 12:40
-
-
Save ahmadalsajid/fdb849a72359691ddb3835fa291d6534 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from celery import Celery | |
from celery.schedules import crontab | |
import MySQLdb | |
app = Celery('db_update', broker="pyamqp://guest@localhost//") | |
# disable UTC to use local time | |
app.conf.enable_utc = False | |
@app.task | |
def update_data(): | |
try: | |
db = MySQLdb.connect(user='root', passwd="qweqwe", db="celery_test") | |
c = db.cursor() | |
c.execute("""INSERT INTO `student_old` (`id`, `name`, `email`, `address`, `class`) VALUES (NULL, 'new user', NULL, NULL, NULL)""") | |
db.commit() | |
db.close() | |
except Exception as e: | |
print(str(e)) | |
# add "update_data" task to the beat schedule | |
app.conf.beat_schedule = { | |
"sync-db": { | |
"task": "db_update.update_data", | |
"schedule": crontab(minute='*'), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment