Skip to content

Instantly share code, notes, and snippets.

View melvinlee's full-sized avatar

melvinlee melvinlee

  • Singapore
View GitHub Profile
@melvinlee
melvinlee / findLongestEvenWord.js
Last active April 7, 2018 04:05
Given a sentence, find the 1st longest word and the word's length must be in even number.
const findLongestEvenWord = sentence => {
let array = sentence.split(/\s+/);
return array.filter(x => x.length % 2 === 0).sort((a , b) => b.length - a.length )[0];
}
console.log(findLongestEvenWord("The code is self explanatory , I have used the put method"));
console.log(findLongestEvenWord("Once created , you can click on Test and send a JSON as below"));
@melvinlee
melvinlee / CodeDeploy.ps1
Last active October 4, 2017 06:05
CodeDeploy Poweshell Template
# CodeDeploy powershell template
# Are you running in 32-bit mode?
# (\SysWOW64\ = 32-bit mode)
if ($PSHOME -like "*SysWOW64*")
{
Write-Warning "Restarting this script under 64-bit Windows PowerShell."
@melvinlee
melvinlee / GenericRepository.cs
Last active September 22, 2017 02:25
EF Repository
using System;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
public class GenericRepository<T> : IRepository<T>, IDisposable where T : class
{
public GenericRepository(IDbContext context)
{
@melvinlee
melvinlee / LongRunningTask.cs
Created June 6, 2017 09:33
Long running task with cancellation tocken
var cts = new CancellationTokenSource();
var token = cts.Token;
Task.Factory.StartNew(() =>
{
while (true)
{
if (cts.IsCancellationRequested)
{
return;
@melvinlee
melvinlee / ec2_autostart_stop.py
Created April 20, 2017 05:33
Lambda Autostart and autostop EC2 instance
import boto3
ec2 = boto3.resource('ec2')
print('Loading function')
def lambda_handler(event, context):
instance = list_autostop_instance()
stop_instances(instance)
def find_name(instance):
@melvinlee
melvinlee / WMF5Latest.ps1
Created April 13, 2017 07:19 — forked from mgreenegit/WMF5Latest.ps1
Get latest WMF5 and quietly install, reboot suppressed
# Use shortcode to find latest TechNet download site
$confirmationPage = 'http://www.microsoft.com/en-us/download/' + $((invoke-webrequest 'http://aka.ms/wmf5latest' -UseBasicParsing).links | ? Class -eq 'mscom-link download-button dl' | % href)
# Parse confirmation page and look for URL to file
$directURL = (invoke-webrequest $confirmationPage -UseBasicParsing).Links | ? Class -eq 'mscom-link' | ? href -match 'Win8.1AndW2K12R2-KB3134758-x64.msu' | % href | select -first 1
# Download file to local
$download = invoke-webrequest $directURL -OutFile $env:Temp\wmf5latest.msu
# Install quietly with no reboot
if (test-path $env:Temp\wmf5latest.msu) {
start -wait $env:Temp\wmf5latest.msu -argumentlist '/quiet /norestart'
}
@melvinlee
melvinlee / gist:cdc65e1119ac6ced3b54c5c5704d2bb4
Created March 6, 2017 09:30
SQL Server Transactional Replication Error: Could not find stored procedure error and how to recover
--We will assume that my_table table is in my_published_db and it is part of my_publication.
USE my_published_db
GO
EXEC sp_scriptpublicationcustomprocs @publication='my_publication'
-- Remove database from Availability Group:
Alter Database [StackExchange.Bicycles.Meta] SET HADR OFF;
-- Apply t-logs to catch up. This can be done manually in SSMS or via:
RESTORE LOG [StackExchange.Bicycles.Meta] FROM DISK = '\\ny-back01\backups\SQL\_Trans\SENetwork_AG\StackExchange.Bicycles.Meta\StackExchange.Bicycles.Meta_LOG_20160217_033201.trn' WITH NORECOVERY;
-- Re-join database to availability group
ALTER DATABASE [StackExchange.Bicycles.Meta] SET HADR AVAILABILITY GROUP = [SENetwork_AG];
ALTER DATABASE [StackExchange.Bicycles.Meta] SET HADR RESUME;
@melvinlee
melvinlee / ExportSQLServerDatabaseRole.sql
Created March 3, 2017 07:06
Script out Database Role Definition
/********************************************************************
* *
* Author: John Eisbrener *
* Script Purpose: Script out Database Role Definition *
* *
********************************************************************/
DECLARE @roleName VARCHAR(255)
SET @roleName = 'DatabaseRoleName'
@melvinlee
melvinlee / DisposableAction.cs
Created June 30, 2016 02:42
Disposable Action
internal class DisposableAction : IDisposable
{
private readonly Action _action;
public DisposableAction(Action action)
{
_action = action;
}
public void Dispose()