Skip to content

Commit

Permalink
Merge pull request okfn-brasil#497 from oleggator/oleggator-searchvec…
Browse files Browse the repository at this point in the history
…tor-automation

Searchvector automation
  • Loading branch information
sergiomario authored Oct 19, 2019
2 parents 758adb5 + f718ad0 commit ce10d2b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 0 deletions.
8 changes: 8 additions & 0 deletions contrib/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ NEW_RELIC_ENVIRONMENT=development
NEW_RELIC_LICENSE_KEY=

INBOX_PASSWORD=

SCHEDULE_SEARCHVECTOR=True
SCHEDULE_SEARCHVECTOR_CRON_MINUTE=0
SCHEDULE_SEARCHVECTOR_CRON_HOUR=2
SCHEDULE_SEARCHVECTOR_CRON_DAY_OF_WEEK=*
SCHEDULE_SEARCHVECTOR_CRON_DAY_OF_MONTH=*/2
SCHEDULE_SEARCHVECTOR_CRON_MONTH_OF_YEAR=*

5 changes: 5 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ services:
restart: "no"
volumes:
- .:/code

beat:
restart: "no"
volumes:
- .:/code
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,15 @@ services:
hostname: tasks
image: serenata/django
restart: always

beat:
command: ["newrelic-admin", "run-program", "celery", "beat", "--app", "jarbas"]
depends_on:
- queue
env_file:
- .env
environment:
- NEW_RELIC_APP_NAME=Jarbas (Beat); Jarbas (Combined)
hostname: beat
image: serenata/django
restart: always
9 changes: 9 additions & 0 deletions jarbas/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ instructions](https://python-twitter.readthedocs.io/en/latest/getting_started.ht
* `LETSENCRYPT_EMAIL` (_str_) Email used to create the HTTPS certificate at Let's Encrypt
* `HTTPS_METHOD` (_str_) if set to `noredirect` does **not** redirect from HTTP to HTTPS (default: `redirect`)

##### Searchvector schedule

* `SCHEDULE_SEARCHVECTOR` (_bool_) Enable searchvector scheduling
* `SCHEDULE_SEARCHVECTOR_CRON_MINUTE` (_str_) Minute
* `SCHEDULE_SEARCHVECTOR_CRON_HOUR` (_str_) Hour
* `SCHEDULE_SEARCHVECTOR_CRON_DAY_OF_WEEK` (_str_) Day of week
* `SCHEDULE_SEARCHVECTOR_CRON_DAY_OF_MONTH` (_str_) Day of month
* `SCHEDULE_SEARCHVECTOR_CRON_MONTH_OF_YEAR` (_str_) Month of year

### Using Docker

You must first install [Docker](https://docs.docker.com/engine/installation/) and [Docker Compose](https://docs.docker.com/compose/install/)
Expand Down
30 changes: 30 additions & 0 deletions jarbas/celery.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,39 @@
import logging
import os
from celery import Celery
from celery.schedules import crontab

from django.conf import settings

logger = logging.getLogger('celery')

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jarbas.settings')

app = Celery('jarbas')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()


@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
from django.core import management

@app.task(ignore_result=True)
def searchvector():
logger.info('Running searchvector...')
management.call_command('searchvector')
logger.info('Searchvector is done')

if not settings.SCHEDULE_SEARCHVECTOR:
return

sender.add_periodic_task(
crontab(
minute=settings.SCHEDULE_SEARCHVECTOR_CRON_MINUTE,
hour=settings.SCHEDULE_SEARCHVECTOR_CRON_HOUR,
day_of_week=settings.SCHEDULE_SEARCHVECTOR_CRON_DAY_OF_WEEK,
day_of_month=settings.SCHEDULE_SEARCHVECTOR_CRON_DAY_OF_MONTH,
month_of_year=settings.SCHEDULE_SEARCHVECTOR_CRON_MONTH_OF_YEAR,
),
searchvector.s(),
)
9 changes: 9 additions & 0 deletions jarbas/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,12 @@
# Set home

HOMES_REDIRECTS_TO = '/dashboard/chamber_of_deputies/reimbursement/'

# Searchvector schedule

SCHEDULE_SEARCHVECTOR = config('SCHEDULE_SEARCHVECTOR', default=False, cast=bool)
SCHEDULE_SEARCHVECTOR_CRON_MINUTE = config('SCHEDULE_SEARCHVECTOR_CRON_MINUTE', default='0')
SCHEDULE_SEARCHVECTOR_CRON_HOUR = config('SCHEDULE_SEARCHVECTOR_CRON_HOUR', default='2')
SCHEDULE_SEARCHVECTOR_CRON_DAY_OF_WEEK = config('SCHEDULE_SEARCHVECTOR_CRON_DAY_OF_WEEK', default='*')
SCHEDULE_SEARCHVECTOR_CRON_DAY_OF_MONTH = config('SCHEDULE_SEARCHVECTOR_CRON_DAY_OF_MONTH', default='*/2')
SCHEDULE_SEARCHVECTOR_CRON_MONTH_OF_YEAR = config('SCHEDULE_SEARCHVECTOR_CRON_MONTH_OF_YEAR', default='*')

0 comments on commit ce10d2b

Please sign in to comment.