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!


Sunday, January 10, 2016

rails admin date time input format

after upgrading to rails admin 0.8.1,
took me few hours trying to figure why all my datetime input fail to work.

it turned out that rails admin have forced their datetime input format based on
en.time.formats.long
this can be set in en.yml

otherwise, you may set the datetime by declaring in the configuration, etc:
field :created_at do
    date_format do
      :short
    end

end


for more formats:
http://apidock.com/ruby/DateTime/strftime


Monday, January 4, 2016

capistrano is slow, alternative: blazing

capistrano seems to be quite slow to deploy. it works great for production, as it export entire git into independent directory and relink directory from shared directory.

In some of my smaller projects,  i would prefer to maintain only 1 folder, and do a git hard reset and push the update directly from my local git.
my work around is to use blazing. since blazing 0.5, recipes is no longer supported.
blazing by default works great! but until you realize you are using spring, and passenger

ive customize blazing with spring generated bin removed from bin.

https://github.com/u007/blazing

to deploy:
  1. create to $HOME/rails/shared
  2. upload shared/config/application.yml (for figaro)
  3. mkdir shared/log
  4. mkdir shared/public/uploads
  5. mkdir shared/public/system
  6. mkdir shared/tmp
  7. mkdir shared/vendor/bundle

next, in your project,
  1. blazing init
  2. edit config/blazing.rb
    rake :"server:deploy"
  3. copy this file to lib/tasks/
    http://pastebin.com/13cTyvGg #if you use mod passenger
  4. blazing setup production
    # this will deploy git after deploy scripts on server
  5. blazing update production # to update later if changed blazing config

to deploy updates, simply:
  1. git push production master # remote: production, local branch: master

Sunday, December 27, 2015

undefined method `register_preprocessor' for nil:NilClass

seems to occur on sprocket-rails v3.0 sprockets-rails (2.3.3) works great! sprocketrails 2.3.3 + sprockets (3.5.2) it was due to less-rails https://github.com/rails/rails/issues/22647

Wednesday, November 25, 2015

dbflow raw query

As example:

class Abc {
public static String get_latest_updated_at(Class model, String where, Object... args) {
    ColumnAlias[] alias = new ColumnAlias[1];
    alias[0] = ColumnAlias.columnRaw("max(updated_at) as updated_at");

    String query = "select max(updated_at) as updated_at from "+FlowManager.getTableName(model);

    Cursor cursor = FlowManager.getDatabaseForTable(model).getWritableDatabase().rawQuery(query, null);
    if(cursor.getCount() > 1)
        return "";
    cursor.moveToFirst();
    return cursor.getString(cursor.getColumnIndex("updated_at"));
}


}

calling example:
Abc.get_latest_updated_at(Address.class, "")

//where by Address is model extending BaseModel

Monday, November 23, 2015

undefined local variable or method `try_spree_current_user'

If you create your custom spree controller,

beware that you will need to add in some spree controller helper to make the magic works

include Spree::Core::ControllerHelpers::Auth
include Spree::Core::ControllerHelpers::Store
include Spree::Core::ControllerHelpers::Order

or

you may extends from Spree::BaseController





Friday, October 23, 2015

authorize! for update

just realize calling model.update after authorize! is wrong!

the right way is to run an authorize!, then model.assign_attributes, then rerun authorize! again, 
and finally model.save

Tuesday, October 20, 2015

running rvm based cron job

rvm is requires a bash login to work.
in order to execute cron jobs, run this:

*/5 * * * * 'sh /home/myuser/versions/current/runme.sh'

create a shell file,
change working directory to rails root,
and run the required job, such as delayed_job once and exit

vi /home/myuser/versions/current/runme.sh
cd /home/myuser/versions/current
/bin/bash -l -c 'RAILS_ENV=production bundle exec rake jobs:workoff'



Thursday, October 1, 2015

speeding up vagrant and virtualbox synced folder on mac and windows


On MAC:
first, use nfs for sync folder,

in Vagrantfile:
web.vm.synced_folder "/private/var/www", "/var/www", type: "nfs", mount_options: ['rw', 'vers=3', 'tcp', 'fsc', 'actimeo=2']

actimeo=2 # increases speed of file sync

then do a: 
vagrant reload




On Windows:
web.vm.synced_folder "c:/wamp/www", "/var/www", type: "smb"

Notes: nfs on windows is not fully compatible. On linux guest machine, it does not allow file locking. This isn't good enough with rails, especially. Smb is an good option with file locking support, and fast response time. But this requires running command prompt as administrator to start smb command.


Hope it helps ;)

Friday, August 28, 2015

installing volley for android

Latest volley is available in maven repository,
therefore, you do not need to compile volley from source code.
goto Build > Library and dependencies > dependencies
click + add, search for volley
com.mcxiaoke.volley:library-aar:1.0.1


If you insists:
---old stuff---
Follow instruction here:
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

but first, makesure you have setup your environment and paths:

PATH=$PATH:/path-to-android-sdks/platform-tools:/path-to-android-sdks/tools:"/Applications/Android Studio.app/Contents/gradle/gradle-2.4/bin"

export ANDROID_HOME=/path-to-android-sdks

(Please change the path-to-android-sdks and gradle version accordingly)

for windows, you may use SET instead of export.
example: set PATH=%PATH%;...

And,

instead of using ant to build, use `gradle build`.
didnt work for me, i tried to use android studio to open the project, and did not manage to get it compiled...

Thursday, July 30, 2015

connecting to sql server with rails

sql server / sql express is by default cannot be connected by a fixed port
to login to sql server, first, assign a password for sa user,
or any user, and map the database to the user.
you may use db_owner for default role mapping.

then open sql configuration manager
select sql server netwrok configuration > Protocols for localhost / express / your instance name
select TCP/IP
scroll to bottom, and set:
all ports: 1433
dynamic port: (empty, not even a zero)
then goto control panel > services > sql server
restart service,
enable sql browser (auto start delayed) and start service

this shall help to make it work with rails or tiny_tds

Monday, July 20, 2015

mariadb 10.0 upgrade from 5.5

Upgrading to Mariadb10 seems to fail on my ubuntu 14 virtualbox.
What i did was restart the instance,
followed instruction from:
https://mariadb.com/kb/en/mariadb/upgrading-from-mariadb-55-to-mariadb-100/

and prompt to replace my my.inf configuration with maintainer configuration.
but it still failed.
tail -n 200 /var/log/syslog turn out that it was unable to bind at 127.0.0.1.

so i vi /etc/mysql/my.inf
change the binding address to 0.0.0.0

and it works :)


Tuesday, July 7, 2015

exim smarthost matching null or empty string via wildlsearch

okay, it took me a long time to finally figure how to match possible null / empty string
due to nature that sender_address_domain may be empty if its a bounce mail,
delimiting it in a string did not work, turn out ive to just create a blank {}

client_send = : ${extract{auth_name}{${lookup{${if eq{$sender_address_domain}{}{'default'}{$sender_address_domain}}}nwildlsearch{/etc/smart}{$value}fail}}} : ${extract{auth_pass}{${lookup{${if eq{$sender_address_domain}{}{'default'}{$sender_address_domain}}}nwildlsearch{/etc/smart}{$value}fail}}}

with this, it matches in /etc/smart:

default: auth_name=... auth_pass=...


notice the {} empty without any string inside


other complaints:
- for some weird reason, strlen function wont work in section: auth...