Author Archives: Ying Kit Yuen

About Ying Kit Yuen

Life is for fun~ =)

Meteor – High Network IO as it keeps polling MongoDB

I was working on a Meteor project which is connected to a mongoDB hosted on MongoLab. Since it is a sandbox plan, the oplog of the mongoDB instance is not available and in that case the Meteor will keep polling the mongoDB in around every 10 seconds. This causes a very high consumption on CPU and network bandwidth.

If you are running, Meteor 0.7.0 or above and your mongoDB has oplog enabled. You won’t experienced the problem.

Since the oplog is not available so the only workaround which i could think of is to unsubscribe the dataset. Here is a main.js which is executed when the app is loaded and when the user is idle for than 3 mins. Stop the subscription and that will stop polling the mongoDB.
Continue reading

Javascript – Check if user is idle

Nowadays, websites are becoming more advanced with many different features. Sometimes it would be beneficial if we could switch off them on the client browser when the user is idle for a period of time in order to reduce the web server loading.

Here is a Javascript example which would pop up an alert box if the mouse doesn’t move for 3 mins.

$(document).ready(function () {
  // Set timer to check if user is idle
  var idleTimer;
  $(this).mousemove(function(e){
    // clear prior timeout, if any
    window.clearTimeout(idleTimer);

    // create new timeout (3 mins)
    idleTimer = window.setTimeout(isIdle, 180000);
  });

  function isIdle() {
    alert("3 mins have passed without moving the mouse.");
  }
});

 

Done =)

Reference: StackOverflow – Detecting idle time in JavaScript elegantly

陶傑 – 日本人看文化

日本首相安倍出席俄國冬季奧運開幕禮,順道會見普京,普京牽着兩年前安倍贈送的一隻日本柴犬來迎客。安倍摸摸柴犬的頭,說:小狗好乖。普京說,偶爾也會咬人。

兩位領袖,話中有話,是高手過招,安倍向俄國送狗,是因為知道普京喜歡狗。日本柴犬忠誠,也有獨立的意志,跟北極犬,即所謂Huskies一樣,不太容易養。安倍將狗送給愛狗的國家領袖,狗不會送錯去北京或首爾,因為這兩個國家都吃狗肉。像清末英女皇送給李鴻章的一隻哈叭狗,英國人再探望時,李鴻章道謝,說「那隻狗味道很好」。

國際往來,對於他國的民族文化,要有很深的了解。島國日本在這方面的情報研究做足,尤其對於一海之隔的中國。

日本十九世紀末有一個學者高桑駒吉,曾著有「支那文化史」,講述中國唐宋兩代的文化差異。唐宋的文學,因兩代風氣不同,唐朝氣象渾厚,宋朝氣質偏狹,以詩歌來比較,高桑駒吉認為:唐人以詩歌抒達其「情」,宋詩以議論發揮其「智」。唐朝的詩人,以杜甫李白為首,都屬於在政治上不得意的人,覺得懷才不遇,唐詩偏向「啼饑號寒,長歌當哭」。但是宋詩不同,王安石、寇準、辛棄疾,甚至蘇東坡,都深得宋朝趙姓皇帝的重用,日子過得富裕,知識份子得志於廟堂,於是有雍容勝利的風度。
Continue reading

陶傑 – 南北中國

不但普通話和粵語天下本無事,但由於政治挑釁,漸成一場「南北戰爭」,在文化上,早有南北兩個中國。

梁啟超在「中國地理大勢論」說:「長城飲馬,河梁攜手,北人之氣概;江南草長,洞庭始波,南人之情懷。」

北方人豪邁,故言「氣概」;南方人婉柔,故曰「情懷」。中國的文化處處體現南北之差異:共產黨盤踞的先秦陝北,打仗用人海戰術,手段粗狠;國民黨主控的吳越江浙,國軍在徐蚌會戰中,看見戰場前線都是驅趕來做炮灰的百姓,不忍開槍。中國文化的粗獷,戰勝了婉柔。

秦始皇統一六國,是北方中國的勝利。春秋的吳越,戰國的齊楚,文化比北方的秦國精緻,商貿比秦國發達。但是劣幣驅逐良幣,精緻而有修養的文化南國,最終被暴力而好戰的北國吞滅。
Continue reading

陶傑 – 年初四

年初四,小時候香港的童謠:「一二三四馬騮放臭屁,放到年初四。唔係我,就係你。」因此,總以為「年初四」的意義,只為了這首作弄人的兒歌而設。

但是年初四為「羊日」,初七方為「人日」。中國農曆新年為千百年農業社會的生活速度而設。從前的一切,地久天長,驢子拽着石磨,牛拉着耕犂,從這山莊到那村店走三十里路,連農曆新年,每一天都配給一種生畜做生日:雞狗豬羊之後,才輪到牛、馬、人。

過中國新年,就知道中國文化不可以「現代化」。譬如高鐵一連接,將二十一世紀的高速度帶進來,中國的習俗即刻有一大片癱亡。北方的農村,過年要過一個月,如果這樣叫做「不合時宜」,因為現代社會要講求效率,中國人的工商業城市,不可能有這許多天假期,那麼農曆新年的習俗,一天挨一天的淘汰掉。

中國文化不講「快」,一切只求「慢」。廣東的老火湯要熬六七個鐘頭,紅樓夢要看足八十回,中國功夫要學紮馬,同理,農曆新年要過全套,中國文化的意識,隨同那一身藍棉襖、一地紅色的爆竹紙屑,還有一地的皚雪,才會留在腦海。
Continue reading

陶傑 – 抱罐子的人

北京上海嚴重污染,大陸網民還發揮所謂「幽默」,聲稱至少北京的空氣污染,「人人平等」,意思是連中南海裏的「領導人」,跟在天安門廣場下跪哀求的中國農民一樣,毒霧無分貴賤,一齊污染,呼吸的空氣是一樣的。

大陸網民不可以盲目樂觀。中共政治局委員,雖然也住在北京,但中國式的特權,不是賤民可以想像,有一天,北京的官員,可以每人配備一具氧氣筒,口戴氧氣罩,一樣可以在北京四處走動,包括進食店吃包子。

像潛水蛙人揹的那種,氧氣筒多半是金屬造的,重幾十公斤,當然不必領導人揹在背上,由「秘書」或者隨從抱着,連一條塑膠管子,領導人戴着氧氣罩,秘書助理也抱着氧氣筒,寸步不離,跟在身邊。

有人會問:那麼抱氧氣筒的秘書隨從呢?他們也要戴口罩吸氧氣的吧。他們的那具氧氣筒,如果又由另一個人揹着跟隨,那麼一個鏈接一個,豈不無窮無盡,領導人身邊要跟着幾多人?
Continue reading

Puppet – RPM Package ensure => installed failure

In Puppet, the source attribute in package type allow us to install a .rpm package by providing the file path located in the agent machine. But in that case, the package name should be the same as the .rpm filename. Otherwise, you will get following error.

package oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64 is already installed

 

Here is an example of the Puppet manifest.

#package { "oracle-instantclient": <-- this will cause error in every daemon run
package { "oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64":
  ensure   => installed,
  provider => 'rpm',
  source   => '/tmp/oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm',
  require  => File["/tmp/oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.rpm"],
}

 

Done =)

Reference: Esure package installed fails if it is installed

Perl – Setting locale failed

I tried to install Python using pyenv but came across the following error:

vagrant@vagrant-ubuntu-trusty-64:~$ pyenv install 2.7.8
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LC_CTYPE = "UTF-8",
	LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

 

For those which are unset, set the environmental variables in .bashrc or .bash_profile.

export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

 

Re-login and try again.

Done =)

Reference: perl: warning: Please check that your locale settings ubuntu

Windows – Install Python packages

Seems that it is very challenging to set up the Python environment properly on Windows. Python 3.4 ships with pip but if you want to install a older version of Python, there is not simple way for the package management.

If you get stuck on installing Python packages on your Windows machine, you could consider using the unofficial Python package binaries prepared by Christoph Gohlke.

Done =)