Wednesday, January 25, 2017

setting up automatic deployment via git

If you wish to have automatic deployment with rails and thin, here is an example:

1st, clone your repository on server to a path.
Next, vi .git/hooks/post-merge
add in your bash script to run after git is pull
example:
bash -lc /pathtoroot/deploy.sh

chmod a+x .git/hooks/post-merge

in your git repository, add a file called: deploy.sh, you may code any deployment command after git pull, example:
RAILS_ENV=production
cd /pathtoroot
echo "deploy.sh: bundle install"
bundle install
echo "deploy.sh: rake db:migrate"
bundle exec rake db:migrate
echo "deploy.sh: rake assets:precompile"
bundle exec rake assets:precompile
echo "deploy.sh: restarting"
bundle exec thin -C config/thin.yml restart
bundle exec sidekiqctl stop tmp/sidekiq.pid 0
bundle exec sidekiq -d -P tmp/sidekiq.pid -L log/sidekiq.log
echo "deploy.sh: Done!"

makesure you have chmod a+x deploy.sh and commit this file via git on your local machine.

on server, add a cron job to automatically pull on a period basis:
crontab -e
---
* * * * * bash -lc "cd /pathtoroot/ && git pull"
* * * * * sleep 30 && bash -lc "cd /pathtoroot/ && git pull"
---

2nd command allows you to run git pull within 30seconds.

Once git pull is successful, it will auto trigger post-merge and run your deploy.sh

hope it helps :)





Thursday, May 5, 2016

beego bee tool migration error?

beego default migration tool does not show any error,
its very annoying.

here is a fix, either
go get github.com/u007/bee

OR

clone this repository instead,
and goto $gopath
cd github.com/u007/bee
run:
go build
go install

and now, rerun your bee migrate

Sunday, April 24, 2016

rails 5 hstore hash

Rails 5 now returns params as a class instance, no longer a hash.
due to the requirement to have random hashs key based on user input,
the only way to do this is to force this value by calling .to_unsafe_h

example:
res = params.require(:detail).permit(:name, :year)
res[:specification] = params[: detail][:specification].to_unsafe_h

but if you do know your hash key, call this instead:
permit(:specification => { :keyname1, keyname2 }

if you have any suggestion, please comment :)

https://forum.upcase.com/t/rails-doesnt-recognize-a-request-as-patch/5115

http://eileencodes.com/posts/actioncontroller-parameters-now-returns-an-object-instead-of-a-hash/

Tuesday, April 12, 2016

speed up rails development

One of the main concern with rails development is the speed.
its very slow indeed when your stack grows.

I found several reasons for it to be slow:

  1. assets, when debug is enabled, each individual js and css file will be loaded individually
  2. navigation menu with permission triggers query and outputting links to admin requires some loading time
  3. crud - list and read is rendered everytime even though record is not updated, this can save more time
  4. sassc - compiling and loading sass is very slow in ruby

1. Assets

If you aren't planning to debug any scripting or css, set
config.assets.debug = false # set this to false if you dont plan to debug your css or js
config.assets.digest = true # this recommended permanently to allow cache on browser


2. Navigation menu

Navigation menu via cancancan gem requires some query to the database. Often this file is not changed frequently, its almost once setup, its forgotten. Therefore it make sense to cache this file based on user session. One way todo this is to cache this based on template name with prefix and current user id.

example:
views/layout/admin/_nav.html.slim
- cache("template_layout_admin_nav_#{current_user.id rescue 0}") do 
  nav. ...
     ... (more code here)

3. List & read

Since most record will not be changed, it make sense to cache each individual view fragment for respective model to be cached. They call this russian doll caching. Rails cache is smart enough to update cache when model updated_at changes. This can be applied to both listing and show.

example listing template:

tbody
  - @rows.each do |row|
    - cache row do
      td = row.id
      td ...

example show template:
- cache @record do
  .panel-body.resource.box.p-a
    .row
      .col-md-12
        = @record.id

4. Use sassc

Sass compilation on rails is slow. Recently, we have found an alternative for faster sass compilation. Make use of sassc, add this into Gemfile.
gem 'sassc-rails', group: [:development, :test, :staging]

It is not recommended to run assets precompile on production server. Its slow and takes a long time to update on server. One way to do this is to compile assets locally.

Before every deployment, run this to update your assets. But because we do not wish to load static assets on our development, add this to config/environment/development.rb
config.assets.prefix = '/dev-assets'

in config/application.rb
config.assets.initialize_on_precompile = false # this ensure local assets precompile for production works without the need to connect to production database

This configuration ensure all assets is delivered via dev-assets on local development, while production assets is generated into public/assets and retrieved via /assets

To compile assets for production, run this:
export RAILS_ENV=production; bundle exec rake assets:precompile

If you have relative uri to your rails instance, call this:
export RAILS_ENV=production; export RAILS_RELATIVE_URL_ROOT=/shop; bundle exec rake assets:precompile
# where /shop = uri to your site

To enable cache in development

in config/environment/development.rb,
config.action_controller.perform_caching = true
config.cache_store = :file_store, Rails.root.join('tmp', 'cache'), { size: 64.megabytes }
# config.cache_store = :memory_store, { size: 64.megabytes }
    config.public_file_server.headers = {
      'Cache-Control' => 'public, max-age=172800'
    }

It is recommended to set cache memory_store, so that every time you wish to clear cache, simply restart the rails instance. But if you want to have it persistant on every time, use file_store cache.
But if you ever change the template file in navigation, you will need to clear the cache directory
rm -Rf tmp/cache/#
# = represent the cached folder (ls -lat to get the current directory name)



For further speed tuning, please follow the instruction below:
https://www.nateberkopec.com/2015/08/05/rack-mini-profiler-the-secret-weapon.html








Tuesday, March 22, 2016

turbolinks 5 and ckeditor

turbolinks 5 gives some pretty neat features.
it no longer needs turbolinks-jquery.

but in some way, it doesnt work properly due to cache within turbolinks.
a work around this issue would be calling:

#coffeescripts:
jQuery(document).on('turbolinks:load', (e)->
    #do your initialize here
    if Turbolinks
        Turbolinks.Cache()
)

calling turbolinks cache helps to keep the rendered form intact.
but for some reason, this doesn't work with ckeditor.

the work around is to add this in the turbolinks:load function

$('textarea.ckeditor').each(->
    if $(this).css('visibility') != 'hidden'
      # console.log(this)
      CKEDITOR.replace(this)
  )

hope it helps :)


Thursday, February 18, 2016

rails local assets precompile

if you have spring, you may not run bin/rake or bin/rails assets:precompile.

here is a way todo it right

RAILS_ENV=production bundle exec rake assets:precompile

ensure that you have (config/application.rb) set
config.assets.initialize_on_precompile = false

and for development:
config/environment/development.rb
config.assets.prefix = '/dev-assets' # to avoid assets precompile everytime u change a css or js




Wednesday, January 13, 2016

gem install mysql -v 0.3

ive issues with rails 4.1 that it does not play well with mysql2 v 0.4 .
im forced to use mysql2 v 0.3.20

when i ran gem install mysql -v 0.3.20
i came to this error:

errmsg.h is missing

ive already had mysql-dev installed.
after searching around, i found the issue.
cpanel mysql-config is still using mysql 5 dev library.
when i ran which mysql-config, and mysql-config, ive gotten cpanel 3rd party bin with v5.


so i renamed cpanel own mysql-config, and relink the correct mysql-config to cpanel/bin
mv /usr/local/cpanel/3rdparty/bin/mysql_config /usr/local/cpanel/3rdparty/bin/mysql_config5

ln -s /usr/bin/mysql_config /usr/local/cpanel/3rdparty/bin/mysql_config

reran gem install and bundle, and it works great now!