python flask script update heroku db - python

I have a flask app deployed on Heroku right now. I wrote a script to update the database everyday and scheduled it to run everyday using Heroku Scheduler. The script runs fine but I am finding that it is not updating the database.
To deploy my app I have a wsgi.py
from my_project import create_app
app = create_app()
My Procfile
web: gunicorn wsgi:app
My script just imports a function I have in my init.py which looks like this:
def job(app):
with app.app_context():
# update database and execute job
My script:
from wsgi import app
from my_project import job
job(app)
My question is why the db is not updating. Is it because I have the app_context? So am I updating the db, but not in the correct context? Is it correct to import the app from wsgi.py? Is it the correct app that is being used in Heroku?

Related

Flask will execute code in __init__.py but can't get it to run in app.py

If I understand the documentation correctly, in Python3 Flask ___init___.py is used for importing classes and can be completely empty.
I am trying to use app.py to hold my code, but I don't know how to get Flask to execute app.py. Code I put in __init__.py will execute, but I don't think I should do it that way. I have seen some posts where doing this could cause code to execute when it was just importing.
How do I get Flask to hit my app.py instead of __init__.py?
Here is my wsgi config file
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from FlaskApp import app as application
application.secret_key = 'something secret'
In my wsgi.py I just import the app (from app.py) and run it. I do have an older setup though but the approach should still apply.
from app import app
if __name__ == "__main__":
app.run()
and my app.py has the global Flask app variable along with all the other setup and Flask code (logging, routes, etc.)
app = Flask(__name__)
Did you set the FLASK_APP environment variable to your app.py file?
$ export FLASK_APP=app
$ flask run
See: https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery

Flask start-up command on azure for 'manage.py runserver'

I am unable to run the python manage.py runserver command from Azure. My manage.py code is
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from flask_migrate import upgrade as upgrade_database
from glogic import app, db, prepare_app
prepare_app(environment='ms')
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == "__main__":
manager.run()
Where glogic is the package for the bulk of my python files. The prepare app method is called from the __init__.py file which is as follows:
from glogic.config import config_env_files
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# initialises app and db
db = SQLAlchemy()
app = Flask(__name__)
# prepares the app and db environment based on config files
def prepare_app(environment='ms', p_db=db):
app.config.from_object(config_env_files[environment])
p_db.init_app(app)
# load views by importing them
from . import views
return app
Running and hosting locally it all works but I have been unable to find the correct startup command or what to put in a startup.sh file in order to run the server on Azure.
When running locally, I can start it using the command gunicorn --bind=0.0.0.0 --timeout 600 manage:app but this isn't working on Azure

Flask Heroku Correct Procfile

I'm trying to use Heroku to push my Flask app to the web. I think I have everything down, except the Procfile. Here is what my file directory looks like:
My main.py is:
from application import app
My init.py is:
from flask import Flask
from config import Config
from flask_mongoengine import MongoEngine
app = Flask(__name__)
app.config.from_object(Config)
db = MongoEngine()
db.init_app(app)
from application import routes
What is the correct Procfile command here?
Thanks

How to add custom build/deploy commands on OpenShift

I'm trying to properly build and deploy a Flask application to OpenShift.
The application uses SQLAlchemy as an ORM, and Flask-Migrate to do the database migrations.
My app resides in autoapp.py with the following content:
# -*- coding: utf-8 -*-
"""Create an application instance."""
from tcst_api.app import create_app
from tcst_api.utils import get_config_object
CONFIG = get_config_object()
application = create_app(CONFIG)
if __name__ == '__main__':
application.run()
To initialize the app and start it, I do the following locally:
export FLASK_APP=autoapp.py
flask db init
flask db migrate
flask db upgrade
flask run
On the OpenShift side, I managed to set up the FLASK_APP environment variable, and the platform also can hook the source and is able to build it.
However I don't know where can I inject the flask db ... commands into the process.
I use gunicorn to serve the app, and OpenShift manages to find.

Heroku Flask - Deploy a 'modular' app from tutorial not working, foreman start works locally

based on this structure: http://flask.pocoo.org/docs/patterns/packages/
I also tried this post: Deploying Flask app to Heroku
I am having trouble getting this to work on heroku. I usually get the PORT does not set within 60 seconds error. I have read other SO posts and just can't figure out if my project structure is wrong or my procfile. I tried other ports than 5000 as well.
Here is my current project structure:
/myapplication
Procfile
runserver.py
/applicationfolder
__init__.py
views.py
Here is my Procfile
web: python runserver.py $PORT
Here is my runserver.py
from applicationfolder import app
app.run()
if __name__ == '__main__':
import os
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
Here is my init.py
import os
from flask import Flask
from flask import render_template, jsonify, request
app = Flask(__name__)
app.config.from_object('config')
import applicationfolder.views
From there views.py runs.
This works locally with foreman start and python runserver.py, but does not work with heroku. I have tried many things with PORT but port doesn't seem to set even with a different PORT than 5000. I think it has something to do with my project structure.
The app.run() was in there twice, which as you noted is what's screwing things up. The app.run() invokes a simply pure-python development server so that you can easily run and/or debug your script.
By invoking it at the module level (right under your import in runserver.py), you were effectively trying to start the development server as the python code was loaded, and then when it went to run it when invoked from the Procfile, the development server was already in flight, having been starting with it's defaults (latest version of Flask is pulling relevant defaults from the SERVER_NAME environment variable). By having it in both places, you were trying to invoke that method twice.
You basically want either the straight up module load (in which case, kill off the code under "if name ...", or you use the code when invoking under main, in which case don't start the service at module load time.

Categories