question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

gunicorn duplicating scheduler jobs

See original GitHub issue

Have a process in my flask app that refreshes data in a db every hour, works fine in Dev, but once I move to prod gunicorn, on startup the jobs get added multiple times… Did some research and came across this article, but not sure how I can pass the --preload flag to this through docker compose: https://stackoverflow.com/questions/16053364/make-sure-only-one-worker-launches-the-apscheduler-event-in-a-pyramid-web-app-ru

Python code:

# Set cron job to pull data every hour
with app.app_context():
    try:
        scheduler = BackgroundScheduler()
        scheduler.add_job(func=refresh_database, trigger="cron", hour='*')
        dash_app.server.logger.info('Starting cron jobs')
        scheduler.start()
    except BaseException as e:
        dash_app.server.logger.error('Error starting cron jobs: {}'.format(e))

if __name__ == '__main__':
    dash_app.run_server(host='0.0.0.0', debug=False, port=80, ssl_context=('/keys/cert.crt', '/keys/certkey'))

Docker Compose:

  fitly:
    build:
      context: ./dockercontrol-master
      dockerfile: fitly-dockerfile
    container_name: fitly
    restart: always
    depends_on:
      - mariadb
      - letsencrypt
    ports:
      - "8050:80"
    environment:
      - MODULE_NAME=index
      - VARIABLE_NAME=app
      - TZ=America/New_York
      - PUID=1001
      - PGID=100
    volumes:
      - /share/CACHEDEV2_DATA/Container/Fitly/config.ini:/app/config.ini
      - /share/CACHEDEV2_DATA/Container/Fitly/log.log:/app/log.log
      - /share/CACHEDEV2_DATA/Container/LetsEncrypt/keys:/app/keys

Also…If there is a better approach then what I am trying to do above, please share!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ethanoppcommented, Sep 16, 2019

Thanks @mtik00! Got it working with the following (didn’t need the =True portion):

  fitly:
    build:
      context: ./dockercontrol-master
      dockerfile: fitly-dockerfile
    container_name: fitly
    restart: always
    depends_on:
      - mariadb
      - letsencrypt
    ports:
      - "8050:80"
    environment:
      - MODULE_NAME=index
      - VARIABLE_NAME=app
      - TZ=America/New_York
      - GUNICORN_CMD_ARGS='--preload'
      - PUID=1001
      - PGID=100
    volumes:
      - /share/CACHEDEV2_DATA/Container/Fitly/config.ini:/app/config.ini
      - /share/CACHEDEV2_DATA/Container/Fitly/log.log:/app/log.log
      - /share/CACHEDEV2_DATA/Container/LetsEncrypt/keys:/app/keys
0reactions
tiangolocommented, Apr 10, 2020

Thanks for the help here @mtik00 ! 👏 🙇

Thanks for reporting back and closing the issue @ethanopp 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dealing with Gunicorn workers duplicating scheduled tasks
The problem is that Gunicorn uses 3 workers and when I run the app, the Apscheduler job also getting duplicated 3 times. This...
Read more >
Running ApScheduler in Gunicorn Without Duplicating Per ...
I have gunicorn running my app with 5 workers. I have a data structure that all the workers need access to that is...
Read more >
How To Serve Flask Applications with Gunicorn and Nginx on ...
In this guide, you will build a Python application using the Flask microframework on Ubuntu 18.04. The bulk of this article will be...
Read more >
Design — Gunicorn 20.1.0 documentation
It manages the list of running workers by listening for signals like TTIN, TTOU, and CHLD. TTIN and TTOU tell the master to...
Read more >
Securely Deploy a Django App With Gunicorn, Nginx, & HTTPS
Putting Your Site Online With Django, Gunicorn, and Nginx ... jobs -l [1]+ 43689 Running nohup python manage.py runserver & $ kill 43689...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found