I am new to django and have been working on a project for a few weeks now. I am making a django application that I host on heroku. I want to schedule a task. The end goal is to make a series of API calls that save the results to a model.
I currently have in my app a file scheduler.py
from django.core.management.base import BaseCommand, CommandError
from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()
from fans.models import TotalPageLikes
@sched.scheduled_job('interval',minutes=1)
def timed_job():
print('This job runs every minute')
@sched.scheduled_job('interval',minutes=1)
def fan_call():
twr = TotalPageLikes()
twr.fans = 50
twr.save()
print('should have saved TWR')
class Command(BaseCommand):
def handle(self, *args, **options):
sched.start()
In my Procfile I call this
web: gunicorn site1.wsgi
scheduler: python manage.py scheduler
And here is the model
class TotalPageLikes(models.Model):
fans = models.BigIntegerField()
Finally here is my admin
from django.contrib import admin
# Register your models here.
from .models import Fan, Input, TotalPageLikes
admin.site.register(Fan)
admin.site.register(Input)
admin.site.register(TotalPageLikes)
Everything deploys and runs successfully, and I can see the results with heroku logs --tail . I see the print statements, but when I open the app in heroku, and navigate to /admin, I see that the model holds nothing. It is not showing an error for the save, but it is not properly saving.
Does anyone know how to save a scheduled job to a model? Thanks.
UPDATE
I have tried using TotalPageLikes.objects.create(fans=-1) in scheduler.py and still, it does not show up when I open the app in heroku and navigate to admin. I can see in the log that I am logging it, and I see no error.
UPDATE
I ran this locally and it did what I expected it to. Something is occuring with heroku. I have this running on separate dynos because that is how I learned to do this. I am going to attempt to sneak the functions in my models.py to see if this changes anything.
FINAL UPDATE
I had to use a PostgreSQL database for this. The necessary steps involved using the command heroku pg:credentials:url DATABASE
and then setting the proper crednetials from here to my settings.py in DATABASES = . Then I had to create a new superuser python manage.py createsuperuser
to access my database to confirm it is working.