Sending email as a separate service

97 views
Skip to first unread message

Vincent Catalano

unread,
Mar 16, 2013, 4:23:36 AM3/16/13
to pylons-...@googlegroups.com
I'm attempting to setup pyramid_mailer with a website I'm developing. The current documentation (http://docs.pylonsproject.org/projects/pyramid_mailer/en/latest/) recommends creating a single instance of the Mailer and adding it to the configuration setup. However, I want to configure numerous jobs that will run separately from the Pyramid web application using something like the Python APScheduler that will also send emails out to users. Instead of having two separate Pyramid Mailer instances in the app, I would like to abstract the email system into something more like a service . My primary goal is to use the same Mailer configuration for my scheduled services as my Pyramid application since this more clearly follows the principal of Don't Repeat Yourself. Any ideas on how I could accomplish this?

Thanks for your help!

--
Vincent Catalano

Eric Rasmussen

unread,
Mar 17, 2013, 1:44:58 AM3/17/13
to pylons-...@googlegroups.com
I may be misunderstanding the question because I am not too familiar with APScheduler, but it sounds like you want to use mailer.send_to_queue(message) instead of mailer.send(message).

As long as you're sending it to the queue for later processing there shouldn't be any need to have multiple Mailer instances. When you install pyramid_mailer you'll also get a bin/qp script to process mail in the queue, and you can control how and when you run that on a schedule.

-Eric

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Vincent Catalano

unread,
Mar 18, 2013, 5:02:44 PM3/18/13
to pylons-...@googlegroups.com
Thanks for the reply, Eric. I think I figured out a solution, albeit my question was a bit confusing. I have an e-commerce website that integrates with a payment gateway in order to manage paid subscriptions for users. Sometimes these subscription payments fail - either from expired cards or insufficient funds. I wanted to create a schedule task that runs nightly that will check the subscription statuses of each user on the payment gateway, if a user's subscription is no longer valid due to a payment failure I want to notify that user with an email.

The main problem I ran into is that I wanted to configure the pyramid_mailer only once in the applications main() function and use that instance for both the scheduled subscription status job as well as throughout the application (ie. sending a forgotten email password).

In the end, I just pass the pyramid_mailer instance that I created in the applications main() function to a Notifier class that handles the scheduled job. I'm pretty sure I don't have to worry about any race conditions or issues with thread locals since the pyramid_mailer instance is thread safe.

...a bit confusing, but thanks for your help!

-Vincent
--
Vincent Catalano
Software Engineer and Web Ninja,
(520).603.8944

Jonathan Vanasco

unread,
Mar 19, 2013, 1:37:51 PM3/19/13
to pylons-...@googlegroups.com
reading your question... are you using a commandline interface/script for the nightly task ?


after initiating the bootstrap, it would be just a few lines of logic to handle your work -- and it all would happen in a separate process.

Vincent Catalano

unread,
Mar 19, 2013, 2:31:09 PM3/19/13
to pylons-...@googlegroups.com
I am not using a commandline interface. I ended up creating a new APScheduler instance from within the applications main() function. That way, the scheduled service will run only when the server is running. It is in the applications main() function that I create a pyramid_mailer instance and pass it to the APScheduler instance as well as save it to the applications registry so that I have a single shared mailer.

Would you advise against creating a scheduler instance at the main() method level?

--Vincent


--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jonathan Vanasco

unread,
Mar 19, 2013, 5:34:57 PM3/19/13
to pylons-...@googlegroups.com
On Tuesday, March 19, 2013 2:31:09 PM UTC-4, Vincent Catalano wrote:
I am not using a commandline interface. I ended up creating a new APScheduler instance from within the applications main() function. That way, the scheduled service will run only when the server is running. It is in the applications main() function that I create a pyramid_mailer instance and pass it to the APScheduler instance as well as save it to the applications registry so that I have a single shared mailer.

Would you advise against creating a scheduler instance at the main() method level?

--Vincent

I don't know much about APScheduler to advise against it, but the commandline interface is ridiculously simple and powerful.  It took me about 20 minutes to set up a "maintenance" framework via pyramid.paster.bootstrap , and it takes no time for me to create new maintenance tasks.  Some l only run once or twice, via the commandline.  Others I fire off via a cronjob.  Ostensibly one could be a subprocess a scheduled app pops open.

I generally dislike running maintenance routines from the webserver (as in process) , so I can better control how memory and CPU are used within a machine or cluster.  ( akin to running the heavy admin tools separate from more lightweight user views ) that's just my personal preference though.
Reply all
Reply to author
Forward
0 new messages