I am currently running into a problem where on my local machine, I can create and insert a record into a table called Devices but on my heroku deploy, I cannot create a record if the length of my identifier and push_token are too long.
The device table migration I am using is pasted below.
class CreateDevices < ActiveRecord::Migration[6.1]
def change
create_table :devices do |t|
t.string :identifier, null: false
t.string :push_token, null: false
end
end
end
I tried a simple test with an endpoint to see if records were created with
get "/test/devices/:device_id/:push_tokens" do
Device.create(identifier: params[:device_id], push_token: params[:push_tokens])
puts "device_count is"
puts Device.count
end
On my local machine, if i set params[:device_id] and params[:push_tokens] to for example,
278b5d5668b7e2df3b686e0c0d19a0b9 and 1319f7e0f6dd345304c5e8e84e5ec3f52213b8f485e4783861b476373f000dde respectively the active record updates and the device count updates with it. However if i try these values on the heroku deploy, the device count is not updated and no record is created.
I am struggling to figure out whether this is a problem with my postgresql plan on heroku, if my code is not waiting for the record to be created or something entirely different.
2022-09-23T13:47:45.399683+00:00 heroku[router]: at=info method=GET path="/test/devices/123456789123456789/123456789123456789" host=japsoc-passes-server-2.herokuapp.com request_id=21779202-b9b6-4e02-9195-6ead6cc63ba6 fwd="147.12.186.203" dyno=web.1 connect=0ms service=9ms status=200 bytes=191 protocol=https
2022-09-23T13:47:45.394194+00:00 app[web.1]: device_count is
2022-09-23T13:47:45.395126+00:00 app[web.1]: 8
2022-09-23T13:47:59.284900+00:00 heroku[router]: at=info method=GET path="/test/devices/123456789123456789123456789/123456789123456789123456789" host=japsoc-passes-server-2.herokuapp.com request_id=04ef6dab-71c3-4237-b52f-bf2ea16576f0 fwd="147.12.186.203" dyno=web.1 connect=0ms service=7ms status=200 bytes=191 protocol=https
2022-09-23T13:47:59.279643+00:00 app[web.1]: device_count is
2022-09-23T13:47:59.280339+00:00 app[web.1]: 9
2022-09-23T13:48:17.720446+00:00 heroku[router]: at=info method=GET path="/test/devices/123456789123456789123456789123456789/123456789123456789123456789123456789" host=japsoc-passes-server-2.herokuapp.com request_id=1c221b82-f142-44a0-b0ec-774a8515365a fwd="147.12.186.203" dyno=web.1 connect=0ms service=9ms status=200 bytes=191 protocol=https
2022-09-23T13:48:17.715182+00:00 app[web.1]: device_count is
2022-09-23T13:48:17.715917+00:00 app[web.1]: 10
2022-09-23T13:48:35.566064+00:00 heroku[router]: at=info method=GET path="/test/devices/123456789123456789123456789123456789123456789/123456789123456789123456789123456789123456789" host=japsoc-passes-server-2.herokuapp.com request_id=2539f65b-7f82-42dc-9c4e-c078b93412d1 fwd="147.12.186.203" dyno=web.1 connect=0ms service=6ms status=200 bytes=191 protocol=https
2022-09-23T13:48:35.560941+00:00 app[web.1]: device_count is
2022-09-23T13:48:35.561519+00:00 app[web.1]: 11
2022-09-23T13:49:00.943300+00:00 heroku[router]: at=info method=GET path="/test/devices/123456789123456789123456789123456789123456789123456789/123456789123456789123456789123456789123456789123456789" host=japsoc-passes-server-2.herokuapp.com request_id=01acc954-8cf9-4327-8e84-b1958298b14d fwd="147.12.186.203" dyno=web.1 connect=0ms service=8ms status=200 bytes=191 protocol=https
2022-09-23T13:49:00.937948+00:00 app[web.1]: device_count is
2022-09-23T13:49:00.938685+00:00 app[web.1]: 12
2022-09-23T13:49:52.814962+00:00 heroku[router]: at=info method=GET path="/test/devices/123456789123456789123456789123456789123456789123456789/123456789123456789123456789123456789123456789123456789123456789123456789" host=japsoc-passes-server-2.herokuapp.com request_id=c834c2df-b822-4796-b8ae-ab29cce6bdc2 fwd="147.12.186.203" dyno=web.1 connect=0ms service=5ms status=200 bytes=191 protocol=https
2022-09-23T13:49:52.809811+00:00 app[web.1]: device_count is
2022-09-23T13:49:52.810427+00:00 app[web.1]: 12
I did some testing with the endpoint on the heroku deploy and it seems that at some point, if the string is too long for either the identifier or the push_token, no record is created but i still receive a status 200. I would greatly appreciate any help on this matter!
Turns out after using Device.create! instead of Device.create, the reason why a device was not created was because either the device_id had already been taken or push_token had already been taken and it was a problem with my device models rather than active records.
I have been using the official Strapi tutorial on how to deploy strapi to heroku with postgres and after following all the instructions, my heroku app is showing an error. However when I check the build logs, there are no errors and they show the build successful message.
build logs
2020-08-17T15:48:19.744258+00:00 app[web.1]: npm ERR!
2020-08-17T15:48:19.744486+00:00 app[web.1]: npm ERR! Failed at the radio-darya-backend#0.1.0 start script.
2020-08-17T15:48:19.744753+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-08-17T15:48:19.756754+00:00 app[web.1]:
2020-08-17T15:48:19.757071+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-08-17T15:48:19.757252+00:00 app[web.1]: npm ERR! /app/.npm/_logs/2020-08-17T15_48_19_747Z-debug.log
2020-08-17T15:48:19.825573+00:00 heroku[web.1]: Process exited with status 1
2020-08-17T15:48:19.869487+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-17T15:48:32.221633+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=radio-darya-backend.herokuapp.com request_id=1bceee5d-4452-4b2a-9638-d5f242b4337c fwd="213.162.246.193" dyno= connect= service= status=503 bytes= protocol=https
2020-08-17T15:48:32.751425+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=radio-darya-backend.herokuapp.com request_id=95d4de1a-5f17-49e3-bed2-b459bce9259e fwd="213.162.246.193" dyno= connect= service= status=503 bytes= protocol=https
package.json dependencies
"devDependencies": {},
"dependencies": {
"knex": "<0.20.0",
"pg": "^8.3.0",
"sqlite3": "latest",
"strapi": "3.1.4",
"strapi-admin": "3.1.4",
"strapi-connector-bookshelf": "3.1.4",
"strapi-plugin-content-manager": "3.1.4",
"strapi-plugin-content-type-builder": "3.1.4",
"strapi-plugin-email": "3.1.4",
"strapi-plugin-upload": "3.1.4",
"strapi-plugin-users-permissions": "3.1.4",
"strapi-utils": "3.1.4"
},
config database.js
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
"client":"postgres",
"host":"${process.env.DATABASE_HOST}",
"port": "${process.env.DATABASE_PORT}",
"database": "${process.env.DATABASE_NAME}",
"username": "${process.env.DATABASE_USERNAME}",
"password": "${process.env.DATABASE_PASSWORD}",
"ssl": { "rejectUnauthorized": false }
},
options: {
},
},
},
});
her
This is working for me for v3.x strapi.
// Path: ./config/env/production/database.js
const parse = require('pg-connection-string').parse;
const config = parse(process.env.DATABASE_URL);
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client: 'postgres',
host: config.host,
port: config.port,
database: config.database,
username: config.user,
password: config.password,
},
options: {
ssl: false,
},
},
},
});
We also need to set the NODE_ENV variable on Heroku to production to ensure this new database configuration file is used.
heroku config:set NODE_ENV=production
see https://strapi.io/documentation/v3.x/deployment/heroku.html
Note here is v3.x instead of beta version. Google "strapi heroku postgres" for the moment still give legacy beta version.
You didn’t give the error message you’re having?
I did deploy my strapi to heroku a few weeks ago and there was no problems.
You are sure you followed all the steps from strapi documentation?
Only thing I could think to go wrong is database connection.
First you have to install postgress addon in Heroku, then get the config-info and last add environment variables in Heroku (settings/config vars) and also modify strapi config-files to get the database information from environment variables.
Strapi documentation: https://strapi.io/documentation/3.0.0-beta.x/deployment/heroku.html.
EDIT:
Strapi documentation isn’t correct at the moment, database.json file and location has been changed. See:
strapi database.js / multiple database configs
https://www.youtube.com/watch?v=xNE0TrI5OKk
ive just completed that tutorial a day ago... and i also had problems for a complete beginner to strapi and postgres and heroku... heres my experience.
follow along here to get postgres and the database setup:
https://tute.io/install-configure-strapi-postgresql
then complete your setup with the missing pieces from here:
https://strapi.io/documentation/v3.x/deployment/heroku.html
basically:
have postgres installed locally on your system, create the db and user and grant permissions.
install strapi without the quickstart flag and use the details that was used above.
use the heroku cli to set the configs derived from the database_url configvar.
commit and push and all should be well.
EDIT:
In Heroku under the app in question:
click setting and scroll down to Environment variable and click Reveal Config Vars
Ensure that you have a DATABASE_URL config var
Ensure that your postgres specific config vars match up to the DATABASE_URL
eg. DATABASE_URL = postgres://ebitxebvixeeqd:dc59b16dedb3a1eef84d4999sb4baf#ec2-50-37-231-192.compute-2.amazonaws.com: 5432/d516fp1u21ph7b
It's read like so: postgres:// USERNAME : PASSWORD # HOST : PORT : DATABASE_NAME
Also in your ./config/server.js file make sure your host is 0.0.0.0
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
admin: {
auth: {
secret: env('ADMIN_JWT_SECRET', '***********************'),
},
},
});
Also change your database.js config to be:
settings: {
client: 'postgres',
host: env('DATABASE_HOST', '127.0.0.1'),
port: env.int('DATABASE_PORT', 5432),
database: env('DATABASE_NAME', 'strapi'),
username: env('DATABASE_USERNAME', 'postgres'),
password: env('DATABASE_PASSWORD', ''),
ssl: env.bool('DATABASE_SSL', false),
},
options: {}
Theres no much to go on from your question or the logs, but the above is basically some common issues ive experienced.
copy this code directly in the config/database.js
module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client:'postgres',
host:`${process.env.DATABASE_HOST}`,
port: `${process.env.DATABASE_PORT}`,
database: `${process.env.DATABASE_NAME}`,
username: `${process.env.DATABASE_USERNAME}`,
password: `${process.env.DATABASE_PASSWORD}`,
ssl: { "rejectUnauthorized": false }
},
options: {
},
},
},
});
Can someone explain me the different lines in this piece of Heroku's log while my Facebook Messenger bot is running (and people interacting)
2018-08-19T12:21:53.976999+00:00 app[web.1]: 10.124.6.113 - - [19/Aug/2018:12:21:53 +0000] "POST /webhook HTTP/1.1" 200 2 "-" "facebookexternalua"
2018-08-19T12:21:54.060862+00:00 app[web.1]: 10.8.0.31 - - [19/Aug/2018:12:21:54 +0000] "POST /webhook HTTP/1.1" 200 2 "-" "facebookexternalua"
2018-08-19T12:21:53.977357+00:00 heroku[router]: at=info method=POST path="/webhook" host=some-app.herokuapp.com request_id=9691c0bc-43c7-4a5e-83c1-c636a4cc2789 fwd="31.13.115.9" dyno=web.1 connect=0ms service=462ms status=200 bytes=161 protocol=https
2018-08-19T12:21:54.062745+00:00 heroku[router]: at=info method=POST path="/webhook" host=some-app.herokuapp.com request_id=624ca87a-b237-4a16-9e74-ad086c776d2a fwd="31.13.115.11" dyno=web.1 connect=0ms service=514ms status=200 bytes=161 protocol=https
2018-08-19T12:21:55.013052+00:00 app[web.1]: 10.127.18.55 - - [19/Aug/2018:12:21:55 +0000] "POST /webhook HTTP/1.1" 200 2 "-" "facebookexternalua"
2018-08-19T12:21:55.012197+00:00 heroku[router]: at=info method=POST path="/webhook" host=some-app.herokuapp.com request_id=032fef2b-5029-4a7b-903c-45b685b0a9ef fwd="31.13.115.2" dyno=web.1 connect=0ms service=464ms status=200 bytes=161 protocol=https
2018-08-19T12:21:56.138658+00:00 app[web.1]: 10.10.128.18 - - [19/Aug/2018:12:21:56 +0000] "POST /webhook HTTP/1.1" 200 2 "-" "facebookexternalua"
2018-08-19T12:21:56.137932+00:00 heroku[router]: at=info method=POST path="/webhook" host=some-app.herokuapp.com request_id=7f32d132-5587-4cfb-9dd4-b44ddeb8b684 fwd="31.13.115.13" dyno=web.1 connect=0ms service=734ms status=200 bytes=161 protocol=https
In fact, I would like to know what are the times of the type "service = 734ms"
Thanks in advance
service might be the time in milliseconds, in between receiving the request and serving the response; measured at the router and not the web dyno, where the request is being forwarded to.
Heroku's documentation for their router log format is here. Here's what they say about "service":
amount of time in milliseconds spent proxying data between the backend web process and the client
In other words, it's the amount of time it's taking your app (the backend web process) to process the request.
I am testing Facebook Messenger bot. Part of the code with core functionality:
const schedule = require('node-schedule');
app.post('/webhook/', function(req, res) {
let messaging_events = req.body.entry[0].messaging
var j = schedule.scheduleJob('10 * * * * *', function(){
for (let i = 0; i < messaging_events.length; i++) {
let event = messaging_events[i]
let sender = event.sender.id
if (event.message && event.message.text) {
let text = event.message.text
sendText(sender, "Text echo: " + text.substring(0, 100))
}
}
res.sendStatus(200)
})
})
It should reply the same message that is sent to the bot every minute (when the clock crosses 10 seconds - 2:46:10 PM, 2:47:10 PM, etc.)
If I try similar code locally, it works. It does not on Heroku. It behaves irregularly - sometimes it replies the message 2 to 4 times at once and then crashes, sometimes it does not reply anything at all. Here is the Heroku log:
2018-03-19T13:48:43.574712+00:00 heroku[web.1]: Unidling
2018-03-19T13:48:43.575153+00:00 heroku[web.1]: State changed from down to starting
2018-03-19T13:48:45.854373+00:00 heroku[web.1]: Starting process with command `node index.js`
2018-03-19T13:48:48.046370+00:00 app[web.1]: running: port
2018-03-19T13:48:48.550571+00:00 heroku[web.1]: State changed from starting to up
2018-03-19T13:49:10.035608+00:00 heroku[router]: at=info method=POST path="/webhook/" host=dry-badlands-96368.herokuapp.com request_id=89a76e53-663d-4086-8457-64c0f26be8c7 fwd="31.13.113.20" dyno=web.1 connect=1ms service=11963ms status=200 bytes=201 protocol=https
2018-03-19T13:49:10.046345+00:00 heroku[router]: at=info method=POST path="/webhook/" host=dry-badlands-96368.herokuapp.com request_id=0da8c7a9-4d5f-45bc-8797-54f608968581 fwd="31.13.105.114" dyno=web.1 connect=0ms service=20401ms status=200 bytes=201 protocol=https
2018-03-19T13:49:41.297748+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/webhook/" host=dry-badlands-96368.herokuapp.com request_id=cd1568a1-68c3-455f-872e-260f8023d925 fwd="31.13.113.170" dyno=web.1 connect=1ms service=30002ms status=503 bytes=0 protocol=https
2018-03-19T13:49:41.747709+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/webhook/" host=dry-badlands-96368.herokuapp.com request_id=32947747-1c2b-475c-a2b8-46477e13c4a2 fwd="31.13.114.116" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=https
2018-03-19T13:50:10.059682+00:00 app[web.1]: _http_outgoing.js:491
2018-03-19T13:50:10.059738+00:00 app[web.1]: throw new Error('Can\'t set headers after they are sent.');
2018-03-19T13:50:10.059740+00:00 app[web.1]: ^
2018-03-19T13:50:10.059742+00:00 app[web.1]:
2018-03-19T13:50:10.059744+00:00 app[web.1]: Error: Can't set headers after they are sent.
2018-03-19T13:50:10.059745+00:00 app[web.1]: at validateHeader (_http_outgoing.js:491:11)
2018-03-19T13:50:10.059747+00:00 app[web.1]: at ServerResponse.setHeader (_http_outgoing.js:498:3)
2018-03-19T13:50:10.059748+00:00 app[web.1]: at ServerResponse.header (/app/node_modules/express/lib/response.js:767:10)
2018-03-19T13:50:10.059750+00:00 app[web.1]: at ServerResponse.contentType (/app/node_modules/express/lib/response.js:595:15)
2018-03-19T13:50:10.059752+00:00 app[web.1]: at ServerResponse.sendStatus (/app/node_modules/express/lib/response.js:357:8)
2018-03-19T13:50:10.059753+00:00 app[web.1]: at Job.job (/app/index.js:46:9)
2018-03-19T13:50:10.059755+00:00 app[web.1]: at Job.invoke (/app/node_modules/node-schedule/lib/schedule.js:173:10)
2018-03-19T13:50:10.059756+00:00 app[web.1]: at /app/node_modules/node-schedule/lib/schedule.js:543:11
2018-03-19T13:50:10.059758+00:00 app[web.1]: at Timeout._onTimeout (/app/node_modules/node-schedule/lib/schedule.js:501:7)
2018-03-19T13:50:10.059759+00:00 app[web.1]: at ontimeout (timers.js:482:11)
2018-03-19T13:50:10.130605+00:00 heroku[web.1]: Process exited with status 1
2018-03-19T13:50:10.241686+00:00 heroku[web.1]: State changed from up to crashed
Does anyone have any idea why is this happening? Maybe Heroku is not suited for this?
Thank you for all suggestions.
I migrated my app to heroku + mlab from parse. I would too often get request timeout error on the logs. I turned on verbose and below is what i get when a timeout happens:
2016-05-25T15:27:40.946050+00:00 app[web.1]: [36mverbose[39m: POST /parse/events/AppOpened { host: 'dairy-oneandahalf.herokuapp.com',
2016-05-25T15:27:40.946067+00:00 app[web.1]: connection: 'close',
2016-05-25T15:27:40.946074+00:00 app[web.1]: 'x-parse-app-build-version': '1',
2016-05-25T15:27:40.946084+00:00 app[web.1]: "__type": "Date"
2016-05-25T15:27:41.517425+00:00 app[web.1]: [36mverbose[39m: GET /parse/classes/_User { host: 'dairy-oneandahalf.herokuapp.com',
2016-05-25T15:27:41.517436+00:00 app[web.1]: connection: 'close',
2016-05-25T15:27:41.517445+00:00 app[web.1]: 'x-parse-client-version': 'i1.13.0',
2016-05-25T15:27:41.517448+00:00 app[web.1]: 'x-forwarded-for': '58.152.243.212',
2016-05-25T15:27:40.948269+00:00 heroku[router]: at=info method=POST path="/parse/events/AppOpened" host=dairy-oneandahalf.herokuapp.com request_id=3e9b1390-a1b8-487b-8229-f4c44777b2a4 fwd="58.152.243.212" dyno=web.1 connect=2ms service=6ms status=200 bytes=483
2016-05-25T15:28:12.746545+00:00 app[web.1]: [36mverbose[39m: GET /parse/classes/allPosts { host: 'dairy-oneandahalf.herokuapp.com',
2016-05-25T15:28:12.746591+00:00 app[web.1]: connection: 'close',
2016-05-25T15:28:12.746600+00:00 app[web.1]: 'x-request-id': '84c7c271-4725-42c2-a111-3cf8e2c16d52',
2016-05-25T15:28:12.746602+00:00 app[web.1]: 'total-route-time': '0',
2016-05-25T15:28:12.746605+00:00 app[web.1]: "query": {
2016-05-25T15:28:11.513492+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/parse/classes/_User" host=dairy-oneandahalf.herokuapp.com request_id=1260ec0f-3cee-4c43-a87e-9146ef154e29 fwd="58.152.243.212" dyno=web.1 connect=1ms service=30001ms status=503 bytes=0
I'm very new to heroku, can somebody explain to me what is wrong and what I can do about solving it? any help will be greatly appreciated, thanks in advance