SETTING UP STAGING ON A Vagrant virtual machine

To setup staging you require.

  • Vagrant
  • Virtual Box

Vagrant allows you to spin up as many Linux virtual boxes as you want, whenever you need them. It isolates dependencies and their configuration in a single file called “Vagrantfile”. So instead of installing your dependencies one at a time you can just create a Vagrantfile and Vagrant handles the rest of the setup.

Seting up vagrant

Note: make sure that you have 1-2 GB of free RAM as Vagrant runs a full OS in the virtual machine on your system.

  1. Install Virtual Box in your machine.
  2. Install Vagrant in your machine.
  3. Pick the box that you want to spin up at vagrantcloud.com
  4. Execute vagrant box add “name_of_the_box”.
  5. Go to the project folder.
  6. Execute vagrant init.
  7. Execute vagrant up.
  8. Execute vagrant ssh.

So just like that you have a virtual Linux box running on your machine.

Configuring The Vagrantfile

This file is the heart and soul of your vagrant setup and you can use it to normalise and automate the process of setting up your virtual machine. A lot of stuff like provisioning, networking and syncing application folder can be automated directly from this file.

Setting Up The Networking:

config.vm.network "private_network", ip: "192.168.33.10"

This creates a private network. You also get options for port forwarding or connecting to public network.

Setting The Synced folders:

It enables Vagrant to sync folders on the host machine to the virtual machine.

config.vm.synced_folder "./", "/home/vagrant/app"

The first parameter is a path to a directory on the host machine. If the path is relative, it is relative to the project root. The second parameter must be an absolute path of where to share the folder within the virtual machine.

Provisioning The Virtual Machine:

Provisioner in Vagrant allows you to automatically install software, alter configurations, and more on the machines as part of the vagrant up process instead of using ssh and installing all software manually, each time.

config.vm.provision "shell", privileged: false, inline: <<-SHELL

The script will be run in the shell of your virtual machine when you execute vagrant up.


Prashant is an engineer at C42 Engineering. If you liked this post, please consider...

  • following @TheData_Ninja on twitter
  • subscribing to this blog
/

The Strategy Pattern

The standard dictionary meaning of strategy: it is a plan of action designed to achieve a specific goal/target.

If I want to reach the office I will ride my bike and reach the office, I will call it Strategy-1. But these days petrol is very expensive and I am running out of money so to tackle this problem, I can use the train to solve the problem and I will call it Strategy-2. But now I found that train takes more time or it gets late most of the time so the next way to solve this problem is using the bus to reach the office I will call it Strategy-3.

So what happens above is, I am deciding the way to reach my office depending on the current situation.

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. -Gang of Four

Let’s take a look at another example. Our grill depends upon the strategy to tell us what type of food is to be grilled. It means that at runtime we are able to decide the type of food to be grilled.

Now, let’s get the grill ready.

Now let’s get grilling! We’ll start with some sandwiches.

These sandwiches are almost done, time to throw on the burger and veggie patties.

 

We were able to switch out items without creating a new class of Grill. We  are not creating a new separate class for each new food item. That's how we avoid writing more code by using strategy pattern and combining components and writing a new class for every type of grill would become unmaintainable quickly.


Praveen is an engineer at C42 Engineering. If you liked this post, please consider...

/

Syncing with Webhooks

Data or State Syncing is a hard problem. It often appears to be the root cause of most bugs. It also has some overheads like timestamp correction and network overload. Webhooks seems like a good strategy for tackling syncing problems and its overheads. The term webhook was coined by Jeff Lindsay in 2007 from the computer programming term hook.

A webhook in web development is a method of augmenting or altering the behavior of a web page, or web application, with custom callbacks. These callbacks may be maintained, modified, and managed by third-party users and developers who may not necessarily be affiliated with the
originating website or application.
— Wikipedia

Callbacks are basic HTTP POST URLs accepting XML or JSON data. Callback maintainers can process this data any way they want to.

 Consider a scenario of syncing Orders between two applications (say P and Q). Where P is a major E-Commerce platform with millions of orders every day. And Q is a fraud detection backend where humans intervene. An Order cannot be Confirmed unless it's Verified by a human. Here the big overhead is human element of the process which is slow but necessary. In the near future this human verification will be replaced with Machine Learning algorithms once enough data is collected.

Syncing of an Order will happen as follows,

  1. An Order Request with Callback URL will be sent from P to Q.
  2. The Order Request will be processed at Q using human counterparts.
  3. Q will mark Order Request as Verified or Rejected.
  4. If Order Request is Verified, send an HTTP POST with verified data as payload to the Callback URL.
  5. If Order Request is Rejected, send an HTTP POST with rejection reason as payload to the Callback URL.

Syncing of Order may seem like a simple posting and waiting protocol. But it has some minor tweaks. You can also use any amount of magical sauce in it like dispatching callbacks in batches or using pooled async calls.

Conclusion

Webhooks were inherently designed to handle

  • Notifications
  • Real-time data synchronization
  • Process data & repost it
  • Data validation checks

In this post, we've exploited the process data and repost it design feature of webhooks. You can use webhooks for a variety of different use cases.

References


Ranjeet is an engineer at C42 Engineering. If you liked this post, please consider...

/