heroku play! with sendgrid configuration - heroku

Tried to config smtp in application.conf
mail.smtp.host=smtp.sendgrid.net
mail.smtp.user=${SENDGRID_USERNAME}
mail.smtp.pass=${SENDGRID_PASSWORD}
And in the controller
MultiPartEmail email = new MultiPartEmail();
//... setting from,to,subject,content...
Mail.send(email); //using Play's util
But exception occurs, saying bad user credential when authenticating the smtp server.
One thing I notice is that, when push to heroku and start the app, it would warn:
WARNING: Cannot replace SENDGRID_USERNAME in configuration (mail.smtp.user=${SENDGRID_USERNAME})
WARNING: Cannot replace ENV_SENDGRID_PASSWORD in configuration (mail.smtp.pass=${SENDGRID_PASSWORD})
This may due to the precompile flag is on when deploy?? Here is my Procfile:
web: play run --http.port=$PORT --%prod

I've created a simple Play + SendGrid + Heroku example that works for me:
https://github.com/jamesward/playsendgrid
I'm not sure what is different between this example and your code. The only weird thing I noticed above is where it says ENV_SENDGRID_PASSWORD. Perhaps that environment variable name is wrong.

Related

Phoenix-framework : Mysql connection error on production

I am new to Phoenix and actually working on my first project in it.
When deploying the project on production server, I am getting a Database Connection error for mySql. For some reason, it is not considering the username/password values provided in config/prod.exs
import Config
config :g_plus, GPlusWeb.Repo,
username: "root",
password: "Somepassword",
database: "db_name",
hostname: "localhost",
load_from_system_env: true,
pool_size: 20
I also tried with environment variable (DATABSE_URL), but it is still not working.
ecto://root:Somepassword#localhost:3306/db_name
Am I missing any setting/config somewhere?
I couldn't find anything in Google search as well.
Most deployment guides are for apps without DB.
I found the issue. I was using GPlusWeb instead of just GPlus in the following line.
config :g_plus, GPlusWeb.Repo,
I changed it to config :g_plus, GPlus.Repo, and it worked.

Can't create bucket using aws-sdk ruby gem. Aws::S3::Errors::SignatureDoesNotMatch

I have a new computer and I'm trying to set up my AWS CLI environment so that I can run a management console I've created.
This is the code I'm running:
def create_bucket(bucket_args)
AWS_S3 = Aws::S3::Client.new(signature_version: 'v4')
AWS_S3.create_bucket(bucket_args)
end
Which raises this error:
Aws::S3::Errors::SignatureDoesNotMatch - The request signature we calculated does not match the signature you provided. Check your key and signing method.:
This was working properly on my other computer, which I no longer have access to. I remember debugging this same error on the other computer, and I thought I had resolved it by adding signature_version = s3v4 to my ~/.aws/config file. But this fix is not working on my new computer, and I'm not sure why.
To give some more context: I am using aws-sdk (2.5.5) and these aws cli specs: aws-cli/1.11.2 Python/2.7.12 Linux/4.4.0-38-generic botocore/1.4.60
In this case the issue was that my aws credentials (in ~/.aws/credentials) - specifically my secret token - were invalid.
The original had a slash in it:
xx/xxxxxxxxxxxxxxxxxxxxxxxxxx
which I didn't notice at first, so when I double clicked the token to select the word, it didn't include the first three characters. I then pasted this into the terminal when running aws configure.
To fix this, I found the correct, original secret acceess token and set the correct value in ~/.aws/credentials.

Environment Variables on Heroku and Mailgun Problems with Phoenix Framework

I was following this guide on deploying to Heroku and this one for sending email.
Everything works fine in development. My variables are set in Heroku:
heroku config
...
MAILGUN_DOMAIN: https://api.mailgun.net/v3/xxxxxx.mailgun.org
MAILGUN_KEY: key-3-xxxxxx
...
And loaded from the config files like so:
config :take_two, Mailer,
domain: System.get_env("MAILGUN_DOMAIN"),
key: System.get_env("MAILGUN_KEY")
However when I try to send email on Heroku when the Mailgun config is set from environment variables I get this error:
** (FunctionClauseError) no function clause matching in IO.chardata_to_string/1
(elixir) lib/io.ex:346: IO.chardata_to_string(nil)
(elixir) lib/path.ex:467: Path.join/2
(elixir) lib/path.ex:449: Path.join/1
lib/client.ex:44: Mailgun.Client.send_without_attachments/2
This happens when the domain is not set for the Mailgun Client. But it is supposed to be set from the environment variable. I made a simple module to test:
defmodule TakeTwo.Mailer do
require Logger
use Mailgun.Client,
Application.get_env(:take_two, Mailer)
def blank_shot do
Logger.info Application.get_env(:take_two, Mailer)[:domain]
Logger.info Application.get_env(:take_two, Mailer)[:key]
send_email from: "steve#xxx.com", to: "speggy#xxx.com", subject: "Hello", text: "This is a blank shot"
end
When I run TakeTwo.Mailer.blank_shot I see the correct domain/key variables logged followed by the error. I am not sure how to debug the Mailgun client remotely.
Finally, if I recreate the above module in the shell (after running heroku run iex -S mix) it works just fine!?
I feel like when the original module is being loaded perhaps the environment variables have yet to be loaded??
The answer was a little buried in a comment so I wanted to make it easier to find. As the other answer mentions, the environment variables aren't available, but the buildpack lets you configure them to be:
I created a elixir_buildpack.config file and added the following:
config_vars_to_export=(DATABASE_URL MAILGUN_DOMAIN MAILGUN_KEY SECRET_KEY_BASE)
The environment variables aren't available at build time. I had the same issue and decided to get rid of the macro carrying the configuration. You can use this patch to move on.

How to set Heroku config var with contents of a file

To set config vars for a Heroku app, you do this:
$ heroku config:set GITHUB_USERNAME=joesmith
How would I set a config var with the contents of a file?
Take a look at the heroku-config plugin, which adds a heroku config:push command to push key-value pairs in a file named .env to the app.
It also has a heroku config:pull command to do the opposite and works very well with foreman for running the app locally with the config in .env.
https://github.com/xavdid/heroku-config
Example
heroku config:push --file=.env.production
I know this is too late but still it will be helpful for future users who land here.
I also wanted a quick solution to add variable to heroku app but copy-paste is so boring.. So wrote a script to read values from the .env file and set it at the requested app - all things passed as an option:
https://gist.github.com/md-farhan-memon/e90e30cc0d67d0a0cd7779d6adfe62d1
Usage
./bulk_add_heroku_config_variables.sh -f='/path/to/your/.environment/file' -s='bsc-server-name' -k='YOUR_CONFIG_KEY1 YOUR_CONFIG_KEY2'
A simple pure-python solution using honcho and invoke:
from honcho.environ import parse
from invoke import run
def push_env(file='.env'):
"""Push .env key/value pairs to heroku"""
with open(file, 'r') as f:
env = parse(f.read())
cmd = 'heroku config:set ' + ' '.join(
f'{key}={value}' for key, value in env.items())
run(cmd)
The idea here is that you will get the same configuration as if you ran the project locally using honcho. Then I use invoke to run this task easily from the command line (using #task and c.run) but I've adapted it here to stand alone.

How to increase ActiveRecord thread pool size on heroku

Normally I would set the pool size as
development:
adapter: postgresql
encoding: unicode
database: openkitchen_development
username: rails
host: localhost
pool: 10
password:
in database.yml. However heroku replaces the config file. I'm using girl_friday to
do background db work and need to increase the thread pool size.
Simply add a pool query parameter to the DATABASE_URL in your heroku config. To set the pool size to 15 in your heroku app use something like:
heroku config -s | awk '/^DATABASE_URL=/{print $0 "?pool=15"}' | xargs heroku config:add
For what it's worth, using the URL params method as described in other answers here is not recommended by Heroku. They reserve the right to reset or change this URL at any time, and long term this behavior will likely be removed for the Rails build behavior, anyway.
Setting additional parameters via an after-initialize application callback is the recommended way to modify the configuration of your heroku-postgresql databases per this dev center article.
In config/initializers/database_connection.rb:
Rails.application.config.after_initialize do
ActiveRecord::Base.connection_pool.disconnect!
ActiveSupport.on_load(:active_record) do
config = Rails.application.config.database_configuration[Rails.env]
config['pool'] = 10
ActiveRecord::Base.establish_connection(config)
end
end
Heroku now has a nice article on managing pool sizes - https://devcenter.heroku.com/articles/concurrency-and-database-connections#connection-pool
remvee's answer gets to the heart of what is needed but since his command caused my console to hang I thought I would write up how to do this manually.
heroku config
Look for the DATABASE_URL key. For this example lets say it is:
DATABASE_URL: mysql2://something.example.com/stuff?reconnect=true
Add "&pool=10" to the end of the URL (use & instead of ? because the url already has a parameter)
heroku config:add DATABASE_URL=mysql2://something.example.com/stuff?reconnect=true&pool=10
It's not very straight forward but you could try creating your own buildpack.
You'll nee to fork:
https://github.com/heroku/heroku-buildpack-ruby
Then modify the following:
https://github.com/heroku/heroku-buildpack-ruby/blob/master/lib/language_pack/ruby.rb#L325-387
Just add the pool size you require.
Then you can create a new Heroku app with your custom buildpack:
heroku create --stack cedar --buildpack https://github.com/yourgithubusername/heroku-buildpack-ruby.git
That should be it!

Resources