[Use Case] How to publish Teapot application in Ephemeric Cloud

To publish Pharo Image in Ephemeric cloud you need to make it autostart on 8080 port. It is not a problem for Seaside applications as ZnZincServerAdaptor takes care of restarting the server on VM restart, however this function is not included in Teapot framework. If you have just registered Teapot application and saved your Image, the application will not run when you restart the Image. Thus you will not be able to publish it in Ephemeric Cloud as the published Image will not respond on 8080 port.

This article suggests to try approaches that make possible to publish Teapot applications in Ephemeric Cloud

1 Startup file

The most straightforward solution is to add a startup.st file to Image Archive File before publishing your application to Ephemeric cloud. This file is executed by pharo-vm each time the Image is loaded in Ephemeric cloud memory.

You can read more on Image startup file in Ephemeric Instances article.

startup.st

    teapot := Teapot stopAll; configure: { #port -> 8080 }.
    teapot
        serveStatic: '/' from: './static/';
        "put more teapot commands here"
        start.

2 Publishing with Pharo EphemericAPI library

If you implement publishing of Image using Pharo EphemericAPI library then you can use one trick to initialize your Image in the cloud.

The method of EphemericApiClient publishMeAs:with: makes a copy of current Image and publishes it in the Cloud. As it is a full copy of the Image it means two Images (copy and original) are saved in the state of executing code. This means that when the published version is run in the cloud it will continue current instruction flow from the point of backup.

To be able to run different code in original image and in the copy the publishMeAs:with: method answers boolean value which is true for the copy and false for the original image.

Therefore you may push some startup instructions easily on your published image. The following is a snippet of publishing code of the PillarBook application:

isNew := client publishSelfAs: imageName.
isNew 
	ifTrue: [ "We are in ephemeric cloud, initialize environment"
		WAAdmin clearAll.
		WAAdmin disableDevelopmentTools.
		PBViewApplication registerAt: self defaultBaseUrl.
		PBArticle exportDirectory: nil. "work directory as default"
		WAAdmin defaultDispatcher defaultName: self defaultBaseUrl.
		WAAdmin register: WAFileHandler at: 'files'. "standard libraries"
		^nil ];
	ifFalse: [ "Original image"
		^client lastPublishedInstance ]

3 Smalltalk start-up list

Pharo allows to run script on start of your Image by adding start-up scripts inside the Image itself. 'Startup list' protocol included of SmalltalkImage (accessible via Smalltalk global variable) is used for that purpose.

To add a new entry to the list create a new class in the Image and implement startUp method on the class side. The method will be run at every start of an Image. To add the class to startup list execute the following code and then save the Image:

Smalltalk addToStartUpList: MyClass.

To make Teapot run on Image restart, just add the initialization code of Teapot application to the startUp class method of class added to Smalltalk startup list. For instance the following code initialize simple counter application:

CounterApplication class>>startUp
    |counter teapot|

    counter := 0.

    teapot := Teapot stopAll; configure: { #port -> 8080 }.

    teapot
        serveStatic: '/' from: './static/';
        GET: '/counter' -> [:req | counter ];
        GET: '/inccounter' -> [:req |counter := counter + 1];
        GET: '/deccounter' -> [:req |counter := counter - 1];
        start.

You may download the Image with Teapot auto-starting here: http://imagetransport.pharocloud.com/imagestore/TeapotCounter.zip

To test the Image in Ephemeric cloud just drag&drop the archive to the Ephemeric cloud manager site.