I've a ruby on rails app which is on 0.0.0.0:3000 and discourse docker container on port 0.0.0.0:80 . I want to bind localhost RoR app with docker container because postgreSQL is running inside the docker container and want to connect RoR app with postgreSQL which is inside docker container. How can I make db connection between RoR app and docker container postgrePSQL.
Here is my RoR controller.
class CronController < ApplicationController
# slack channel hook
$slackHook = "http://ift.tt/2uBsszh"
$discourseHost = 'community.cloudways.com/';
$messageText = "New Notification";
# import http module
require 'net/http'
=begin
A method which build connection with postgreSQL and fetch last 24hr records
=end
def slackNotification
logger.debug "*******Cron Started********"
begin
$query = "SELECT users.username AS username, topics.title AS topic_title, topics.id AS topic_id, posts.raw AS raw FROM post_replies
JOIN posts ON posts.id = post_replies.post_id
JOIN users ON users.id = posts.user_id
JOIN topics ON topics.user_id = users.id
WHERE post_replies.created_at BETWEEN current_date AND current_date - INTERVAL '1' DAY;"
$res = ActiveRecord::Base.connection.exec_query(query);
# iteration on each record
$res.each do |row|
sendNotifications row
end
logger.debug "*******Cron successfully executed.********"
render :json => {:status => "true", :message => "Cron successfully executed."}
rescue Exception => e
logger.fatal "Exception: #{e}"
end
end
=begin
such method which take payload and send it to slack hook url
@params row
=end
def sendNotifications row
$title = row['topic_title']
$topicId = row['topic_id']
$content = row['raw']
begin
uri = URI.parse($slackHook)
# Full control
http = Net::HTTP.new(uri.host, uri.port)
response = Net::HTTP.post_form(uri, {
'payload' => '{
"fallback": "#{$messageText}",
"text": "#{$title}",
"pretext": "<http://#{$discourseHost}|#{$title}>",
"color": "#36a64f",
"fields": [
{
"title": "#{$title}" ,
"value": "#{$content}",
"short": false
}
]
}'
})
rescue Exception => e
logger.fatal " Attributes: title: #{$title}, topicId: #{$topicId}, content: #{$content}, URL: <http://#{$discourseHost}|#{$title}> "
logger.fatal "Exception: #{e}"
end
end
end
Aucun commentaire:
Enregistrer un commentaire