How do I know when an Amazon EC2 operation is complete? - amazon-ec2

Besides polling, how can I tell when a long-running Amazon EC2 operation is complete? For example, using the CreateImage API function can take upwards of several minutes.
Right now I'm doing this:
// MAKE THE API CALL
var createRequest = new CreateImageRequest().WithInstanceId("i-123456").WithName("MyNewAMI");
var createResponse = myAmazonEC2Client.CreateImage(createRequest);
var imageId = createResponse.CreateImageResult.ImageId;
// ICKY POLLING CODE
bool isImaging = true;
while (isImaging)
{
var describeRequest = new DescribeImagesRequest().WithImageId(imageId);
var describeResponse = myAmazonEC2Client.DescribeImages(describeRequest);
isImaging = describeResponse.DescribeImagesResult.Image.Single().ImageState == "pending";
Thread.Sleep(10000); // sleep for 10 seconds
}
// CreateImage IS COMPLETE; MOVE ON WITH OUR WORK
I hate this. After calling CreateImage, I'd like to just get notified somehow that it's all done and move on. Is this possible? I'm using the AWS .NET SDK in this example, but I'm not looking specifically for a C# solution.
UPDATE: Cross-posted to the AWS Forums

Some events in amazon can be configured to send notifications to an SNS Topic. For example when using auto scaling you can have notifications when a server is launched and terminated. As far as I know there is no way to trigger these notifications for other services such as CreateImage. I've looked for this type of feature in the past with no luck. I was trying to do it to create a script that would launch servers in a specific order. I wound up just polling their API as I couldn't find any way to register to those events.

James Hunter Ross answered this question over on the AWS Forums as follows:
Polling is it. That said, since you have a C# program started, why not let it spawn a polling process that notifies you as you wish? It seems you are almost done, in some respects.
(Of course, it would be nice if such functionality was built-in at AWS.)
I wasn't able to find a StackOverflow profile for him, but if he shows up I'll edit this to give him credit.

Related

Implement service on Firefox OS

I'm quite new to Firefox OS. At the moment I'm struggling with implementing some kind of service that listens for geolocation updates in the background.
If there are lots of apps running at the same time mine seems to be killed. While debugging with App Manager it disconnects silently.
I tried requestWakeLock('cpu') and the use of a Worker (as proposed in this thread) but without success.
Background services API isn't implemented, yet and will be available for certified apps only.
I know that there are non-certified apps like ConnectA2 that stay alive all the time so there has to be a way.
Could anybody give me a hint?
Firefox OS doesn't provide a way for you to run a service in the background intentionally, since the classes of the devices that we target (for example, the 128MB device) won't be able to support running apps constantly in the background.
There are alternate ways of implementing these kinds of services though. For example you can use the mozAlarm API in order to wake up your application at specific intervals, or you can use the SimplePush API which allows you to notify your app when a remote server initiates an event.
You can use Alarm API to prevent your app to be killed in the background.
Alarms wakes up the app at fixed intervals.
var alarmId = 0;
function setAlarm() {
function onAlarmAdded() {
alarmId = request.result;
}
var alarmDate = new Date(Date.now() + (60 * 1000)); // 60 seconds later
var request = navigator.mozAlarms.add(alarmDate, "ignoreTimezone");
request.onsuccess = onAlarmAdded;
}
function setHandler() {
function onAlarm(mozAlarm) {
// set next alarm
setAlarm();
}
navigator.mozSetMessageHandler("alarm", onAlarm);
}
function startAlarm() {
setHandler();
setAlarm();
}
function stopAlarm() {
navigator.mozAlarms.remove(alarmId);
}

Problems with online visitor tracking

I am working with web-socket project. We are tracking the customers of the website and showing that information to our customers dashbaord.
For this we are using pubnub service. They have api to subscribe and her_now to get currenly active connections. But seems it is not working properly. It is not tracking as we expecting.
It is not sending events properly when customers have lot of traffic(>150 active). And even his cost is too high.
We are planning to move to some other technique to do this. Please suggest which one is the good option.
How about nodejs with socket.io. One thing i am thinking the scalability when our customers have lot of traffic.
Please suggest on this.
Thanks,
Govind.
Move to Socket.io (It,s support mobile and all browser) and performance is good
Problems with online visitor tracking:
Here is my answer,When user visit you website you emit some event using following code
Server side:
var io = require('socket.io');
io.sockets.on('connection', function (socket) {
//From user
socket.on('userEnterYourWebsite',function(data){
//Do some DB operation means retrive user related information from DB
//If emit some data to user
socket.emit('sendToUser',{name:'mmm',state:'tamilnadu'});
});
});
Client Side:
<script>
var socket=io.connect('http://ipaddress:8080');
var userIDorUniqueID='mmmmmmm';
socket.emit('userEnterYourWebsite',{uniqueID:userIDorUniqueID});
//From server
socket.on('sendToUser',function(data){
console.log(data.state);
});
</script>
Property Annouce Max
You just need to set the Presence Announce Max property on the Presence add-on higher. Contact support#pubnub.com to set it higher than 100.
Build vs. Buy
What will be the cost of scaling your DIY node/socket.io solution? See the following before considering the DIY route.
http://www.pubnub.com/blog/build-vs-buy-why-diy-isnt-always-better/
http://www.pubnub.com/customers/swaplingo/
http://www.pubnub.com/customers/tint/

Best Way to Send Scheduled E-Mail in .NET MVC3 Application using MVCMailer

I am working on a .NET MVC3 C# Application. This application is hosted on our own Server.
Now I want to Send Scheduled Email in my application like daily(at a specific time),Weekly, monthly and so on...
Currently I am using MVCMailer to send Emails in my application.
I tried Fluent Scheduler to send scheduled Emails, but it doesn't works with MVCMailer. It Works fine if I send mails without MVCMailer and for other scheduling jobs.
It gives me a ERROR NULLReferenceException and says HTTPContext cannot be null.
What can I do to solve this problem.
Also suggest me which will be the best way to send E-mails in my applicaton.
Windows Service (Having own server)
Scheduler (Fluent Scheduler)
SQL Scheduled jobs
I am attaching ERROR snapshot:
It could be that MVCMailer depends on an HttpContext, which will not exist on your scheduled threadlocal's.
You could consider scrapping MvcMailer and implementing your own templating solution. Something like RazorEngine (https://github.com/Antaris/RazorEngine), which gives you the full power of Razor without having to run ontop on an Http stack. You could still source your templates from disk so that your designers could modify it.
Then you could mail the results using the standard classes available from .net.
For e.g.:
string template = File.ReadAllText(fileLocation);//"Hello #Model.Name, welcome to RazorEngine!";
string emailBody = Razor.Parse(template, new { Name = "World" });
SmtpClient client = new SmtpClient();
client.Host = "mail.yourserver.com";
MailMessage mm = new MailMessage();
mm.Sender = new MailAddress("foo#bar.com", "Foo Bar");
mm.From = new MailAddress("foo#bar.com", "Foo Bar");
mm.To.Add = new MailAddress("foo#bar.com", "Foo Bar");
mm.Subject = "Test";
mm.Body = emailBody;
mm.IsBodyHtml = true;
client.Send(mm);
Obviously you could clean this all up. But it wouldn't take to much effort to use the above code and create some reusable classes. :)
Since you already have the FluentScheduler code set up, you may as well stick with that I guess. A windows service does also sound appealing, however I think that it's your call to make. If it's a simple mail service you are after I can't think of any reason not to do it via FluentScheduler.
I have created a full example of this available here: https://bitbucket.org/acleancoder/razorengine-email-example/src/dfee804d526ef3cd17fb448970fbbe33f4e4bb79?at=default
You can download the website to run locally here: https://bitbucket.org/acleancoder/razorengine-email-example/downloads
Just make sure to change the Default.aspx.cs file to have your correct mail server details.
Hope this helps.
Since MVC Mailer works best in the HTTP stack (i.e. from controllers), I've found that a very reliable way to accomplish this is by using Windows Task Schedule from a server somewhere. You could even spin up a micro instance on Amazon Web Server.
Use "curl" to call the URL of your controller that does the work and sends the emails.
Just setup a Scheduled Task (or Cron if you want to use *IX) to call "c:\path_to_curl\curl.exe http://yourserver.com/your_controller/your_action".
You could even spin up a *IX server on AWS to make it even cheaper.

How to refresh my HttpWebRequest

I'm creating a Windows Phone 7 app which allow to listen a web radio. But I also want to get the cover of the song and I want it to be refreshed every 3 minutes for example.
When I start debugging my app, I've no problem but I've no idea how to call back my code to refresh the cover.
Thanks a lot.
Aymeric.
I found a solution not really the one I looked for but my mates used a timer to refresh the same application for Android and iOS since there is no push notification from the server.
So I used this code
DispatcherTimer refreshTimer = new DispatcherTimer();
refreshTimer.Interval = TimeSpan.FromSeconds(30);
refreshTimer.Tick += new EventHandler(refreshTimer_Tick);
refreshTimer.Start();
Thank you all for the time you spent on this question.
Aymeric.

Checkin Notification with StarTeam

Does someone know how we can have Starteam send email notifications when a check in occurs? We are using Starteam 2006 R2.
Unfortunately StarTeam doesn't offer the ability to execute post-checkin actions. You may be able to use an application like Cruise Control to monitor your repository for changes and then take action upon seeing them.
I had a similar need a few months ago, this is what I discovered:
Starteam does not have commit hooks but it does have Starteam MPX (borland.com). From that link,
StarTeamMPX is a framework for publish/subscribe messaging. The StarTeamMPX Server uses an advanced caching and communication technology that both improves the performance of StarTeam clients and extends the scalability of the StarTeam Server.
Ok, so, we can subscribe to events. It's looking promising.
There is a Java API (borland.com) for Starteam, create an app using this API with your own emailing implemention of the CheckinListener interface. The app will then have to connect to Starteam, find any views that you're interested in and register listeners against them. And then wait.
Your listener will receive CheckinEvents and can interrogate these. Unfortunately, it appears to be on a file by file basis. I couldn't see anything in the API to say "commit finished", only "file finished". You are able to discover if a commit was cancelled. I don't know how easy it is to combine file checkin events back into a complete check in event.
*StarteamMPX is an extension (paid) to Starteam, it is available for 2006 R2. All of this is clearly only applicable if it is enabled.
My Experience:
My company didn't have that extension enabled, and to enable it required upgrading i.e. more money. So it didn't happen (I think it's painful enough paying for Starteam to begin with). At this point I abandoned my research and none of the above was ever implemented this. I hope this is some use to someone.
I've been doing some homework into this topic too, so will share what I've learnt.
MicroFocus now offer a Notification Agent tool for this sort of thing:
http://www.youtube.com/watch?v=QTKAT-ufkIs
It's an extra you pay for though.
I've been also pondering how to "roll-your-own" via advice given in Dan's post above. Yes, MPX does seem to be the way to go, although after studying the CheckinListener, this isn't the class you're after. To clarify, CheckinListener is used by the client performing the check-in, so that it can monitor progress of the check-in (perhaps to display a progress bar, that sort of thing).
Here's some sample-code of what listening to MPX events looks like:
Server s = new Server(strAddress, nPort);
s.connect();
s.enableMPX(); // must do this for MPX support
s.logOn(strUsername, strPassword);
Project p = s.findProject("mylovelyproject");
View v = p.s.findView("mylovelyview");
ItemListener listener = new ItemListener()
{
public void itemAdded(ItemEvent e)
{
System.out.println("itemAdd() - " + e.getNewItem().getComment());
}
public void itemMoved(ItemEvent e)
{
System.out.println("itemMoved() - from: " + e.getOldItem().getParentFolderHierarchy() + ", to: " + e.getNewItem().getParentFolderHierarchy());
}
public void itemChanged(ItemEvent e)
{
System.out.println("itemChanged() - " + e.getNewItem().getComment());
System.out.println(" - from: v" + e.getOldItem().getDotNotation().toString());
System.out.println(" - to: v" + e.getNewItem().getDotNotation().toString());
User locker = e.getNewItem().getLocker();
if (locker != null)
System.out.println(" - locked by:" + locker.getDisplayName());
else
System.out.println(" - not locked");
}
public void itemRemoved(ItemEvent e)
{
System.out.println("itemRemoved() - " + e.toString());
}
};
v.addItemListener(listener, s.getTypes().FILE);
The MPX-related items to focus on here are new ItemListener() (what to do about the events you listen to) and v.addItemListener() (which starteam view you want to listen to).
The sample code will spit out various print output to the console as files in your view are added/modified/moved/deleted.
Apart from ItemListener, you also have ViewListener and ProjectListener. Each interface provides a different scope of events to listen for, more info on this in the sdk docs, also a nice article here:
http://conferences.embarcadero.com/article/32231#MPXEventHandling
So if you want to roll-your-own notification emails, these MPX events provide part of your answer (a way to listen to these change-events).
Other aspects you'll need to look into after this are:
How to allow users to subscribe to various servers/projects/views, to decide what they want to listen to.
How to email to the user what they want (StarTeam's Server class offers a .SendMail() method, which can help here).
Once all these bases are covered, you should have something that does the trick. I'll be working on something like this myself over the coming days, I'll share what I can.

Resources