Wednesday, October 20, 2010

Schedule Scripts With Interval of One Second

Cron jobs with default setting has default scheduling time Of 1MIN.
Below is the trick to operate in seconds.
#! /bin/bash
PROCNAME=’/usr/bin/xyz.rb’
PIDS=`ps -efa | grep $PROCNAME | grep -v grep | awk ‘{ print $2 }’`
for ff in $PIDS
do
echo “$ff” > /tmp/w
done
if [ -f "/tmp/w" ] ; then
pid=$(cat /tmp/w)
if [ "$pid" == "" ] ;then
/usr/local/bin/ruby /usr/bin/xyz.rb
else
echo “Already running $pid”
fi
rm -rf /tmp/w
else
/usr/local/bin/ruby /usr/bin/xyz.rb &
fi
Where xyz.rb is simple rb program in loop
require ‘rubygems’
require ‘daemons’
loop do
system(‘/usr/bin/Server_get |xargs >/var/www/html/text.txt’)
system(‘/usr/bin/get_app_stats |xargs >/var/www/html/app_stat.txt’)
sleep (25)
end
At the last  add it to crontab
* * * * * /usr/bin/fast_cron
cheers  its much useful in fast messaging,emailing campaigns .

Thursday, October 7, 2010

Caching and Forward name server on linux

acl trusted {
192.168.0.0/24;
localhost;
};

options {

directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";

forwarders {8.8.8.8;8.8.4.4};
listen-on port 53 { any; };
allow-query { trusted; };
allow-query-cache { trusted; };
};
logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};
view localhost_resolver {
match-clients { trusted; };
match-destinations { trusted; };
recursion yes;
include "/etc/named.rfc1912.zones";
};

Git Manual clone and push

For github:

git clone --bare git@github.com:myaccount/my-old-repo.git
cd my-old-repo
git push --mirror git@github.com:mycompany/our-new-repo.git
cd ..
rm -rf my-old-repo

Same can be applicable to private git using gitosis.



Monday, October 4, 2010

Use Memcache As Seesion Store in Rails

Install memcache :

yum install memcached .

chkconfig memcached on

/etc/init.d/memcached restart

Install gem :
gem install memcache-client

Add in environment.rb :

require 'memcache'
CACHE = MemCache.new(:namespace => "myapp)
CACHE.servers = '127.0.0.1:11211'
config.action_controller.session_store = :mem_cache_store

config.action_controller.session = {
:session_key => '_my_session',
:secret => '9abfc85851505e1a08sdfgsdfgsfdsfdgdsfgbb389be2acd0356ae0d4ea383f59cde7140ec7b04df473c31e1d4a4b9b78d55175d0c37bb29852c025d491c5cda9194ae',
:cache => CACHE,
:expires=>900 }

Note :
secret => do rake secret and paste the key .
port => default port is 11211

Clear Session From Database In Ruby On Rails

Create: app/models/session.rb

Session = CGI::Session::ActiveRecordStore::Session
Session.class_eval do
def self.sweep!
delete_all ['updated_at < ?', 15.minutes.ago.utc]
end

Add One cron jobs and swap scheduled :


1 4 * * * /usr/local/bin/ruby /app/apps/current/script/runner -e production Session.sweep!