Refactor to untangle celery code from flask code

Also:
- Also make all connections be created only on demand
- Close connections when they are no longer required
Change-Id: I8a04a90d2633360f13a78498fda36a5d97fa87cf