Gem stalker on production - ruby-on-rails

I'm using Stalker to interact with beanstalkd on a production server. My jobs file looks like this:
gem 'stalker'
# config/jobs.rb with Rails
RAILS_ENV = ENV["RAILS_ENV"] || "development"
require File.expand_path("../environment", __FILE__)
job "mapeamento_paciente.importar" do |args|
mapeamento = Mapeamento::Paciente.find(args['id'])
importador = ImportadorPaciente.new(mapeamento)
importador.importar!
end
I'm trying to start stalker by issuing the command:
bundle exec stalk config/jobs.rb RAILS_ENV=production
But on loading the environment file, Rails tries to access the development database, instead of production, issuing an error. Does anybody knows how to solve this issue!?
Thanks!

RAILS_ENV=production bundle exec stalk config/jobs.rb

Related

Clockwork gem is trying to access development db in production

I am trying to start the clockwork with following command sudo bundle exec clockwork config/clock.rb in production. But it throws the following error
ActiveRecord::AdapterNotSpecified: 'development' database is not configured. Available: ["production"].
It works correctly in local. We have Puma server and JRuby setup in server.
Add the RAILS_ENV variable:
RAILS_ENV=production sudo bundle exec clockwork config/clock.rb
Or, set the RAILS_ENV variable in your .bashrc:
echo 'export RAILS_ENV=production' >> ~/.bashrc
And then exit the shell, and login again. From then on, clockwork (and all other rails related things) should be in production mode. You can use the command you were using originally.

Run ruby on rails on upstart

I'm folowing this link to put ruby on rails start automatically.
But I cant understand its ruby code
#!/usr/bin/env ruby
require File.expand_path("../../config/environment", __FILE__) #1.what is this path?
STDOUT.sync = true
Stalker::job 'user.fetch_details' do |args| #2.what is this user?
begin
user = User.find(args['id']) #3.what is this id?
user.fetch_user_details!
rescue ActiveRecord::RecordNotFound # too fast
Rails.logger.warn "Unable to find user ##{args['id']} - suspect too fast, requeuing"
Stalker.enqueue('user.fetch_details', :id => args['id'])
end
end
jobs = ARGV.shift.split(',') rescue nil
Stalker.work jobs
and conf file code in /etc/init/
description "TweetedLinks Ruby Worker"
# automatically start
start on filesystem
# working directory
chdir /var/www/TweetedLinks/current #4.should I change this to my own dir?ie home/usr/Trail/test-app
# command to run, with Bundler support!
env RAILS_ENV=production
exec bundle exec ruby script/worker.rb >> log/worker.log #5.do i need to create this worker.log? and this script folder can be put anywhere?
respawn
I have my rails app under home/usr/Trail/test-app
I have create a script folder under home/usr/script
I have my rvm generated under home/usr/.rvm/wrapper/test-app
How to modify this code according to my situation.
and the 5 question in code comments I also very confuse.
Thanks in advance!have been stuck for month!
The easiest way would be to use foreman gem that can export Procfile to upstart, see http://ddollar.github.io/foreman/#UPSTART-EXPORT

Rails always using production environment

I'm having an issue where no matter what environment I try to run Rails in it always goes to production. For example:
$ rails c development
Loading production environment (Rails 3.2.16)
1.9.3p484 :001 >
$ RAILS_ENV=development rails console
Loading production environment (Rails 3.2.16)
1.9.3p484 :001 >
I first noticed this when I was running the Rails server and it was writing to the production database instead of development. If I run "rails s -e development" it says it starts up in development but still uses the production database.
Here's my config/environment.rb
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Skeletor::Application.initialize!
I tried grepping through the project to see if RAILS_ENV was being set anywhere but I don't see it.
Try run:
RAILS_ENV=development bundle exec rails s
In an initializer I had done "if Rails.env = 'production'" instead of "if Rails.env == 'production'" which was causing the problem. Thanks for the suggestions everyone, I knew it had to be something silly.

Automatically Starting ElasticSearch with Rails

I've been doing Ruby on Rails development with ElasticSearch between two machines and its starting to get a little annoying. My usual workflow is:
git pull
bundle install
rake db:migrate (or rake db:setup depending)
rails server
elasticsearch -f -D myconfig.xml
rake environment tire:import CLASS=MyObject FORCE=true
Is there anyway I can add all of these commands to some type of start up script in Rails to bring them all into one place? It would make bringing up a dev environment a lot easier on me everytime I switch machines.
The best way I've found is to use the Foreman gem to kickstart your project and associated processes.
It looks like you should do this in your deployment using Capistrano. Here is an example config/deploy.rb file:
[basic parts omitted]
after "deploy", "bundler:bundle_install"
after "bundler:bundle_install", "db:db_migrate"
after "deploy:db_migrate", "deploy:elastic_search_indexing"
namespace :bundler do
desc 'call bundle install'
task :bundle_install do
run "cd #{deploy_to}/current && bundle install"
end
end
namespace :db do
desc 'fire the db migrations'
task :db_migrate do
run "cd #{deploy_to}/current && bundle exec rake db:migrate RAILS_ENV=\"production\""
end
end
namespace :elasticsearch do
desc 'run elasticsearch indexing via tire'
task :index_classes do
run "cd #{deploy_to}/current && bundle exec rake environment tire:import CLASS=YourObject FORCE=true "
end
end
[rest omitted]
Make sure you have a config file on the target machine (Linux) in /etc/elasticsearch/elasticsearch.yml with contents like:
cluster:
name: elasticsearch_server
network:
host: 66.98.23.12
And the last point to mention is that you should create an initializer config/initializers/tire.rb:
if Rails.env == 'production'
Tire.configure do
url "http://66.98.23.12:9200"
end
end
As you can see, this is the exact same IP address, but only used for the production environment. I assume that you access elasticsearch locally (in development mode) via localhost. elasticsearch is connection per default to
http://0.0.0.0:9200
A good starting point and also in depth help is provided by awesome Ryan Bates and his Railscasts http://railscasts.com/episodes?utf8=%E2%9C%93&search=capistrano
Whats keeping you from putting it in a bash script? And put the script inside your RAILS_APP_HOME/scripts folder?
#!/bin/sh
git pull
bundle install
rake db:migrate
rails server
elasticsearch -f -D myconfig.xml
rake environment tire:import CLASS=MyObject FORCE=true

Deploying Rails Engine on Heroku

Does anyone know how to deploy a rails engine on heroku? The engine that I am developing is self contained so I am trying to avoid making another wrapper rails application to deploy my engine.
FIXED
I had to add config.ru and Procfile in my engine root directory to so that heroku knows that its a rails application.
Thanks,
Ajay
I guess is more dependant on how you've built your engine. If you can run it standalone locally, it'll run just fine like that on Heroku - there's nothing about Heroku which would make running your code any harder other than the normal constraints
As the edit of the question states, config.ru and Procfile are needed. I just copied config.ru from a Rails app and changed the path:
# This file is used by Rack-based servers to start the application.
require ::File.expand_path('../test/dummy/config/environment', __FILE__)
run Rails.application
As for Procfile, the following will work:
web: bundle exec rackup -p $PORT
You may want to add a gem for web server, as it will default to WEBrick which is not suitable for production.
In order to get proper logs on Heroku, add the following to the Gemfile
gem 'rails_12factor', group: :production
Assets compilation won’t work unless you define the assets:precompile task (according to https://devcenter.heroku.com/articles/ruby-support#rails-4-x-applications-compile-phase).
I just added the following to engine’s Rakefile:
namespace :assets do
desc 'Precompile assets within dummy app'
task :precompile do
Dir.chdir('test/dummy') do
system('bundle exec rake assets:precompile')
end
end
end
I also disabled JS compression (or you can add uglifier gem to the Gemfile).

Resources