Skip to content

Instantly share code, notes, and snippets.

@ahmadalsajid
Created June 12, 2019 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadalsajid/fdb849a72359691ddb3835fa291d6534 to your computer and use it in GitHub Desktop.
Save ahmadalsajid/fdb849a72359691ddb3835fa291d6534 to your computer and use it in GitHub Desktop.
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