Wednesday, December 19, 2012

Possible reasons that your apn_on_rails may not work

There were several times that user reported that the Push Notification not working. That's always my first response: "It should be working. No way it is not working. I just fixed it few days ago, and it was working !"

I found the need to share all the errors I saw, and also as a checklist reference for my future projects.

1. I was using the wrong fork of http://github.com/XXX/apn_on_rails 

  There are bugs in the master github repository PRX/apn_on_rails. And in some forks, you should put the .pem file path as the configatron.apn.cert, while in some other branches, configatron.apn.cert is actually asking for file content:

    configatron.apn.cert = File.join(Rails.root, 'config', 'certs', 'apn_dev.pem')
    configatron.apn.cert = File.read(File.join(Rails.root, 'config', 'certs', 'apn_dev.pem'))

  There are some more other fixes loosely exist in some forks and I have no idea which one is the best, so ... I forked one more for myself. https://github.com/3dd13/apn_on_rails


2. apn push token I collected from 2 mobile apps are the same

I was setting up two mobile apps push notification by one backend server, and user is using both mobile apps. I knew that I had to use two different .pem cert file to push to the two different apps, but sometimes one of them was not working and sometimes it pushed messages to the wrong mobile apps.

After tracing all the code, finally, I found out that the apn_token from the 2 mobile apps I collected are actually the same. And the APN::Device instance did not create with the correct app:

    device = APN::Device.find_or_create_by_token(apn_token)

so this is my fix:

    device = APN::Device.find_or_create_by_token_and_app_id(self.apn_token, apn_app.id)


3. I submitted the apn token with incorrect format

I solved this issue few months ago, but recently after I rewrite some code and this bug dragged me crazy ... once again.

the apn push token you collected from mobile is in this format:

    00ea74e76a873e8e9c14c2dd2afe3b42abb35148e94042811e2b6985072641f2

but actually, apn_on_rails is expecting this:

    00ea74e7 6a873e8e 9c14c2dd 2afe3b42 abb35148 e9404281 1e2b6985 072641f2

I wrote a method to convert that:

4. I setup the staging with production push cert with sandbox apn host

One of my early chore was setting up apn push cert, so to do it quick (and dirty), I was using development .pem cert file on both my dev machine and so-called production. But later when I was switching the production cert to the real production cert, It did not work.

At that moment of time, what drove me crazy was that no error was thrown and the "sent_at" attribute of the APN::Notification is correctly updated.

So, we keep checking everything again and again, generating certs for several times, restarting servers, clearing up database records. AND ... the real bug is actually the incorrect configatron.apn.host

gateway.push.apple.com should be used for production, and gateway.sandbox.push.apple.com should be used for development


5. Used production for staging (on heroku) Procfile

Push messages are sent out in worker threads (by sidekiq), and workers are started using Procfile:

    worker: bundle exec sidekiq -e production -C config/sidekiq.yml

obviously, this Procfile won't work for staging or development machine, the correct one should be:

    worker: bundle exec sidekiq -e $RACK_ENV -C config/sidekiq.yml


6. Errno::ENAMETOOLONG File name too long

There were something like that in the log

    Errno::ENAMETOOLONG
    WARN: File name too long - Bag Attributes

actually, this is not an error, just a warning. However, it fills up my logentries page which makes it hard to debug.

This one was caused by some strange bug (perhaps not a bug, but I don't understand why). It is related to the configatron.apn.cert path issue I was talking about.

In the same function, configatron.apn.cert was treated as both path and file content. If you want to know the details, check out the commit I made to fix it:
https://github.com/3dd13/apn_on_rails/commit/0b60b26b38540d0c59a6cd21068d6b2f9a6cf139


Tuesday, November 27, 2012

Deploying single ruby script to heroku


I need a simple ping service which notifies me (via email) when my web application is down. I can write a simple ruby script to achieve that without much problem.


However, I also want this script to be run regularly and hosted on the internet for FREE. the following is my attempt on using Heroku and Heroku's Scheduler add-on.
(Heroku's scheduler can run every 10mins, 1 hour or 1 day)


only 2 files are needed: health_check.rb and Gemfile.
Source Code:
https://gist.github.com/4153156

Directory Structure:


OK, actually 3 files.
- health_check.rb is a ruby script which calls Net::HTTP and sends email using Net::SMTP
- Gemfile is an file which tells Heroku that this is a ruby application
- Gemfile.lock is the file being generated after running bundle install

that's it. go ahead and push it to heroku:
git push heroku master


and then add Heroku Scheduler to your project.
you can do it by logging in to heroku.com or via command line


add a scheduler job with ruby command line:


DONE.




Tuesday, August 21, 2012

invalid DATABASE_URL error when I deploy padrino + mongoid to heroku

Problem


I deployed my padrino apps to heroku but could not be started. and I saw "invalid DATABASE_URL" when I run "heroku run padrino console"

the exception was thrown at this line

Padrino.before_load do
  Mongoid.load!(File.join(Padrino.root,"config/database.yml"))
end

Troubleshooting


I suspected that something wrong with my database configuration file, so I double checked all the configuration files, everything's fine. All the env and (list by running 'heroku config') looks fine. Then I tried run the remote console and try to repeat the problem by requiring and executing the Mongoid methods directly.


Real Cause


I found out that the program keep reading from database.yml and the content was totally different from my database.yml. It was something like erb script reconstructing the login credential for a PostgreSQL connection.
It turned out that, although the postgreSQL shared database add-on was no longer activated on heroku be default, heroku still tried to generate the config/database.yml which OVERWRITTEN my config/database.yml.

Solution


simple.
just rename my database.yml to mongoid.yml and then change the Mongoid.load! to

Mongoid.load!(File.join(Padrino.root,"config/mongoid.yml"))

Wednesday, July 11, 2012

Keyboard shortcuts for Android emulator (for Windows and Mac keyboard)

We are working on several Android projects recently, that's why we have to interact with emulator a lot.

Here is a handy keyboard shortcut list to perform Android phone physical button actions (i.e. Back and Home button):
http://www.shortcutworld.com/en/win/Android-Emulator.html


if you are using Mac magic keyboard, or Mac book pro, you may wonder where is the "Home" button on your keyboard. It is actually ... "Command + Fn + Left"

Thursday, June 14, 2012

Codeaholics June 2012 Meetup: .Net on MVC4, Scala on Play and Ruby Queue

.Net and MVC4

yesterday Thomas shared about MVC4 (beta) at Codeaholics
he did a demonstration on generating a new CRUD web application and a set of HTTP web API.

it was nice !
it just copied most of the tools/design from Ruby On Rails and the IDE is even better than those for Rails.
although the controller code is still too verbose
and the community to build library (gems) is not as active as Ruby one

but it is really a good move for Microsoft developers (most of them get stuck in the big enterprisy company, learning nothing new except politics) to know more about the best practices to develop Web application

Play on Scala

Another funny thing was that ... Jacob was sharing about Play and the code base turned out to be so similar to those written by Thomas !
yes ! the syntax of Scala on Play and .Net on MVC4 was so similar

It is good !

Both framework picked a lots of popular features/design from Ruby On Rails and it is good ! more and more best practices being found and recognized by all the coding community. It makes software better and coders are easier to pick up another tech stack.

Thanks all the speakers !
special thanks to Jacob who was just visiting Hong Kong and spent some time sharing coding things with our local community !

Sunday, April 1, 2012

Segmentation Fault when you run ruby / rake / rails / gem install commands

this post is based on
http://www.christopherirish.com/2011/09/02/ruby-1-9-2-segmentation-fault-and-openssl/


every ruby developer must have seen "Segmentation fault" when they run rake tasks, install gems or updating gems. The usual reason is because of the depending libraries are not updated or using the wrong one during the setup of ruby runtime.


very often it is because of openssl or iconv. To fix that error, you have to re-install your ruby runtime. If you are using rvm, these are the commands to uninstall and reinstall everything with correct libraries:


rvm remove 1.9.3
rvm pkg install iconv
rvm pkg install openssl
rvm install ruby-1.9.3 --with-openssl-dir=$rvm_path/usr --with-iconv-dir=$rvm_path/usr

Thursday, March 15, 2012

Compile Coffeescript to Javascript

Many friends not familiar with coffeescript yet, but Rails 3.2 is already generating coffee script by default when you do scaffold. So far it is OK for them to achieve something by "copy and paste" from internet. However, it is hard for them to modify the script they got.


So, one way is ... use Javascript by changing the extension from .js.coffee to .js. But human has to learn, right? Here is the second way: learn by comparing coffee script and javascript implemtation.


Compile CoffeeScript to Javascript inside Aptana Studio

[Commands] -> [CoffeeScript] -> [Run] -> [Compile and Display JS]

a corresponding .js file will be generated and then you can see how each line of coffee script is compiled into javascript.



if there is error saying coffee command not found, just go ahead and install coffee-script library for your OS.

e.g. in MacOSX, brew install coffee-script


Wednesday, February 15, 2012

Internship Program to recruit Hong Kong university student

Here I consolidate all the website and resources for recruiting internship. Hopefully, it lower your resistance to recruit internship students.

(It is really frustrated to find out the correct form to fill in in all these not user-friendly website. Most of them are not SEO-friendly. Some departments have their own internship webpage too.)


Every university will ask for your Business Registration document, so, prepare a soft copy.



Joint U


There is one online job posting system which is supposed to reach all the students. However, not many students go there and keep an eye on the job there.

http://www.jijis.org.hk/

HKUST


  1. complete this form: Form.doc
  2. email it to sails@ust.hk

Career Center

contact Irene Tsui:(852)23586695


HKU


post your job here: Employer Corner

you can also see some students' resume here: Net Match


Cedars

CU


Register and post your job here: OSA


Recruitment Service

Email: cpdc@cuhk.edu.hk

Contact: 39437202


PolyU


fill in the online form here: Online Form



Poly U Recruit

Email: Poly U Career

Contact: 27666789


BU


register an account and post job here: BU Career



Email: bucareer@hkbu.edu.hk

Contact:3411 7440


City U


  1. Fill in the form:
    Recruit Form
  2. Email Business Registration Certificate BR to CAIO
  3. Complete your email request by specifying subject title:
    Summer Jobs Advertisement -- [your company name]



Providing Internship


LingNan


Register and post the job here: For Employer

you can search for CV inside the system too



For Employers

Email: career@LN.edu.hk

Tel: 2616-7406 / 2616-7446

Fax: 2891-4516 / 2891-9603