Skip to content

Commit

Permalink
Switch from manage.py to flask cli (#611)
Browse files Browse the repository at this point in the history
* Switch from manage.py to flask cli

* Added shebang

* Updated readme

* updated import and removed obsolete files
  • Loading branch information
Bibhas committed May 20, 2021
1 parent 57393c0 commit 0bd76fb
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 85 deletions.
57 changes: 7 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,6 @@ Hasjob requires a `FLASK_ENV` environment variable set to one of the following v

In a production environment, you must set `FLASK_ENV` globally for it to be available across processes. On Ubuntu/Debian systems, add it to `/etc/environment` and reboot.

### With Docker

#### Install and run with Docker

- Install [Docker](https://docs.docker.com/installation/) and [Compose](https://docs.docker.com/compose/install/)

- Next, rename the `instance/development.docker.py` to `instance/development.py`

- Build the images

```
$ docker-compose build
```

- Initialize the database

```
$ docker-compose run web sh
web$ python manage.py db create
web$ exit
```

- Start the server

```
$ docker-compose up
```

- You can edit the server name and Lastuser settings in `docker-compose.yml`

### Without Docker

Hasjob without Docker requires manual installation of all dependencies.

#### Postgres and Redis

Hasjob requires Postgres >= 9.4 and Redis. To set up a Postgres DB:
Expand Down Expand Up @@ -117,23 +83,14 @@ If you intend to actively contribute to Hasjob code, some functionality is sourc
$ for DIR in coaster baseframe flask-lastuser; do cd $DIR; python setup.py develop; cd ..; done
$ cd baseframe && make && cd ..

Finish configuration with:

$ python manage.py db create

You will need to install all dependencies listed in `package.json`

$ cd hasjob/assets
$ npm install

You will need to run Webpack to bundle CSS, JS files & generate the service-worker.js
You will need to install all dependencies, run Webpack to bundle CSS, JS files & generate the service-worker.js

$ cd hasjob/assets
$ yarn build
$ cd <project root>
$ make

Before you run the server in development mode, make sure you have Postgres server and Redis server running as well. To start Hasjob:

$ python runserver.py
$ ./runserver.sh

### Create root board

Expand All @@ -143,9 +100,9 @@ Some functionality in Hasjob requires the presence of a sub-board named `www`. C

Hasjob requires some tasks to be run in periodic background jobs. These can be called from cron. Use `crontab -e` as the user account running Hasjob and add:

*/10 * * * * cd /path/to/hasjob; python manage.py periodic sessions
*/5 * * * * cd /path/to/hasjob; python manage.py periodic impressions
0 2 * * * cd /path/to/hasjob; python manage.py periodic campaignviews
*/10 * * * * cd /path/to/hasjob; flask periodic sessions
*/5 * * * * cd /path/to/hasjob; flask periodic impressions
0 2 * * * cd /path/to/hasjob; flask periodic campaignviews

### Testing

Expand Down
2 changes: 1 addition & 1 deletion hasjob/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

# Third, after config, import the models and views

from . import models, views # NOQA # isort:skip
from . import cli, models, views # NOQA # isort:skip
from .models import db # NOQA # isort:skip

# Configure the app
Expand Down
36 changes: 18 additions & 18 deletions manage.py → hasjob/cli.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

from datetime import timedelta

from coaster.manage import Manager, init_manager
from flask.cli import AppGroup

from coaster.utils import utcnow
from hasjob import app
from hasjob.models import db
import hasjob
import hasjob.forms as forms
import hasjob.models as models
import hasjob.views as views

periodic = Manager(usage="Periodic tasks from cron (with recommended intervals)")
from . import app, models, views
from .models import db


@app.shell_context_processor
def shell_context():
return {'db': db, 'models': models}


periodic = AppGroup(
'periodic', help="Periodic tasks from cron (with recommended intervals)"
)


@periodic.command
@periodic.command('sessions')
def sessions():
"""Sweep user sessions to close all inactive sessions (10m)"""
es = models.EventSession
Expand All @@ -25,22 +31,16 @@ def sessions():
db.session.commit()


@periodic.command
@periodic.command('impressions')
def impressions():
"""Recount impressions for jobposts in the dirty list (5m)"""
views.helper.update_dirty_impression_counts()


@periodic.command
@periodic.command('campaignviews')
def campaignviews():
"""Reset campaign views after more than 30 days since last view (1d)"""
views.helper.reset_campaign_views()


if __name__ == '__main__':
db.init_app(app)
manager = init_manager(
app, db, hasjob=hasjob, models=models, forms=forms, views=views
)
manager.add_command('periodic', periodic)
manager.run()
app.cli.add_command(periodic)
2 changes: 1 addition & 1 deletion hasjob/views/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ def campaign_view_count_update(campaign_id, user_id=None, anon_user_id=None):
db.session.commit()


def reset_campaign_views(): # Periodic job (see manage.py)
def reset_campaign_views(): # Periodic job
live_campaigns = Campaign.query.filter(Campaign.state.is_live).options(
db.load_only(Campaign.id)
)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ known_first_party = ['baseframe', 'coaster', 'flask_lastuser', 'hasjob']
known_sqlalchemy = ['alembic', 'sqlalchemy', 'sqlalchemy_utils', 'flask_sqlalchemy', 'psycopg2']
known_flask = [
'flask',
'click',
'werkzeug',
'itsdangerous',
'wtforms',
Expand Down
4 changes: 2 additions & 2 deletions rq.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
#!/bin/sh

rqworker -c rqinit hasjob
flask rq worker hasjob
11 changes: 0 additions & 11 deletions runserver.py

This file was deleted.

5 changes: 5 additions & 0 deletions runserver.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
export FLASK_ENV=development
export FLASK_RUN_HOST=0.0.0.0
export FLASK_RUN_PORT=5000
flask run
3 changes: 1 addition & 2 deletions website.py → wsgi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import os.path
import sys

from hasjob import app as application

__all__ = ['application']

sys.path.insert(0, os.path.dirname(__file__))
from hasjob import app as application # isort:skip

0 comments on commit 0bd76fb

Please sign in to comment.