Skip to content

Instantly share code, notes, and snippets.

@mazz
Created January 8, 2017 05:41
Show Gist options
  • Save mazz/38864c91438fa7111c8eecd9d7eb929b to your computer and use it in GitHub Desktop.
Save mazz/38864c91438fa7111c8eecd9d7eb929b to your computer and use it in GitHub Desktop.
my_task.apply_async() not firing?
from pyramid.httpexceptions import HTTPNotFound, HTTPFound
from websauna.utils.slug import uuid_to_slug
from websauna.utils.slug import slug_to_uuid
from websauna.system.core import messages
from websauna.system.http import Request
from websauna.system.core.route import simple_route
from email.header import Header
from email.utils import formataddr
from websauna.system.mail import send_templated_mail
from .models import Media
from .tasks import my_task
import pafy
# Configure view named home at path / using a template hearit/home.html
@simple_route("/", route_name="home", renderer="hearit/home.html")
def home(request: Request):
"""Render the site homepage."""
latest_media_list = request.dbsession.query(Media).order_by(Media.published_at.desc()).all()[:10]
return locals()
@simple_route("/media/{media_uuid}", route_name="detail", renderer="hearit/detail.html")
def detail(request):
# Convert base64 encoded UUID string from request path to Python UUID object
media_uuid = slug_to_uuid(request.matchdict["media_uuid"])
media = request.dbsession.query(Media).filter_by(uuid=media_uuid).one_or_none()
if not media:
raise HTTPNotFound()
if request.method == "POST":
media = request.dbsession.query(Media).filter_by(uuid=media_uuid).first()
video = pafy.new(media.url)
media.title = video.title
media.duration = video.duration
media.description = video.description
bestaudio = video.getbestaudio()
print("bestaudio: {0}".format(bestaudio))
my_task.apply_async(args=(9,), tm=request.transaction_manager)
if request.user:
# my_task.apply_async(tm=request.transaction_manager)
# send_review_sms_notification.apply_async(args=(delivery.id,), tm=request.transaction_manager)
send_templated_mail(request, [request.user.email], "email/audio_downloaded", context={}, sender="[email protected]", immediate=True)
request.dbsession.flush()
messages.add(request, msg="got the post", kind="success")
return HTTPFound(request.route_url("fetch_media_result", media_uuid=uuid_to_slug(media.uuid)))
if not media:
raise HTTPNotFound()
return locals()
@simple_route("/media/{media_uuid}/fetch_media_result", route_name="fetch_media_result", renderer="hearit/fetch_media_result.html")
def fetch_media_result(request: Request):
# Convert base64 encoded UUID string from request path to Python UUID object
media_uuid = slug_to_uuid(request.matchdict["media_uuid"])
media = request.dbsession.query(Media).filter_by(uuid=media_uuid).first()
if not media:
raise HTTPNotFound()
return locals()
@mazz
Copy link
Author

mazz commented Jan 8, 2017

hearit app init.py

"""App entry point and configuration."""

import websauna.system


class Initializer(websauna.system.Initializer):
    """An initialization configuration used for starting hearit.

    Override parent class methods to customize application behavior.
    """

    def configure_static(self):
        """Configure static asset serving and cache busting."""
        super(Initializer, self).configure_static()

        self.config.registry.static_asset_policy.add_static_view('hearit-static', 'hearit:static')

    def configure_templates(self):
        """Include our package templates folder in Jinja 2 configuration."""
        super(Initializer, self).configure_templates()

        self.config.add_jinja2_search_path('hearit:templates', name='.html', prepend=True)  # HTML templates for pages
        self.config.add_jinja2_search_path('hearit:templates', name='.txt', prepend=True)  # Plain text email templates (if any)
        self.config.add_jinja2_search_path('hearit:templates', name='.xml', prepend=True)  # Sitemap and misc XML files (if any)

    def configure_views(self):
        """Configure views for your application.

        Let the config scanner to pick ``@simple_route`` definitions from scanned modules. Alternative you can call ``config.add_route()`` and ``config.add_view()`` here.
        """
        # We override this method, so that we route home to our home screen, not Websauna default one
        from . import views
        self.config.scan(views)

    def configure_models(self):
        """Register the models of this application."""
        from . import models
        print('configure_models')
        self.config.scan(models)

    def configure_model_admins(self):
        """Register the models of this application."""

        # Call parent which registers user and group admins
        super(Initializer, self).configure_model_admins()

        # Scan our admins
        from . import admins
        self.config.scan(admins)

    def configure_tasks(self):
        print('configure_tasks')
        self.config.scan("hearit.tasks")
        
    def run(self):
        super(Initializer, self).run()


def main(global_config, **settings):
    init = Initializer(global_config)
    init.run()
    return init.make_wsgi_app()

@mazz
Copy link
Author

mazz commented Jan 8, 2017

conf/development.ini

# pserve and command line configuration for a local development machine

[includes]
include_ini_files =
    resource://websauna/conf/development.ini
    resource://hearit/conf/base.ini
    resource://websauna/conf/base.ini

[app:main]
websauna.site_id = hearit_dev
websauna.site_email_prefix = [hearit DEV]
#sqlalchemy.url = postgresql://localhost/hearit_dev
sqlalchemy.url = postgresql://hearit:REDACT_PASSWORD@localhost:5432/hearit_dev

websauna.secrets_file = resource://hearit/conf/development-secrets.ini

websauna.mailer = mail
mail.host = localhost
mail.port = 25
mail.username = 
mail.password = 

websauna.celery_config =
    {
        "broker_url": "redis://localhost:6379/3",
        "accept_content": ['json'],
        "task_always_eager": False
    }

@mazz
Copy link
Author

mazz commented Jan 8, 2017

log

-------------------- Restarting --------------------
configure_tasks
tasks scan!
configure_models
[00:36:58] [websauna.system.core.redis create_redis] Creating a new Redis connection pool. Process 21797, thread MainThread, max_connections 16
Starting server in PID 21797.
Serving on http://0.0.0.0:6543
[00:37:13] [websauna.system.core.redis log_redis_statistics] Redis connection statistics - process: 21797, thread: waitress, created: 1, max: 16, in-use: 1, available: 0
[00:37:15] [websauna.system.core.redis log_redis_statistics] Redis connection statistics - process: 21797, thread: waitress, created: 1, max: 16, in-use: 1, available: 0
[00:37:17] [websauna.system.core.redis log_redis_statistics] Redis connection statistics - process: 21797, thread: waitress, created: 1, max: 16, in-use: 1, available: 0
[00:37:20] [root _get_video_gdata] Fetched video gdata
bestaudio: audio:m4a@128k
[00:37:20] [websauna.system.mail send_templated_mail] Sending out email to:['[email protected]'] subject:Audio Downloaded Subject
[00:37:20] [websauna.system.core.redis log_redis_statistics] Redis connection statistics - process: 21797, thread: waitress, created: 1, max: 16, in-use: 1, available: 0

@mazz
Copy link
Author

mazz commented Jan 8, 2017

screen_shot_2017-01-08_at_00_52_52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment