Giter Club home page Giter Club logo

Comments (9)

viniciuschiele avatar viniciuschiele commented on July 18, 2024

Hi,

You can pass in your own APScheduler object to Flask-APScheduler.

from apscheduler.schedulers.background import BackgroundScheduler
from flask_apscheduler import APScheduler

scheduler = APScheduler(BackgroundScheduler())

Does it help you? If not, show me the code you are doing with APScheduler so I can show you how to do the same with Flask-APScheduler.

Cheers

from flask-apscheduler.

shr-chauhan avatar shr-chauhan commented on July 18, 2024

Thanks, got it
this is fine, but somehow I am not able to get it working on a rest post request
example structure,
api
v2_0
scheduler.py
app.py

in scheduler.py, I have a post method on a URL where I am doing this
scheduler.add_job(id='12344perform',func='background_tasks.schedule:immediate_task',trigger='interval',seconds=5)

in app.py
from flask_apscheduler import APScheduler
scheduler = APScheduler()
def create_app(config_name):
scheduler.init_app(app)
scheduler.start()

Just given the apscheduler related configuration, running it on a local flask server as of now, so when i run it create_app method gets triggered.
Dont know what am I doing wrong, but the task(background_tasks.schedule:immediate_task) is not getting triggered when I hit the URL...
Thanks for any help with this

from flask-apscheduler.

shr-chauhan avatar shr-chauhan commented on July 18, 2024

this is the structure

api-
----v2_0-
--------scheduler.py
background_tasks-
----schedule.py
app.py

from flask-apscheduler.

viniciuschiele avatar viniciuschiele commented on July 18, 2024

Hi,

If I understood well, you have an flask endpoint that adds a new task in APScheduler, this task is executed on background every 5 seconds.

This code below is an example of this and it works well for me:

from datetime import datetime
from flask import Flask, Response
from flask_apscheduler import APScheduler


def job1():
    print(datetime.now())


app = Flask(__name__)
scheduler = APScheduler()


@app.route('/add_task', methods=['POST'])
def add_task():
    task_id = str(datetime.now())

    scheduler.add_job(task_id, 'rest:job1', trigger='interval', seconds=5)
    return Response()


if __name__ == '__main__':
    scheduler.init_app(app)
    scheduler.start()

    app.run()
curl -X POST http://127.0.0.1:5000/add_task

Maybe you could enable the logs for APScheduler to see what's going on.

from flask-apscheduler.

shr-chauhan avatar shr-chauhan commented on July 18, 2024

yea task is running, but somehow not able to produce the logs for the scheduler
i tried with an example, and i used this basicConfig like this
import logging
logging.basicConfig(filename='/var/log/testsched', level=logging.DEBUG,format='[%(asctime)s]: %(levelname)s : %(message)s')

which worked fine for me, but somehow in my project log file itself is not getting generated

from flask-apscheduler.

shr-chauhan avatar shr-chauhan commented on July 18, 2024

so logging configuration is kinda tricky, so I already have a logging setup which I dont want to tweak, so because of that logging.basicConfig() is not working, can you help me in figuring out how can I make it work, without really disturbing the current logging functionality.
This is my logging_config.py file:

'''
set and configure a common module logger
'''
import logging
import logging.handlers
import os
from common import configuration

log_folder = configuration.system['log_folder']
if not os.path.exists(log_folder): os.mkdir(log_folder)
applogger = logging.getLogger(configuration.system['app_name'])

#do not propogate logs to the root logger
applogger.propagate = False
logging_level = logging.DEBUG

def configure_app_logger(debug=False):
'''configure common moudle logger'''
global applogger
global logging_level
if debug:
log_level = logging.DEBUG
log_formatter = logging.Formatter(
fmt='%(asctime)s : %(levelname)-8s %(message)-81s <- %(module)s.%(funcName)s, line %(lineno)d',
datefmt='%d/%b/%y %I:%M:%S%p')
else:
log_level = logging.INFO
log_formatter = logging.Formatter(fmt='%(asctime)s : %(levelname)-8s %(message)s',
datefmt='%d/%b/%y %I:%M:%S%p')
logging_level = log_level
file_handler = logging.handlers.WatchedFileHandler(
os.path.join(log_folder, '{a}.log'.format(a=configuration.system['app_name'])))
file_handler.setLevel(log_level)
file_handler.setFormatter(log_formatter)
console_handler = logging.StreamHandler()
console_handler.setLevel(log_level)
console_handler.setFormatter(log_formatter)
applogger.setLevel(log_level)
applogger.addHandler(file_handler)
applogger.addHandler(console_handler)

from flask-apscheduler.

viniciuschiele avatar viniciuschiele commented on July 18, 2024

If I'm not wrong, APScheduler logs its logs in the logger name apscheduler.executors.default

I think you need to set up a logger with this name.

logger = logging.getLogger('apscheduler.executors.default')
logger.addHandler(...)
logger.addHandler(...)

I'm not sure this code above will work, that is just a guess.

from flask-apscheduler.

shr-chauhan avatar shr-chauhan commented on July 18, 2024

Bang on...thanks mate
did not know about apscheduler.executors.default, as basicConfig worked for me so never looked into this....so now I created a new logger for scheduler with a different set of config and all works well for me now

Cheers

from flask-apscheduler.

viniciuschiele avatar viniciuschiele commented on July 18, 2024

I'm glad to hear that.

Cheers

from flask-apscheduler.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.