I currently have a running jekyll set up on heroku and use the following procfile:
web: jekyll serve --no-watch --port $PORT --host 0.0.0.0
I think the issue is somewhere here, but my rss feed xml returns this and obviously doesn't have the domain name that I need to set links.
What is going on here?
The best way I found to handle this is to bundle jekyll inside Rack. http://rack.github.io/
You can do this by creating a config.ru and a Rake task that handles the build
Rakefile:
namespace :assets do
task :precompile do
puts `bundle exec jekyll build`
end
end
config.ru:
require 'rack/contrib/try_static'
require 'rack/contrib/not_found'
use Rack::TryStatic,
:root => "_site",
:urls => %w[/],
:try => ['index.html', '/index.html']
run Rack::NotFound.new('_site/404.html')
Related
I would like to user Redis in my Sinatra app. Though I can access Redis instance in the console on local and remote (heroku), when I want to use it in a rake task, an error is triggered and I don t seem to get why is that.
app.rb:
class MyApp < Sinatra::Base
configure do
uri = URI.parse(ENV["REDISCLOUD_URL"])
$redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
end
config.ru:
require 'rubygems'
require 'sinatra'
require './app'
run MyApp
Gemfile
gem 'redis'
Rakefile.rb
desc 'Try Redis'
task :try_redis do
puts $redis.set("try", 0)
end
rake aborted! NoMethodError: undefined method `set' for nil:NilClass
I am not really used to Sinatra, and nothing looks particularly wrong to me. I do not understand why my global variable $redis would not be accessible from everywhere in my app...
If you can enlighten me, thank you by advance!
I do not understand why my global variable $redis would not be
accessible from everywhere in my app
Your rake task is not related to your app. The $redis variable available only when you run sinatra server and not available when you run your rake task. The rake task run in own thread.
I have a very basic ruby example running on Thin, but I would like to know how to translate this example to use Unicorn or Puma as the HTTP server instead. Here is the code I have now:
require 'rack'
class HelloWorld
def talk()
return "Hello World!"
end
end
class SomeServer
def start(server_object, port)
app = proc do |env|
[ 200, {"Content-Type" => "text/html"}, [server_object.talk()] ]
end
Rack::Handler::Thin::run(app, :Port => port)
end
end
SomeServer.new.start(HelloWorld.new, 3000)
This runs fine and well, but I cannot figure out how to make it run using Puma or Unicorn instead. Most online documentation I find for the two is for Rails apps. How can I utilize the multi-threading capabilities of these servers with this simple program?
use sinatra.
So to take it step by step first install sinatra and puma gems
gem install sinatra
gem install puma
then create a file myapp.rb
require 'sinatra'
configure { set :server, :puma }
get '/' do
"Hello World!"
end
then run the file
ruby myapp.rb
by default sinatra listens on 4567 so go to localhost:4567
you can configure puma to listen on a specific port or do a lot of other things using a config file read the documentation
A minimal example that doesn't require additional gems looks like this.
Using a single file
Create a puma config file config.rb with the following content:
app do |env|
body = 'Hello, World!'
[200, { 'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }, [body]]
end
bind 'tcp://127.0.0.1:3000'
and run puma with
puma -C /path/to/config.rb
That's it.
Using a configuration and a rackup file
In the example above, the config file contains the application itself. It makes sense to move the application to a rackup file: Create a rackup file app.ru with the following content:
class HelloWorld
def call(env)
body = 'Hello, World!'
[200, { 'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }, [body]]
end
end
run HelloWorld.new
Then update your config.rb, removing the app and linking the rackup file instead:
rackup '/path/to/app.ru'
bind 'tcp://127.0.0.1:3000'
and run puma as before with
puma -C /path/to/config.rb
The example configuration file for puma is helpful. (Update: This example configuration file is no longer maintained. The authors refer to dsl.rb instead.)
Instead of starting your app with Rack::Handler::Thin, you should be able to use the Puma Rack handler instead, like this:
Rack::Handler::Puma.run(app, :Port =>port)
You'd need to also require 'rack/handler/puma' after installing the Puma gem.
I'm trying to start my application. Is a unicorn + foreman + sinatra application.
This is my config.ru file:
require "rubygems"
require "sinatra"
Bundler.require
require File.expand_path '../twitter_module.rb', __FILE__
run TwitterModule
require File.expand_path '../lib/tweet_streamer.rb', __FILE__
require 'sidekiq/web'
run Rack::URLMap.new('/' => Sinatra::Application, '/sidekiq' => Sidekiq::Web)
this is my sidekiq.yml:
---
:pidfile: tmp/pids/sidekiq.pid
development:
:verbose: true
:logfile: log/sidekiq_development.log
and this is my Procfile:
unicorn: bundle exec unicorn -c config/unicorn.rb
sidekiq: bundle exec sidekiq -C config/sidekiq.yml -e development -r lib/workers
The problem now is that I always give this error:
2013-10-12T15:30:23Z 28234 TID-ov4rh38wo INFO: Please point sidekiq to a Rails 3/4 application or a Ruby file
2013-10-12T15:30:23Z 28234 TID-ov4rh38wo INFO: to load your worker classes with -r [DIR|FILE].
What am I missing?
Instead of specifying the lib/workers/ directory as the -r option for sidekiq, it's necessary to point it to some file which will deal with the necessary requires and setup for your workers. For example, you might have an environment.rb file in the root of your project, requiring all the workers within the lib/workers/ directory; you'd specify this with Sidekiq by using -r ./environment.rb. It might perhaps be useful to note that Sidekiq won't load your config.ru file by itself as it doesn't use Rackup; instead, you might like to extract anything necessary to an environment.rb file or similar, and require that from within config.ru.
So i am trying to serve my static assets from Amazon s3 locally and for Heroku, I was loading a yml file but that doesn't work as Heroku doesn't accept symlinks.
So i was given the suggestion to use ENV variables as Heroku also uses these. I have a rake task to precompile the assets to AWS. two problems at the moment
1) My ENV variables are not being read.(Fog directory can't be blank, Aws access key can't be blank, Aws secret access key can't be blank
2) When running a rake task i also get the error 'already initialized constant VALID_CHARACTER'
So the activesupport constant is being loaded twice?
My setup
env.rb
ENV['aws_bucket'] = 'bucketname'
ENV['aws_access_key'] = 'myaccesskey'
ENV['aws_secret_key'] = 'mysecretkey'
Rakefile
require 'bundler/setup'
Bundler.require(:default)
require './env' if File.exists?('env.rb')
AssetSync.configure do |con|
con.fog_provider = 'AWS'
con.fog_region = 'eu-west-1'
con.fog_directory = ENV['aws_bucket']
con.aws_access_key_id = ENV['aws_access_key']
con.aws_secret_access_key = ENV['aws_secret_key']
con.prefix = "assets"
con.public_path = Pathname("./public")
end
namespace :assets do
desc "Precompile assets"
task :precompile do
AssetSync.sync
end
Gemfile
source :rubygems
gem 'sinatra'
gem 'pony'
gem 'sinatra-flash'
gem 'heroku'
gem 'asset_sync', git: 'git://github.com/ejholmes/asset_sync.git', branch: 'sinatra'
UPDATE
AssetSync has activesupport in it's gemspec so that's getting included any way. It seems to be conflicting with the constant defined in the mail gem from the pony gemspec.
So with the Pony gem removed i can precompile assets locally, but when i try and compile for heroku nothing happens, it starts the rake task but then just goes back to the terminal ready for a new command.
The other thing is i need Pony for my mailer, how can i get around this?
To get rid of the clash between Pony and when running Rake locally, put the gems into different groups, e.g.
# Gemfile
group :assets do
gem 'asset_sync', git: 'git://github.com/ejholmes/asset_sync.git', branch: 'sinatra'
end
group :mail do
gem "pony"
end
# moreā¦
in the Rakefile
Bundler.require(:assets,:database,:whatever_else_you_need)
in the rackup/app file
Bundler.require(default,:assets,:database,:mail,:whatever_else_you_need)
As to your other problem, you should set the env vars for production via heroku config (see https://devcenter.heroku.com/articles/config-vars) and load them locally using the Rakefile as I said in the other question you asked about this. The env vars will live for the extent of the Ruby process, so if you load them in via Rake and also start the local server within the same Rake process you'll get Sinatra picking up all the env vars.
Edit: env vars will last as long as the process that added them, so if you put them in a dependent task the following task will have access to them:
namespace :assets do
task :environment do
AssetSync.configure do |con|
con.fog_provider = 'AWS'
con.fog_region = 'eu-west-1'
con.fog_directory = ENV['aws_bucket']
con.aws_access_key_id = ENV['aws_access_key']
con.aws_secret_access_key = ENV['aws_secret_key']
con.prefix = "assets"
con.public_path = Pathname("./public")
end
end
desc "Precompile assets"
task :precompile => :"assets:environment" do
AssetSync.sync
end
You might want to split this into different questions. This makes it easier to help you. As for your first question: I assume you didn't put the env.rb under version control?
Why are your env vars not being picked up by Sinatra? Because you configure Fog in the Rakefile, and Simatra never sees that file. It's used by rake only.
I'd suggest you put the Fog configuration into a third file and require that in both Rakefile and Sinatra app.
Resque/Sidekiq come with a web frontend, which is a Sinatra app.
The way to mount this in a Rails app is to add this to routes (http://railscasts.com/episodes/366-sidekiq?view=asciicast):
mount Sidekiq::Web, at: "/sidekiq"
How do i mount this in a Padrino app?
Mapping it in config.ru like other Rack apps does not work
map '/sidekiq' do
run Sidekiq::Web
end
Padrino uses Padrino.mount which expects apps to respond to dependencies and setup_application. This hack (https://gist.github.com/1718723) allows you to mount a Sinatra application inside a Padrino application
Padrino app is a rack app and in config.ru you would see
require ::File.dirname(__FILE__) + '/config/boot.rb'
run Padrino.application
You can change this to use Rack::URLMap
require ::File.dirname(__FILE__) + '/config/boot.rb'
run Rack::URLMap.new("/sidekiq" => Sidekiq::Web.new, "/app" => Padrino.application.new)
Add gem 'sidekiq' to Gemfile
bundle install
Add following lines to config/boot.rb
Padrino.before_load do
Padrino.dependency_paths << Padrino.root('app/workers/*.rb')
end
Add following lines to config/apps.rb
require 'sidekiq/web'
Padrino.mount('Sidekiq', app_class: 'Sidekiq::Web', app_root: Sidekiq::Web.root).to('/sidekiq')
Create any worker in app/workers/
Run bundle exec sidekiq -r ./config/boot.rb