Mongo Native TTL

Turns out MongoDB supports a native TTL mechanism, natively removing documents at or after a certain amount of time, similarly to Redis’s auto-expiry. Here’s an example usage flow:

> db.foo.insert({time: new Date(), name: "Jones"})
> db.foo.find()
{ "_id" : ObjectId("53ce96f03fdf121aa2e36622"), "time" : ISODate("2014-07-22T16:53:04.001Z"), "name" : "Jones" }
> db.foo.ensureIndex( { "time": 1 }, { expireAfterSeconds: 5 } )
> db.foo.find()
{ "_id" : ObjectId("53ce96f03fdf121aa2e36622"), "time" : ISODate("2014-07-22T16:53:04.001Z"), "name" : "Jones" } //still there, less than 5 seconds have passed
> //wait 5 seconds
> db.foo.find()
> //empty

Mongo Awesomeness FTW. 

Advertisements

Operation Protective Edge: The Case For Israel

Here are a few issues and clarifications regarding Israel’s military and political actions during operation Protective Edge, in the form of addressing the most common accusations against Israel.

Israel is Causing a Humanitarian Crisis!

1) Even under fire, Israel is still supplying humanitarian aid to citizens of Gaza. Fuel, food, medical supplies, as well as electricity and water. Gaza is running on Israeli fuel and electricity, even as they fire rockets on Israeli civilians.

ref:
http://www.israelnationalnews.com/News/News.aspx/182689#.U8JHOo2SyYk,
http://www.israelnationalnews.com/News/News.aspx/182742#.U8JGso2SyYk

2) Gaza has an open border with Egypt (its South border). Egypt does not supply Hamas with humanitarian aid, nor does it allow Palestinians to come into Egypt.

Israeli is Attacking Civilians!

Even the most cynic of Israel’s critics should be able to realize Israel has much to lose with each civilian life – even assuming a completely selfish perspective with no care for loss of civilian life, clearly Israel loses much political credit when causing civilian lives. Wouldn’t Israel do its best to avoid this?

Israel’s Attacks Have Too Many (Mistaken) Civilian Casualties!

1) Israel pre-announces when it’s going to attack a house. This is in order to allow any civilians to flee the area. Naturally, this also allows combatants to flee the area, which is a significant military setback, especially considering Hamas combatants are always among civilian population.

The announcement is via:

  1. Leaflets in Arabic dropped in the area.
  2. A telephone call to the house prior to the attack.
  3. A “roof knocking”, a tiny, non-explosive missile to hit the roof of the house to signify the “seriousness” – this house is about to be bombed, yo.

Only after all these is a target hit.

Example: http://www.youtube.com/watch?v=v4q-_rKLPU8

2) Israel only attacks confirmed terrorist centers. Hamas (by their own admission) target civilians all the time, as is clearly evident by indescriminate rocket strikes at civilian cities, kidnapping of civilian teens, and attempted terrorist infiltrations into civilian population centers. E.g., check out Wikipedia’s list of Palestinian suicide attacks. Remember this is the list of successful suicide attacks. For each such successful attempt, Israel’s security apparatus stops ten-fold failed attempts. Also take note of the list of civilian targets – bus stops, restaurants, markets, cafes, and so on.

3) Hamas uses human shields as a matter of routine, hiding arms and combatants amongst civilian houses, mosques, schools and hospitals, as well as encouraging civilians to rush to targets which are about to be bombed (as noted above) in an effort to either stave off the attack or “die as martyrs”.

Examples of Hamas spokespeople referring to these human shields:

Gazans tell how Hamas uses them as human shields:

4) Hamas routinely hides rockets in houses and schools. If civilians homes are used as military bases, any injuries incurred by these civilians is on the fault of whomever is storing the weapons there. Hamas even uses ambulances as part of the terror arsenal:

Civilian deaths in Gaza are the fault of Hamas. All casualties would cease immediately if Hamas stops its fire.

Israeli Response is Disproportionate!

1) Israel’s response is more moderate than, arguably, any other modern western country. How would the US react if thousands of rockets were being fired at NYC and DC? How would Russia react if missiles were being fired at Moscow? Israel has offered multiple times “quiet for quiet”, which has been rejected by Hamas.

Missiles have been fired at Israel from Gaza continuously ever since Israel withdrew (unilaterally) in 2005 from the Gaza strip. Hamas is now firing missiles How would your country react to a decade of being continuously bombed? How is Israel supposed to show restraint when Hamas is firing rockets at Israel, not even making any demands other than calling for the destruction of Israel?

2) Gazan death toll is higher (and any civilian death is a tragedy, to be sure) than Israels only for lack of Hamas ability. Hamas clearly state that were it up to them, they would kill any Jewish man, woman and child in Israel – it is not for lack of wanting that they (usually) fail to do so. Israelis run for shelter upon hearing sirens, rather than running for the roof to act as a human shield.

Israel also has far better defensive and offensive infrastructure because Israel uses its time and money building a country rather than perpetrating Jihad on its neighbors. Any country which wishes to have nonviolent peace with Israel can receive quiet from the Israeli side. Towards this end Israel has made peace with Jordan, peace with Egypt (including giving back the Sinai peninsula, which was 2/3 of Israel’s land at the time), and withdrew from the Gaza strip unilaterally.

  • Hamas charter excerpts: http://fas.org/irp/world/para/docs/880818a.htm, article 13: ‘[Peace] initiatives, and so-called peaceful solutions and international conferences are in contradiction to the principles of the Islamic Resistance Movement’

Israel has been under Islamic militant fire for 66 years, often close to the point of obliteration. This fire has ALWAYS included civilian targets, and – by open admission of parties such as Hamas, Syria and Hizballah – will not stop until the Jewish people are eradicated from Israel. Considering that despite all the above Israel still makes every effort to avoid civilian casualties, this is clearly not a disproportionate response.

It’s Israel fault, because…

…of the Blockade On Gaza! The point of the blockade is to prevent Hamas from building arms and rockets, which it will use to bomb Israeli civilian cities, as it indeed is doing. This conflict seems to prove the blockade is quite justified. Humanitarian supplies are being supplied to Gaza by Israel itself, and can be supplied via the Egyptian border as well (see above, but they’re not).

…Israel isn’t making peace with the PLO! When Israel withdrew in 2005 from Gaza, it withdrew unilaterally, freeing the Palestinian people to do whatever they wanted. What they did was to promptly elect Hamas and immediately begin firing on Israeli civilian settlements. It’s been a decade of that, now, no matter what happens in the Israeli leadership or policy. (As mentioned, armed resistance is the proclaimed slogan directive of Hamas – they will fight Israel until they banish all the Jews from Israel.)

Israel is understandably “reluctant” to allow a similar situation in the West Bank (which is much larger and even closer to Israeli main cities). More importantly, Hamas doesn’t care about any such peace agreement and explicity renounce any peace dealing with Israel. As their charter says explicitly, “There is no solution for the Palestinian problem except by Jihad”

…of the settlements! Well, there’s always something. Israel was under attack before any (post-1967) settlements existed – including terrorist attacks against civilian populations – and even before 1948. Even the worst critics of the settlements cannot justify attacking civilian population with rockets. Even Hamas themselves proudly proclaim that nothing bar a complete removal of all Jews from Israel will stop them.

Why doesn’t Israel trust the United Nations?

1) Historically, the United Nations has always acted to restrain Israel, while never acting to restrain anti-Israeli forces. This was observed in the wars of 1967 & 1973 (when Israel was on the verge of destruction) as well as the Lebanon wars of 1982 and 2006. In all the above cases the UN failed to prevent attacks against Israel, opting instead to leave the area when the conflict began.

2) UNRWA is the ONLY UN organization for refugee assistance that exists towards a SPECIFIC group of people. Its goal is not the expected assistance of Palestinians, but explicitly making them return to Israel. To repeat: these “refugees” have been in “refugee” status for 50 or 65 years (depending on who you ask), and are still living in refugee camps, instead of being resettled elsewhere. Despite the fact that Jewish refugees from Northern Africa, Middle Asia, and all of Europe fled at the same time – no UN agency has been formed to help the Jews, but one exists for the Palestinians. These Palestinians could have long been resettled if that was the objective, rather than an anti-Israeli objective.

UNRWA funds schools which brain-wash education of Gazan kids to hate Israel, and even have Hamas members on their payroll.

3) The United Nations “Human Rights Council” has a comical anti-Israel basis. Despite having countries such as Sudan, Syria and Iran in its body, its council has (per Wikipedia):

  • as of 2014, condemned Israel in 50 resolutions – more than the rest of the world combined
  • The council voted on 30 June 2006 to make a review of alleged human rights abuses by Israel a permanent feature of every council session
  • Their self-proclaimed mandate is to “investigate human rights violations by Israel only, not by Palestinians.”

Beyond critical, this bias seems simply ridiculous. Well at least this is admitted by the UN Secretary-General, Ban Ki-moon.

4) Iranian officials proudly support terrorism against Israeli civilians and development of nuclear weapons while they proclaim they will “destroy Israel”. The UN’s response has been meek.

The international community has never supported Israel, and it should be no surprise it does not support it now, despite valiant efforts to show restraint and address humanitarian concerns while its cities are (again/still) under fire.

But, but…

Hamas is the democratically-elected leadership of the Gaza strip. It is up to the Gazan people (and/or the international community) to restrain the unilateral attack on Israel and substitute it with a quiet that will be met by Israel, as always, with more quiet.

Israel does not want “to kill all the Arabs”; Hamas DOES want “to kill all the Jews”.

Israel observes human rights, Hamas does not (including the Red Cross Charter).

Israel is a western, modern country trying to allow its citizens a normal life while demonstrating remarkable military restraint in order to preserve human rights.

Israel sanctifies life, Hamas sanctifies death.

Israel is doing the best it can to keep on living while minimizing civilian casualties and observing human rights — the world should be supporting it, not bashing it. Despite being under rocket fire from Gaza, Israel is still the only entity in the world supplying humanitarian aid to Gaza, and Gaza — as we speak — is running on food, fuel, and water coming in from Israel.

The international media coverage of the current conflict is appalling. It forces Israel into a situation where they cannot trust anyone but themselves for defense or support. The hatred is disgusting, and the onus and blame of civilian deaths is on Hamas and on the international community for not reigning it in. Civilian deaths are terrible and they are the fault of Hamas.

If it were up to Israel, peace would be achieved immediately.

Ideas

Here’s a list of some web-app ideas I’ve been thinking about. 

1. Daat – Quality-controlled free-form Q&A site(s).

Quora has proved that a market exists for such a site, and has convinced its investors to the tune of a $1bn. They have not yet captured the whole market, though, and most notably they remain largely invisible and uninteresting to anyone whose first language is not English. 

2. NKH (No Kupat-Holim) – An online doctor’s examination 

Much medical interaction consists of formatted informational q&a / flow-charts which is the forte of online web applications. This has naturally led to people resorting to using the web to diagnose themselves, but no clear leader has emerged to replace the tier-0 functionality of forums and “googling your symptoms”. A dedicated website, allowing you to adequately identify specific symptoms as well as browse others’ anonymous diagnoses and/or a premium live response from paid doctors would be great. 

It’s one of those things that you just know are going to happen sometime – why sit in line to talk to someone about why your stomach hurts, when you can ask every doctor in the world? 

3. Micropayments Portal – an easy way to pay small amounts online. 

Most people won’t pay for stuff online because of a combination of:

  1. It’s a hassle. 
  2. You can usually get whatever you wanted for free instead of paying for it.

But it doesn’t have to be that way. If you could hassle-free pay very small amounts (say, less than 1$) people would be much more inclined to use such payments. These payments could be used anywhere – to read articles, donate money, in-(web)-app purchases…


If any of these interests you, drop me a line: sella.rafaeli@gmail.com. 

I believe that …

I believe that every time a developer implements infinite scrolling, a kitten orphanage catches fire, everyone dies, and the world becomes a worse place.

(src: http://blog.makandra.com/2011/11/i-hate-infinite-scrolling/)

Haha – well I actually think infinite scrolling is an amazing good (or should I say, basic) UX. But I enjoy generic bitching and the ‘every time X, something horrible’ meme. 

Side note – ‘infinite’ is one of the harder words to type. 

Mongo Console BI syntax essentials

When messing around with the Mongo console I’ve found that Mongo is not just JavaScript, and the JS-foo and standard APIs you might be used to in the browser or in Node are not always available – where’s console.log, for Console’s sake?

Here’s a head start on how to perform some basic manipulations, assuming prior familiarity with JavaScript and Mongo themselves.

So suppose:

  1. You’ve got your data spread around mutiple collections (supposed they’re named ’employees1′, ’employees1′, ’employees1′),
  2. You want to query all the fields of a nested document (say, the field ‘city’ of nested document ‘address’, which not all the users have).

You might want something like this:

mongo > res = []; 

mongo > for(i=0;i<4;i++){ res=res.concat(db.getCollection('coll'+i).find({"address": {$exists: true}}).toArray()); }

mongo > res.length //number of employees that have an 'address' sub-document.

mongo > res.forEach(function(x){print(x.address.city)}) //print out all the cities

Hooray for science.

Node Background Processing Awesomeness

Unless I’m misunderstanding something, a Node.js callback can replace a full system architecture of handling asynch background processing.

Suppose you have a webapp route that initiates a heavy background task: POST ‘/do_heavy_lifting’

You don’t want your users to wait forever while you process; that’s sucky UX and blocks your app’s precious resources. So, you might drop an event down to some messaging queue (say, Rabbit), and write up a worker to listen to that queue and then perform the heavy lifting in the background. Now you’ve got to maintain the worker, the queue, the event, and synchronize all the different parts together… This is only an issue once you reach a certain scale, but then — it’s an issue, once you reach a certain scale.

However, there is a better way.

As you surely know, you can use Node.js’s callback awesomeness to perform heavy background processing (codename “I/O”) without blocking the code execution flow. However, if you respond to the request without waiting for the I/O to finish, you can even respond to the user – thus freeing the connection – while allowing the background processing to finish in its own time!

This allows you to handle arbitrarily heavy background tasks while immediately freeing the response, both clearing the HTTP connection and responding immediately to the client. Holy smokes!

Sample code, using writing content to a file as an example of heavy lifting (observe the response.end outside the callback):

// Require what we need
var http = require('http');
var fs = require('fs');

function now(){
    var d = new Date(); 
    return d.getHours()+d.getMinutes()+':'+d.getSeconds()+'.'+d.getMilliseconds();
}

var app = http.createServer(function(request, response) {
  response.writeHead(200, { 'Content-Type': 'text/plain'});

  fs.writeFile('baz', 'baz contents: '+now(), function(err) {
    console.log('created file. Now running arbitary background code');            
    console.log('before Second Reponse:'+now());
    response.end('Redundant response: '+now()+'\n'); 
  }); 

  console.log('before First Response :'+now());
  response.end('First Response : '+now()+'\n');
});

app.listen(1337, 'localhost');
console.log('Server running at http://localhost:1337/');

Copy the above into foo.js, $ node foo.js, and (separately) $ curl localhost:1337. Notice:

  1. You will always get ‘First Response’, which is expected.
  2. The ‘redundant response’ is never used. In fact we can comment it out; same behavior. (It’s redundant in our case because it’s a background operation, and we don’t need it to finish in order to respond to the user.)
  3. The I/O operation does get executed, as well as its callback (which can be arbitrarily extended).
  4. Under ‘arbitrary background code’, we can now execute whatever we want — the client has already receiving its response.

You have just implemented a full-blown background asynch processing paradigm, with about a line and a half of code and some Node.js-Fu.

Copy Local Files into Remote Server through n>1 SSH Hops

Copy your local files into a remote environment with an arbitrary number of hops using the following:

$ rsync -have "ssh -A -t user@middleman ssh -A -t user@destination" source_path :target_path

As in, copy your local directory ‘foo’ to the root path of your staging server using:

$ rsync -have "ssh -A -t user@middleman ssh -A -t user@destination" foo :

Notes:

  1. If you are copying a directory, remember the target path should be the target directory’s parent folder.
  2. If you are using this (‘hot-deploying’) instead of a traditional ‘deploy’ from a version-controlled repository, exercise the appropriate caution: verify your files have been copied to the correct location, remember they haven’t necessarily been source-controlled, and so on.
  3. I learned this from: http://mjbright.blogspot.co.il/2012/09/using-rsync-over-multi-hop-ssh.html

Hooray for Science!