I run a python script on EC2 that sends the release date of the manga by email on a weekly basis. However, the free period of aws has passed and it costs a lot of money.
So I tried to move it to heroku, which seems to be free. I will summarize what I have tried.
###heroku environment preparation(centos premise)
##Install heroku toolbelt
$ sudo wget -qO- https://toolbelt.heroku.com/install.sh | sh
$ sudo ln -s /usr/local/heroku/bin/heroku /usr/local/bin/heroku
##heroku command execution confirmation
$ heroku version
heroku-toolbelt/3.32.0 (x86_64-linux) ruby/2.0.0
You have no installed plugins.
##Authentication
$ heroku login
Enter your Heroku credentials.
Email: [email protected]
Password (typing will be hidden):
Authentication successful.
###APP preparation
$ cd myAPP
#Library installation with pip
#Write code as vi
#To test
$ git init
$ git add .
$ git commit -m "my first commit"
###Deploy to heroku
##APP registration
$ heroku create
#I don't think heroku create my APP is straightforward here
#Otherwise the APP name will be properly named on heroku
##Specify the required modules
$ pip freeze > requirements.txt
$ git add requirements.txt
$ git commit requirements.txt
##Deploy
$ git push heroku master
##View log
$ heroku logs -t
Around here, I went through the getting start listed on heroku and found out. I hate centos, when I follow the official installation of toolbelt, only debian is written, so I can install it from standalone.
reference: Getting Started with Python on Heroku | Heroku Dev Center [vagrant 1.5] How to use Heroku from CentOS 6.5 on Vagrant. --Qiita
#Set each
$ heroku config:set SENDGRID_USER_ID="hogehoge"
$ heroku config:set SENDGRID_PASS="fugafuga"
$ heroku config:set TO_ADDRESS="[email protected]"
$ heroku config:set FROM_ADDRESS="[email protected]"
#Verification
$ heroku config
=== hogehoge Config Vars
FROM_ADDRESS: [email protected]
SENDGRID_PASS: fugafuga
SENDGRID_USER_ID: hogehoge
TO_ADDRESS: [email protected]
For example
sample.py
import os
print os.environ["SENDGRID_USER_ID"]
Then you can set environment variables for each APP. This can also be set from the heroku web screen. (In that case, you can refer to it immediately from local)
Heroku is deployed via git, but if you deploy via git.heroku.com? Or github connection, the code will be published, so I want to hide the email address and path.
So, if you set the environment variable associated with the application from cli or web, it will be hidden, so I tried it.
I felt that there was a more appropriate method, but once heroku toolbelt is included, the local test seems to be no problem, so this is it.
Put sendgrid with pip and almost follow the tutorial.
However, it is as follows with some changes to use the above environment variables.
sg_test.py
import sendgrid
import os
sg = sendgrid.SendGridClient(os.environ["SENDGRID_USER_ID"], os.environ["SENDGRID_PASS"])
message = sendgrid.Mail()
message.add_to(os.environ["TO_ADDRESS"])
message.set_from(os.environ["FROM_ADDRESS"])
message.set_subject("Sending with SendGrid is Fun")
message.set_html("and easy to do anywhere, even with Python")
sg.send(message)
If you look at this tutorial that looks like heroku official, there are api_key and api_user. It's like having to issue an api token. However, it was confusing because the account id and pass used when logging in to sendgrid were all right.
Also, it took a few days to create an account with sendgrid, so I had to prepare in advance to get it done.
reference: sendgrid/sendgrid-python Python - SendGrid Documentation | SendGrid
After deploying and checking the operation on heroku,
$ heroku run python sg_test.py
Add add-on for heroku scheduler.
$ heroku addons:add scheduler:standard
Then you can check it from the APP screen of heroku, so
Enter cron-like settings from the add-on management screen
After that, you can check the operation by looking at log etc.
By the way, I was doing various things while installing the heroku scheduler, and the process scheduler was also registered. It seems that you can change the resource set of Heroku on an hourly basis in a week for resource management. Since there is a calendar GUI, I wish I could do this setting collectively with heroku scheduler ...
reference: Heroku Scheduler | Add-ons | Heroku Heroku Scheduler | Heroku Dev Center How to use heroku scheduler (Rails, sinatra) --Qiita
I was able to implement the subject matter ... but I couldn't do what I wanted to do because I wanted to send emails in batches on a weekly basis ... (I noticed it on the way, but I wanted to play with it or touch heroku lightly)
Heroku scheduer could only be set every 10 minutes, every hour, every day ... I wonder if it can be solved by adding such logic on the APP side. Also, in some cases, there is paas, and if you do not access it for 1 hour, the process will sleep, and you will need to poke with curl regularly, so it will be barren ...
After all, I felt it was easier to build one VM for 500 yen a month per digital ocean than to stick to Murikuri free. After all, I found that local / remote testing and run-time problems were difficult with paas.
Is there any paas for batch or specialized for cron execution? Maybe there is, but I don't think I can find it.
By the way, there seems to be a paas? That makes a docker container and puts it on the web Dokku alternative, so I felt that my wish could be fulfilled there. .. But is it already paas? And this is a story about making paas on your own. .. Reference: Docker-based PaaS that is easy to use like Heroku | KRAY Inc
that's all.
Recommended Posts