Uncategorized

Docker basic commands

docker run [-d runs on background deamon, -p expose_port:docker_port , –name ]
docker start
docker stop
docker ps [-a include stopped containers]
docker rm
docker build -t
docker push

Examples:

docker run -p 2181:2181 -p 9092:9092 –env ADVERTISED_HOST=localhost –env ADVERTISED_PORT=9092 spotify/kafka
docker start kafka
docker stop kafka
docker rm kafka
docker build -t vlknbcr/test . #. represents I am in the folder where docker file it is.
docker push vlknbcr/test

Windows Service Operations

Create windows service
sc create <service_name> binpath= "<path_to_binary_executable>" [option1] [option2] [optionN]

Ex:
sc create Test binPath= "C:\Program Files\Aspera\Enterprise Server\bin\Debug\test.exe" DisplayName= "Test Central" start= auto

Delete windows service
sc delete <service_name>

Query windows service configuration:
sc qc <service_name>

Change configuration
sc config<service_name> binpath= “<path_to_binary_executable>”

Chek service status
sc query <service_name>

Latitude/Longitude Distance Calculation in SQL Server

Here’s the SQL function:

CREATE FUNCTION CoordinateDistanceMiles(
@Latitude1 float,
@Longitude1 float,
@Latitude2 float,
@Longitude2 float
)
RETURNS float 
AS  
BEGIN 
-- CONSTANTS
DECLARE @EarthRadiusInMiles float;
SET @EarthRadiusInMiles = 3963.1
DECLARE @PI  float;
SET @PI = PI();
-- RADIANS conversion
DECLARE @lat1Radians float;
DECLARE @long1Radians float;
DECLARE @lat2Radians float;
DECLARE @long2Radians float;
SET @lat1Radians = @Latitude1 * @PI / 180;
SET @long1Radians = @Longitude1 * @PI / 180;
SET @lat2Radians = @Latitude2 * @PI / 180;
SET @long2Radians = @Longitude2 * @PI / 180;
RETURN Acos(
Cos(@lat1Radians) * Cos(@long1Radians) * Cos(@lat2Radians) * Cos(@long2Radians) + 
Cos(@lat1Radians) * Sin(@long1Radians) * Cos(@lat2Radians) * Sin(@long2Radians) + 
Sin(@lat1Radians) * Sin(@lat2Radians)
) * @EarthRadiusInMiles;
END

Here is how to use it.

SELECT * FROM Hotels
WHERE dbo.CoordinateDistanceMiles(Latitude, Longitude, @ZipLatitude, @ZipLongitude)  @Radius 
--@Radius is miles.

How to manage unhandled exception

Here is a small console application ;


using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace ConsoleApplication
{
    public static class Program
    {
    	static void Main()
    	{
    		AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException;

    		Application.ThreadException += Application_ThreadException;
    		Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

    		Application.Run(new TestForm());

    		throw new Exception("Main");
    	}

    	static void Application_ThreadException(Object sender, ThreadExceptionEventArgs e)
    	{
    		MessageBox.Show(e.Exception.Message, "Application.ThreadException");
    	}

    	static void AppDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e)
    	{
    		MessageBox.Show(((Exception)e.ExceptionObject).Message, "AppDomain.UnhandledException");
    	}
    }

    public class TestForm : Form
    {
    	public TestForm()
    	{
    		this.Text = "Test Application";
    		this.ClientSize = new Size(200, 60);
    		this.MinimumSize = this.Size;
    		this.MaximumSize = this.Size;
    		this.StartPosition = FormStartPosition.CenterScreen;

    		Button btnThrowException = new Button();

    		btnThrowException.Text = "Throw";
    		btnThrowException.Location = new Point(0, 0);
    		btnThrowException.Size = new Size(200, 30);
    		btnThrowException.Click += (s, e) => { throw new Exception("Throw"); };

    		Button btnThrowExceptionOnOtherThread = new Button();

    		btnThrowExceptionOnOtherThread.Text = "Throw on other thread";
    		btnThrowExceptionOnOtherThread.Location = new Point(0, 30);
    		btnThrowExceptionOnOtherThread.Size = new Size(200, 30);
    		btnThrowExceptionOnOtherThread.Click += (s, e) => new Thread(() => { throw new Exception("Other thread"); }).Start();

    		this.Controls.Add(btnThrowException);
    		this.Controls.Add(btnThrowExceptionOnOtherThread);
    	}
    }
}