Forums

always on task and database

hi,

i have a script in my root directory picture of directory and code whenever i make calls to my database via these methods accounts = retrieve_poe_accounts() i get this error https://pastebin.com/k9ndq7Fe

I'm 99% sure it;s because my database and/or app isn;t initialized which makes sense but i don't know how to init when when the script is running in an always on task / by itself outside of my app.

any help would be great! thank you

What code do you have to initialize it inside your app? (Stripped of any private information like passwords, of course.)

this at the moment:

import os
from dotenv import load_dotenv
from flask import Flask
from extensions import database, commands
from config import ProductionConfig, DevelopmentConfig

from blueprints.home.views import home
from blueprints.poe_leaderboard.views import poe_leaderboard

load_dotenv()

if os.environ.get('THIS_ENV', 'DevelopmentConfig') == 'ProductionConfig':
    config = ProductionConfig
else:
    config = DevelopmentConfig

def create_app():
    app = Flask(__name__)
    app.config.from_object(config)

    database.init_app(app)
    commands.init_app(app)

    app.register_blueprint(home)
    app.register_blueprint(poe_leaderboard)

    return app

if __name__ == "__main__":
    create_app().run()
else:
    app = create_app()

I might try calling the task with my virtual env specified?

calling it with my virtual env didnt work either

always on script

import os
import requests
import time
from sd_games import app
import extensions
from extensions.commands import retrieve_poe_accounts, create_character_entry, does_character_already_exist, if_character_already_exists_update_fields

def get_poe_account_character_data():
    with app.app_context():
        accounts = retrieve_poe_accounts()
        for user in accounts:
            time.sleep(1)

            url_profile = 'https://api.pathofexile.com/character'
            headers = {
                'Authorization': 'Bearer ' + user.access_token,
                'User-Agent' : 'OAuth '+os.environ.get('OAUTH_CREDENTIALS_ID')+'/1.0.0 (contact: stefan@sdgames.co) StrictMode'
            }
            response_character = requests.get(url_profile, headers=headers)

            if response_character.status_code == 200:
                character_data = response_character.json()
            else:
                return 'Error ' + str(response_character.status_code)

            account = user.username

            for character in character_data['characters']:
                character_name = character['name']
                level = character['level']
                experience = character['experience']
                league = character['league']

                if does_character_already_exist(character_name):
                    if_character_already_exists_update_fields(character_name, level, experience)
                else:
                    create_character_entry(account, character_name, level, league, experience)

while True:
    get_poe_account_character_data()
    time.sleep(1)

by the way i can run my app and the script grab_character_data.py on their own and they both work

Thanks for the details! I think that what you'll need to do is use the "flask" command, which allows you to run code that is configured like your website code (with all of the config loaded and so on). Here are the official docs, but they're a bit terse; but when I googled for "flask custom commands tutorial" I saw a bunch of results that looked like they might be useful for you.

Hi, hmm I have custom commands but not sure how that would help? I would run that outside of an alwaysontask? like an alwaysontask but just run it through a gitbash?

It looks like something is wrong with that database configuration when you run your code as a script How do you run it in bash and how do you run it in alwaysontask. I would start with checking if your environment is actually populated from dotenv or if some relative vs absolute path problem related to working directory makes it not populated or something like that.

thanks! i ended up fixing the above!

Glad to hear that!