Posted on

Using AWS Auto Scaling with an Elastic Load Balancer cluster on EC2

Back in June last year I wrote about creating a WordPress cluster on Amazon’s EC2. In this post I’ll run through a couple of the problems with that cluster, I’ve experienced, and how I solved them with Amazon’s Auto Scaling service.

The problems with the cluster

A couple of things were not ideal with the cluster that I’ve been putting up with for far too long but finally set aside some time this afternoon to fix.

1) The price of micro-sized spot instances spikes, to crazy prices, sometimes.
This has meant that although the price of micro instances while they’re running is cheap, when the price spikes they all die off and leave the cluster vulnerable. Unfortunately to set up auto-scaling in combination with spot-priced micro instances would require coding up a hybrid solution with shell scripts, and although I enjoy tinkering with this setup, I can’t justify that much effort when an out-of-the-box solution exists, Auto Scaling.
Continue reading Using AWS Auto Scaling with an Elastic Load Balancer cluster on EC2

Posted on

How to Edit and Disable Checkout Fields in Woocommerce

This is just a quick post that shows you how to edit or disable the checkout fields in Woothemes WordPress shopping cart Woocommerce. It may make all the Magento developers jealous to see how nearly trivial it is – but I couldn’t see a good blog post explaining it, so here goes.

Disabling and Editing Checkout

To use the WordPress API, I made a simple little plugin that listens for the filter event and then makes some changes to the arrays.

The anatomy of a plugin is really just a class definition, in the constructor you listen for events and bind them to functions of that class. In those functions, you do the real grunt work. In this case, that’s where I weed out disabled fields, and swap in my edited field definitions.

Hopefully the way the code is laid out makes the actual editing/disabling is performed self-evident. To see the full range of available fields and options, please see the underlying array declaration in the core file woocommerce/classes/checkout.class.php.

My class looks like this:
Continue reading How to Edit and Disable Checkout Fields in Woocommerce

Posted on

Java App Engine integration testing with Spring MVC and OpenPersistenceManagerInViewFilter

In this post I’ll quickly show the technique I used for integration testing of Java services where the services rely on Spring data repositories being injected. We’re using Google’s App Engine to host and run our mobile sales dashboard for Magento/Amazon/Shopify so the tests will use the integration testing environment provided in the SDK.

Starting with a number of great articles on the subject I was able to quickly get a basic integration test up and running for App Engine with Spring. Our problem was related particularly to the PersistenceManager.

In the real application each web request is assigned a ThreadLocal PeristenceManager which is kept alive for the duration of that request, and always closed at the end, thanks to the OpenPersistenceManagerInViewFilter provided by Spring. So this is great when you’re calling data access methods on repositories from services in a web container, but means that if you’re trying to call those services during an integration test, the different repository calls will trigger exceptions that the objects involved belong to different PersistenceManagers.

The solution was to emulate the behavior of the OpenPersistenceManagerInViewFilter before and after a web request, in the setUp and tearDown of the tests themselves. For that I checked out the code for the filter and simply applied the same logic in the test.

First we make sure the Test class is setup to use Springs Junit functionality like below. Also note we are specifically setting the context to use the two test xml configurations. This is important because we actually wire a LocalPersistenceManagerFactoryBean in this Test class.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =  {"classpath:application-config-test.xml", "classpath:servlet-config-test.xml"})
public abstract class BaseSpringJunitTest
extends AbstractJUnit4SpringContextTests {
 
	@Autowired
	LocalPersistenceManagerFactoryBean pmf;
 
	// Standard App Engine integration testing helper
	protected final LocalServiceTestHelper helper =
			new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig(), new
					LocalTaskQueueTestConfig(), new LocalMemcacheServiceTestConfig());
 
	// ...
}

Most of the test xml configuration is standard, the important bit is wiring in the bean, which should look familiar from your usual container deployment:

<bean class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean" name="persistenceManagerFactory">
	<property name="persistenceManagerFactoryName" value="transactions-optional" />
</bean>

The setUp() just creates a new PersistenceManager (from the PMF we wired in) and stores it for use during the test (the same way the filter stores it for a web request).

@Before
public void setUp() {
    helper.setUp();
    PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(pmf.getObject(), true);
    TransactionSynchronizationManager.bindResource(pmf.getObject(), new PersistenceManagerHolder(pm));
}

In the tearDown() we’ll just do the same thing the filter does after the request.

@After
public void tearDown() {
	PersistenceManagerHolder pmHolder = 			(PersistenceManagerHolder)TransactionSynchronizationManager.unbindResource(pmf.getObject());
    PersistenceManagerFactoryUtils.releasePersistenceManager(pmHolder.getPersistenceManager(), pmf.getObject());
	helper.tearDown();
}

Once you have that class setup, you can extend it for your actual testing like this:

public class TestAccountService extends BaseSpringJunitTest {
 
	@Autowired
	private AccountService accountService;
 
	//..
 
	@Test
	public void testSubscribeNull() {
		accountService.subscribe(null, null);
		// check that subscribe behaved OK for null parameters...
	}
}

That’s it, go forth and test!. App Engine sure does make local integration testing and developing a local copy of your app easy and painless – I suggest you try it.

PS: This is the first Java development related post I have written in a long time, apologies to the Magento developer readers out there, it may not be the last.

Posted on

Quick update on Site Monitor and Google App Engine D-Day

I’ve made a few changes to the way the monitor works in the hope that it will reduce the load on the app and thus the cost based on the new App Engine pricing. There are three implications you should be aware of if you use the monitor.

1) ‘Up’ emails are not sent anymore.
You’ll just have to trust the site is being checked, without the email confirmation.

2) Checking will be 24 times per day, roughly one hour apart
I’ve changed the way the monitoring queue works, so that the sites will be not be dequeued concurrently, and so checked more slowly. However they should all get checked every hour, approximately one hour apart. Failed sites get checked 2 further times to make sure they’re really down, these rechecks will happen almost straight away.

3) If your site is down for 96 consecutive checks (about 4 days) it will no longer be checked.
This is to stop sending endless emails for weeks when a site is taken offline.

Hopefully these changes will keep the monitor running for free from here on out.

Posted on

Google App Engine billing changes and my website monitoring service

A long long time ago I wrote a simple little monitoring service on Google App Engine. It’s been happily ticking over for the last couple of years but with the recent changes to Google’s App Engine pricing, I’m going to have to make some changes.

This is a blog post for those who do use the service (there’s over 100 of you!) and may care about whether it is still monitoring your site. I sent this email to you all (sorry for the spam!) and will make any further updates on this site to save your inbox tears.

Hi, All

I’m Ashley, I made the website monitoring service you signed up for
here: http://monitor.aschroder.com/ – if you no longer use it, or
don’t care, please delete this email – I won’t be sending any more.

If you’re still with us so far, firstly sorry to spam all of you, but
there’s been some important changes by Google in the way they price
the App Engine platform, and apps like this monitor service, are not
free any more.

I’ll do my best to do a quick rewrite of the monitor in a way that can
keep the running costs at zero, but if I can’t, I’m going to have to
shut it down. If that does happen, I’ll gladly open source the code,
so that you can all run your own version of it, on your own app engine
accounts.

As promised I won’t email any of you about this again, I know how
annoying emails like this are. If you use and care about the service
working, subscribe at http://www.aschroder.com for updates on the
future of the monitor, and the code if/when it is open-sourced.

Any questions, please just ask me.

Kind regards,
Ashley
aschroder.com

So either there’ll be an update about what the new service does and does not do, or there’ll be some code so you can run it yourself. D-Day is Nov 7.

I will just say, I’m still a big fan of App Engine, despite the changes – it’ll remain my platform of choice for future projects until something better comes along.