Archive

Archive for April, 2022

Running #Mac on #AWS

Running Mac on AWS isn’t that new, it’s been available since 2020, however, it’s a useful cost-saver for developers looking to create an iOS app without purchasing a Mac. Using this approach, you can create a remotely-accessible Mac that you can turn on and off when you need it. Theoretically, costing about $1 an hour (Effetively, the host needs to be reserved for 24 hours, so it’s more like $24 a day), which makes it more or less like the MacInCloud offering of $30 a day.

Before you can create a Mac Ec2 Instance, you need to create a Mac Dedicated host. Once this is created, you can create a Mac Ec2 Instance and apply it to the host. Once created, you can connect to it using a VNC Client (Assuming you want a GUI interface)

Copy/Pasting from AWS help, here is how you connect via VNC from Windows

2.    Run the following command to install and start VNC (macOS screen sharing SSH) from the Mac instance

sudo defaults write /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing -dict Disabled -bool false
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.screensharing.plist

3.    Run the following command to set a password for ec2-user:

sudo /usr/bin/dscl . -passwd /Users/ec2-user

4.    [ON YOUR WINDOWS PC] Create an SSH tunnel to the VNC port. In the following command, replace keypair_file with your SSH key path and 192.0.2.0 with your instance’s IP address or DNS name.

ssh -i keypair_file -L 5900:localhost:5900 ec2-user@192.0.2.0

Note: The SSH session should be running while you’re in the remote session.

5.    Using a VNC client, connect to localhost:5900.

And then you can go ahead and install XCode and your App source code.

Don’t forget to de-allocate your dedicated host after stopping your ec2 instance, otherwise you’ll have a huge bill at the end of the month!

The overall experience is much more “clunky” and slow than a real Mac, it’s not much of a substitute, and the risk that if you forget the de-allocate the dedicated host, then it’ll cost you more than a mac itself in AWS fees.

Categories: Uncategorized

A Waterfall pattern for C#

First, let’s paint a scenario – you can query multiple sources for the same data, with the hope that one of these sources will have a response.

These sources can respond in three possible ways;

1 . An answer.

2. An exception indicating that the answer is impossible, and there is no need to query other sources

3. An exception indicating that the source failed, but perhaps another source will work.

So, Let’s define an exception of case (2)

class ExpectedException : Exception
{
}

Now, define our three sources, which must have the same input and output types, in this case they will be strings, but they could be any types, as long as they are consistent.

static string SourceA(string input)
{
	throw new Exception("Some random error");
}

static string SourceB(string input)
{
	throw new ExpectedException();
}

static string SourceC(string input)
{
	return "OK";
}

Now, we define a Waterfall function, that will call a list of functions in order, until it reaches either case (1) or case (2)

private static T1 Waterfall<T1,T2>(IEnumerable<Func<T2, T1>> waterfallFunctions, T2 parameter)
{
	var waterfallExceptions = new List<Exception>();
	foreach (var waterfallFunction in waterfallFunctions)
	{
		try
		{
			var functionResult = waterfallFunction(parameter);
			return functionResult;
		}
		catch (ExpectedException)
		{
			throw;
		}
		catch (Exception e)
		{
		   waterfallExceptions.Add(e);
		}
	}
	throw new AggregateException(waterfallExceptions);
}

Which is called as follows

var result = Waterfall( new List<Func<string, string>>
{
	SourceA,
	SourceC,
	SourceB
},"hello");

This code is available on a public Github repo here: https://github.com/infiniteloopltd/Waterfall

Hope this helps someone!

Categories: Uncategorized
%d bloggers like this: