ssh -i ~/.ssh/my_pem.pem root@my_aws_ip.eu-west-1.compute.amazonaws.com -v
-v show you ssh debug
Tuesday, 4 December 2012
Monday, 26 November 2012
gitstats generator
1. Install Gitstats
git clone git://github.com/hoxu/gitstats.git
and Gnuplot
brew install gnuplot
--------------------------------------------
2. From "Gitstats" dir
gitstats /Users/yanniskolovos/Sites/Project_name Project_name
gitstats target output
--------------------------------------------
Requirements
Git Python Gnuplot
--------------------------------------------
http://stackoverflow.com/questions/9301759/how-can-i-use-gitstats-to-find-out-how-many-sloc-a-git-repo-has-in-total-and-per
git clone git://github.com/hoxu/gitstats.git
and Gnuplot
brew install gnuplot
--------------------------------------------
2. From "Gitstats" dir
gitstats /Users/yanniskolovos/Sites/Project_name Project_name
gitstats target output
--------------------------------------------
Requirements
Git Python Gnuplot
--------------------------------------------
http://stackoverflow.com/questions/9301759/how-can-i-use-gitstats-to-find-out-how-many-sloc-a-git-repo-has-in-total-and-per
Tuesday, 23 October 2012
Sunday, 21 October 2012
Saturday, 13 October 2012
Wednesday, 10 October 2012
migrating to (PostgreSQL) 9.2.1
From logs
DETAIL: The data directory was initialized by PostgreSQL version 9.1, which is not compatible with this version 9.2.1.
FATAL: database files are incompatible with server
#1. Stop the server
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
#2. Move old files
mv /usr/local/var/postgres /usr/local/var/postgres.old
#3.Create a new dir
initdb /usr/local/var/postgres
# DONE
# Success. You can now start the database server using:
# postgres -D /usr/local/var/postgres
# or
# pg_ctl -D /usr/local/var/postgres -l logfile start
#4.Upgrade all db
pg_upgrade -d /usr/local/var/postgres.old/ -D /usr/local/var/postgres -b /usr/local/Cellar/postgresql/9.1/bin -B /usr/local/Cellar/postgresql/9.2.1/bin
Tuesday, 9 October 2012
Monday, 8 October 2012
Friday, 5 October 2012
Thursday, 13 September 2012
rails migration set defaults
class AddDefaultMigration < ActiveRecord::Migration
def self.up
change_column :tasks, :status, :string, :default => default_value, :null => false
end
end
also
https://github.com/bsm/attribute-defaults
Wednesday, 12 September 2012
simple datetimepicker in rails
http://www.jakoblaegdsmand.com/blog/2012/05/simple-datetimepicker-in-rails
caller
See full stack flow of execution ..very useful for debugging
c =caller.join("\n <br/>")
render :text => c
/Users/yanniskolovos/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
/Users/yanniskolovos/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/base.rb:167:in `process_action'
/Users/yanniskolovos/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/rendering.rb:10:in `process_action'
/Users/yanniskolovos/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
/Users/yanniskolovos/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-
and so on
c =caller.join("\n <br/>")
render :text => c
/Users/yanniskolovos/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
/Users/yanniskolovos/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/base.rb:167:in `process_action'
/Users/yanniskolovos/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_controller/metal/rendering.rb:10:in `process_action'
/Users/yanniskolovos/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
/Users/yanniskolovos/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-
and so on
tap
each element
[1,2,3,4,5].each {|a| a.tap {|t| p t + 10}}
=>
11
12
13
14
15
Tap the array
[1, 2, 3].tap(&:reverse!)
=> [3, 2, 1]
unique Relationship
class Relationship < ActiveRecord::Base
belongs_to :entry
belongs_to :tag
validates :tag_id, :uniqueness => { :scope => :entry_id }
end
Thursday, 6 September 2012
Tuesday, 4 September 2012
heroku-accounts
Or you can choose a fully-automated approach:
$ heroku accounts:add work --auto
Enter your Heroku credentials.
Email: work@example.org
Password: ******
Generating new SSH key
Generating public/private rsa key pair.
Your identification has been saved in ~/.ssh/identity.heroku.work.
Your public key has been saved in ~/.ssh/identity.heroku.work.pub.
Adding entry to ~/.ssh/config
Adding public key to Heroku account: work@example.org
To switch an app to a different account:
# in project root
heroku accounts:set personal
To list accounts:
$ heroku accounts
personal
work
To remove an account:
$ heroku accounts:remove personal
Account removed: personal
Set a machine-wide default account:
$ heroku accounts:default personal
To clone a git repository from Heroku, change 'heroku.com' to the Host of the desired account defined in your .ssh/config:
$ git clone git@heroku.work:repository.git
If you want to switch the account for an app:
$ heroku accounts:set work
This also changes the URL of the git origin heroku
to make sure you're using the correct SSH host.
https://github.com/ddollar/heroku-accounts
rake db production
Production environment in localhost
rake db:migrate RAILS_ENV="production"
RAILS_ENV=production bundle exec rake assets:precompile
rails s -e production
for heroku:
heroku create --stack cedar
Thursday, 30 August 2012
Friday, 24 August 2012
sam-stephenson-of-37signals
http://alittlecode.com/coffeescript-intro-video-by-sam-stephenson-of-37signals/
Tuesday, 14 August 2012
FAST KEY REPEAT RATE vic nava
FAST KEY REPEAT RATE
the fastest rate by system preference is 2
need too logout/login to see the effect
defaults write NSGlobalDomain KeyRepeat -int 0
Saturday, 11 August 2012
rais in production compile assets
in : config/environments/production.rb
config.assets.compile = true
Then run:
rake assets:precompile --trace RAILS_ENV=production
Then:
rails s -e production
Thursday, 9 August 2012
Wednesday, 8 August 2012
Monday, 6 August 2012
Thursday, 2 August 2012
Spork and Rpsec and Bash
in .profile
Start Spork if is not runing and pass params the file
function rtest{
if pgrep -f spork; then echo running; else spork &; fi
rspec --drb --color --format doc "$@"
}
rtest spec/requests/whatever
Tuesday, 31 July 2012
Saturday, 28 July 2012
what?
%w{rubygems pp wirble what_methods}.each do |lib|
begin
require lib
rescue LoadError => err
$stderr.puts "Couldn't load #{lib}: #{err}"
end
end
%w{init colorize}.each { |str| Wirble.send(str) }
if I have an object, what method can I call on it to get that result?
1.9.3-p194 :009 > "hello".what? "HELLO"
"hello".upcase == "HELLO"
"hello".swapcase == "HELLO"
"hello".upcase! == "HELLO"
"hello".swapcase! == "HELLO"
Thursday, 19 July 2012
Struct && delegate
Sunday, 15 July 2012
Saturday, 14 July 2012
location aware realtime visitor tracker with pusher and php
http://www.webresourcesdepot.com/location-aware-realtime-visitor-tracker-using-pusher-tutorial/
underscore
Underscore provides 60-odd functions that support both the usual functional suspects:map, select, invoke — as well as more specialized helpers: function binding, javascript templating, deep equality testing, and so on. It delegates to built-in functions, if present, so modern browsers will use the native implementations offorEach, map, reduce, filter, every, some and indexOf.
http://documentcloud.github.com/underscore/
http://documentcloud.github.com/underscore/
nowjs
Server push
You can call easily client functions from the server and server functions from the client. That means you can push to the client simply by calling a client-side Javascript function on the server. Communication is achieved using Socket.io
Use me for push notifications: http://nowjs.com/
Moment.js
---------------------------------
<!-- <html>
<head>
<title>in a Moment.js</title>
</head>
<body>
<h1>Moment.js here now: <span id="then" data-date="Sat Jul 14 2012 07:43:14 GMT-0400 (EDT)"></span></h1>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://raw.github.com/timrwood/moment/1.6.2/moment.js"></script>
<script>
$(document).ready(function(){
var then = $('#then'),
date = moment(new Date(then.attr('data-date'))),
update = function(){
then.html(date.fromNow());
};
update();
setInterval(update, 6000);
});
</script>
</body>
</html>
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Formatting dates using the Moment.js date library</title>
</head>
<body>
<p><time data-momentjs>2012-07-12</time></p>
<p><time data-momentjs>1976-09-09</time></p>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/timrwood/moment/1.1.1/moment.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$("time[data-momentjs]").each(function (idx, item) {
var $item = $(item),
mdate = moment($item.text(), "YYYY-MM-DD");
$item.attr("title", mdate.format("ddd, MMM Do, YYYY")).text(mdate.fromNow());
});
});
</script>
</body>
</html>
------------------------------
Moment.js here now:
Friday, 13 July 2012
Tuesday, 10 July 2012
ror db Snapshot by 37signals
Snapshot
The snapshot plugin adds two new rake tasks that make it easy for you to take a snapshot of your existing (development) database, and restore it again.
Why would you want this?
Imagine you are developing an app. You've spent a few hours filling your development DB with data so that you can design a particular UI scenario. Now, though, you need to design another scenario, which requires a different dataset, and you are loathe to lose the data you so laboriously entered.
The snapshot plugin saves the day:
$ rake db:snapshot
This creates a db/snapshot file (which records all the data in your DB), and a db/snapshot.schema file (which records the state of the schema when the snapshot was taken). At any time, then, you can restore that snapshot:
$ rake db:snapshot:restore
This will erase the existing DB, restore the db/snapshot.schema schema, and then load the data. If there are any pending migrations, it will then run those, and then regenerate the snapshot so that it stays at the latest schema.
You can pass a different snapshot file to use as a parameter, to either task:
$ rake db:snapshot[scenarios/real-estate]
...
$ rake db:snapshot:restore[scenarios/telemarketer]
Limitations
The current version will probably fail when there are foreign key constraints, since the order the tables and rows are restored is not guaranteed to be in any particular order.
Also, even moderately large data sets (e.g. multiple thousands of rows) may result in poor performance during snapshot and restore, since the data is all loaded into memory.
https://github.com/37signals/snapshot
DJ demons
On production, you would want the worker to be running all the time. Also, it will be nice to have the ability to stop the worker just before the deployment and start it again once the deployment finishes. It sounds complicated, but its pretty easy to set up.
First, install the daemons gem:
# Add in environment.rb
config.gem 'daemons'
$ rake gems:install
Then, copy this script in your rails project in file script/delayed_job and give it execute permission. This creates a worker daemon, which when started keeps running as a background process.
Alternately, instead of daemons gem, you can use daemon-spawn gem.
Install the gem on the host where you want the delayed_job daemon to run:
$ sudo gem sources -a http://gems.github.com
$ sudo gem install alexvollmer-daemon-spawn
And then copy this script instead in script/delayed_job. Rest all of the following steps are identical, whichever gem and script/delayed_job you choose.
# start the worker daemon
$ ruby script/delayed_job start
# stop it
$ ruby script/delayed_job stop
http://8raystech.com/2009/2/28/background-job-processing-in-rails-with-delayed_job
Sunday, 8 July 2012
Wednesday, 20 June 2012
Running Pow with Apache
If you want to run pow alongside the MacOS X built-in apache this page will instruct you on how to setup pow so that it doesn't takeover port 80 and instead apache will reverse proxy requests to pow.
For the Impatient
$ curl get.pow.cx/uninstall.sh | sh #if you have pow installed
$ echo 'export POW_DST_PORT=88' >> ~/.powconfig
$ sudo curl https://raw.github.com/gist/1058580/zzz_pow.conf -o /private/etc/apache2/other/zzz_pow.conf
$ sudo apachectl restart
$ curl get.pow.cx | sh
Full Installation Instructions
1. Before installing
Before you install pow, add the following line to your ~/.powconfig
export POW_DST_PORT=88
Here it is as a one-liner
$ echo 'export POW_DST_PORT=88' >> ~/.powconfig
This will cause pow's firewall run to redirect all traffic from port 88 instead of port 80. You can pick any port you like for this if you use 88 for something else. It doesn't affect the remainder of these instructions. If you have already installed pow, you will need to uninstall it before you you continue.
2. Install pow as normal
If you need those instructions, you can find them here: Installation
3. Configure Apache
You'll need to drop the file in this gist into
/etc/apache2/other/zzz_pow.conf
Here it is as a one-liner
$ sudo curl https://raw.github.com/gist/1058580/zzz_pow.conf -o /etc/apache2/other/zzz_pow.conf
This sets up apache to act as a reverse proxy to pow. It goes directly at pow's default listen port, not the firewall port we configured in the previous step. The file name zzz_pow.conf to ensure that apache picks up the vhost for pow last, allowing you to put vhosts for other apps in front of it.
4. Start/Restart Apache
You'll need to turn on "Websharing Sharing" in the MacOS X system preferences if you haven't already. You can find it in System Preferences -> Sharing -> Web Sharing. NOTE: This will make it so anyone with your machines IP Address can access your apache server. This is the default setting from apple. If you want to change this, you will want to change the IP address that apache binds to in /etc/apache2/httpd.conf.
If Apache is already running, you can restart by either turning Web Sharing off and then back on, or you use this on the command line
$ sudo apachectl restart
After Installation
Leveraging pow's DNS for other apps
If you now configure your vhosts for the apps you want to run in apache with *.dev hostnames, pow will provide the DNS resolution for you, which means you get to keep the benefit of not mucking with your hosts file. For example, if you have a php app, you can setup its vhost entry to use my_php_app.dev and you don't need to add that to your hosts file.
https://github.com/37signals/pow/wiki/Running-Pow-with-Apache
Monday, 18 June 2012
Wednesday, 13 June 2012
rake
namespace :pick do desc "Pick a random user as the winner" task :winner => :environment do puts "Winner: #{pick(User).name}" end desc "Pick a random product as the prize" task :prize => :environment do puts "Prize: #{pick(Product).name}" end desc "Pick a random prize and winner" task :all => [:prize, :winner] def pick(model_class) model_class.find(:first, :order => 'RAND()') end end
In lib/task/something.rake
terminal:
rake pick:winner
rake pick:prize
rake -T
Tuesday, 12 June 2012
Monday, 4 June 2012
render javascript in rails (text format)
headers["Content-Type"] = "text/javascript"
render(:partial => "get_bookmark_script")
Saturday, 26 May 2012
observers
Posted by ihower on July 24, 2010
Observer serves as a connection point between models and some other subsystem whose functionality is used by some of other classes, such as email notification. It is loose coupling in contract with model callback.
Bad Smell
class Project < ActiveRecord::Base
after_create :send_create_notifications
private
def send_create_notifications
self.members.each do |member|
ProjectMailer.deliver_notification(self, member)
end
end
end
In this example, we use the model callback to send email notifications after creating a project. It would be better to use Observer because the email notification is a subsystem whose functionality is used by other classes.
Refactor
class Project < ActiveRecord::Base
# nothing here
end
class NotificationObserver < ActiveRecord::Observer
observe Project
def after_create(project)
project.members.each do |member|
ProjectMailer.deliver_notice(project, member)
end
end
end
Using the Observer, there is no necessary for Project to know about email notification, the NotificationObserver is responsible for sending emails after create a project, it may also sending emails for creating or updating an issue. It's much easier to maintain email notifications in one Observer than some email notification functions distributed over different models.
Updated: thanks @carlosantoniodasilva
http://rails-bestpractices.com/posts/19-use-observer
node.js + Socket.io + Bash. A collaborative terminal for your browser
https://gist.github.com/947512
Thursday, 24 May 2012
Tuesday, 22 May 2012
Monday, 21 May 2012
workng with meteor
Try it yourself
In about 3 minutes, you'll make your own copy of Leaderboard and deploy it live on the Internet for you and your friends to use. No programming knowledge required!
Pick a name for your new app.
I'll call it...
.meteor.com
new suggestion
Install Meteor (if you haven't already). In your Terminal window, run:
$ curl install.meteor.com | sh
Make a copy of the example.
$ meteor create --example leaderboard
Get it running on the cloud.
$ cd leaderboard
$ meteor deploy testing-app.meteor.com
Now, open
testing-app.meteor.com
in a new tab in your web browser. There's your app!
http://meteor.com/examples/leaderboard
Push Service
Push Example
=========================================
create new server and client for push/sub notifications
----------------
client: http://stream-channel.herokuapp.com/
server: http://stream-server.herokuapp.com/
code:https://github.com/msroot/push-sub-example-ror
Sunday, 20 May 2012
High performance Publish/Subscribe solution for Ruby On Rails or Juggernaut vs. Faye
In this post I want to share some experience in pub/sub for RoR. First, I'll briefly explain what kind of application we have, why we've chosen Faye and then I'll say how to run many instances of Faye without any Load Balancers and without Redis.
One of RoR apps I've developed is handling more than 30 000 RMP (requests per minute) while i'm writing this post (and it is ready to handle more). About 90% of requests do pushes to Faye. About 6 000 users are connected to Faye right now. Everything is working very stable and smooth. And we are going to grow :)
But before Faye we used Juggernaut. These technologies are pretty same. And both are easy to use and integrate with Rails app. We've choosen Juggernaut because it was... more popular I think. So, we've patched it a little bit for our needs and quickly integrated to our application. Everything was good untill first deployment to production with less than hundred online users. It loaded servers - but we were ready to accepted it. But then we found another issue - it was very unstable. If somebody pushes broken data to it - process dies. Totally dies. So we've added monit to monitor Juggernaut. Not really cool, right? We've been patching it a lot. Still, it was not stable and sometimes it was using so much CPU that we've decided to look on another solution. And here we've found out Faye.
Before describing Faye I want to say that Juggernaut has 1 great benefit which Faye doesn't have - you can push events directly to Redis and Juggernaut will catch it and process. You can't easily do the same with Faye (maybe it will be done in future). Instead of it you have to send a HTTP request, which is slower and loads Faye's server.
So, we've decided to switch to Faye. We've choosen node.js instead of Thin and after first deployment we found the difference - Faye is stable and doesn't load system at all.
And now, finally, about high-performance and scalability.
Both Node.js and Thin are very-very slow if you run it with SSL - check my next post to see how to solve this problem.
First of all, you need many instances of Faye to support a lot of concurrent users. Faye supports Redis as a shared storage (in experimental mode, but it seems to be stable). It gives you a possibility to run many instances on many servers but it's slow - it needs to communicate with Redis. So, we've decided to create our own simple mechanism of sharding instead of using Redis.And we didn't want to use one more Load Balancer for it.
Note: this mechanism is designed to work when user subscribes to his own channel. To push data to global channel you need to perform requests to each shard.
Let's say we have a domain name app.com pointing to Load Balancer for Rails app. All our servers have sub-domains like server1.app.com, server2.app.com etc.
Configuration files:
We've created a YAML config where we listed all our instances. It looks like:
production:
shards:
-
node_port: 42000
node_host: server1.app.com
node_local_host: 10.x.x.1 #local IP of server
run_on: server1_hostname
-
node_port: 42001
node_host: server1.app.com
node_local_host: 10.x.x.1
run_on: server1_hostname
-
node_port: 42000
node_host: server2.app.com
node_local_host: 10.x.x.2
run_on: server2_hostname
.....
run_on option is used by our own Rake task to detect what shard to start on specific server during deployment.
node_host is a public domain name or IP - we are using it to generate URL for users.
node_local_host is a local IP of server, cause we want to push data through local interfaces.
Sharding:
We assign shard for user by very simple formula:
shard = @shards[user.id % @shards.size]
If you have 3 shards, users with ids 0, 3, 6 are connected to 1st shard; users with id's 1, 4, 7 - to 2nd...
Client side code:
client = new Faye.Client(<%= raw Faye.shard_for(user).url.inspect %>);
client.subscribe(<%= raw Faye.shard_for(user).channel.inspect %>, function(data){...});
Method .url returns URL like http://server1.app.com:42000/faye
Channel for user is just a "/#{user.id}", eg. '/123'.
So, now we need to push events to the needed shard:
...
uri = URI.parse(Faye.shard_for(user).local_url)
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Post.new uri.path
body = {'channel' => Faye.shard_for(user).channel, 'data' => data.to_json, 'ext' => {:auth_token => FAYE_TOKEN}}
req.set_form_data('message' => body.to_json)
http.request req
...
Method .local_url returns http://10.x.x.1:42000/faye.
http://rails-alex.blogspot.com.au/2011/10/high-performance-publishsubscribe.html
http://railscasts.com/episodes/316-private-pub
Subscribe to:
Posts (Atom)