For me, one of the most important aspects of my career as a software developer is learning. I love to learn new stuff, and I don’t just mean play with shiny new technologies or gadgets; I mean improve my knowledge about the tools I need to use in my work. In fact, I like to learn something new every day!
So I thought I would start writing these quick snippets to record the new things I learn each day (assuming that I do in fact learn something new each day!).
Because not all browsers (ahem IE…) support console.log() when the debugger isn’t running, you may get a nullref exception!
It’s much better to use debugger; – so when you have the debugger open your code will automatically break at that point!
As I mentioned in part one of this 2-part “straight to TV” drama, Azure Webjobs are pretty awesome! They’re Scheduled Tasks without the secrecy. They’re Azure Worker Processes without the cost! You can host them on Azure for free as a Web App (recently renamed from “Websites”) and straight out of the box you get an awesome Dashboard that allows you to check the status of your Webjob and replay any single invocation at a single click.
WebJobs are amazing for doing processes that take a decent chunk of time and processing power, so you don’t hold up the your website’s thread and slow down any responses. For the WebJob that I recently wrote, I dump the url and database Id of a pdf file into the Message Queue, and the WebJob grabs the PDF, creates a thumbnail of the first page, saves it back to my Azure Blob Storage container and adds the thumbnail url back into the database – all for free and without freezing up the UI of my CMS.
In part one I showed you how to add messages to an Azure Message Queue. In this post I will show you how you can build an Azure Webjob to process and do something with those messages.
You will need to have the latest Azure SDK installed, which you should have done already if you followed part one. Oh you didn’t read part one yet? Shame on you…
Set up a WebJob project
First we need to create a new project – in Visual Studio, choose ‘Create New Project’ and choose ‘Azure WebJob’ from the options:
You can see this project looks pretty much like a console app – there’s a Program.cs file and an App.config file just like you would expect in a Console Application. The slight difference here is that there is a Functions.cs file. It’s this Functions.cs file that contains the method that will be triggered when a message is added to your message queue (the auto-generated comments on the method tell you pretty much the same thing):
You can see there are two parameters to this ProcessMessageQueue method. The first parameter is the actual message that you will be expecting from the Message Queue. Out of the box this is just a simple string, but as I demonstrated in part one it can be any object type you want. You just need to ensure that whatever object type you add to the message queue matches the object type you specify as this ProcessMessageQueue parameter. You will also notice that the message parameter is decorated by a [QueueTrigger(“queue”)] attribute. This is the bit that triggers this function when data is added to the Message Queue. You will need to swap the “queue” text in there to match the Message Queue name you already specified (remember that this needs to be all lower case in order for this to work…
The second parameter TextWriter allows you to write to the WebJob logfile, the contents of which you can view through the Dashboard (more to come on that later) in the same way you would use console.log() in a Console Application.
Add your Storage Account Connection Strings
The only other thing you need to do in order to get this basic WebJob example running is to tell it how to connect to your Message Queue. In order to do this, you need to add your Azure Storage Connection String (which I told you how to find in part one) into the App.config file. Out of the box there is already a placeholder Connection String in the App.config, so all you need to do is add your own data in there:
You will notice there are two Connection String placeholders in the App.config file. The AzureWebJobsStorage Connection String is the one you want to configure at this point. We’ll get on to the AzureWebJobsDashboard Connection String a bit later.
It’s that simple!
Yes it really is that simple to create a WebJob – now you just have to stick whatever code you want inside your ProcessMessageQueue method, so you can process the data that you stuck in the Message Queue in the first place. You can make that bit as simple or as complex as you like!
Publishing your WebJob
Publishing a WebJob to an Azure Web App is pretty simple too – right click on your WebJob project and choose ‘Publish as Azure Webjob…’:
On the next screen that pops up you will see a whole load of options for when and how your WebJob should run (I have chosen ‘Run Continuously’ for this demo) and click OK.
Next you will be able to choose your publish target – select ‘Microsoft Azure Websites’:
Next you will be asked to Sign In with your Azure account, and then you get to choose either an existing website or create a new one:
Choosing ‘New’ will take you to a screen where you can choose a name for your website, select which region you want to host it in and create a new database if you want to:
Now click ‘Create’ and your website will be created for you, and the publishing settings for the site will automatically be downloaded into your publishing profile:
Finally, click ‘Publish’ to publish your WebJob. The first time you publish a WebJob it takes a while, as it’s uploading all the DLLs etc that you’ve created with your code. Any subsequent publishes all seem to be pretty quick so I assume it’s just pushing up any files that have changed since the last publish.
Debugging your WebJob
So now we have published the WebJob it’s time to discover a pretty awesome feature – you can attach your local Visual Studio to your remote WebJob and debug it in real time! In Visual Studio view the Server Explorer (View > Server Explorer) and you will see ‘Azure’ showing at the top. Expand that and you will be able to see your Storage accounts and your Websites, along with all the other areas of Azure.
Expand the Websites node and you will see your WebJob appear there as a website. Drill down into this website (WebJobs > Continuous) and you will see your WebJob. Right click on your WebJob and select ‘Attach Debugger’:
Stick a breakpoint in your ProcessQueueMessage method and we will now trigger the WebJob by adding data manually to the Message Queue. Again in Server Explorer, navigate to your Message Queue (if it doesn’t yet exist, you can just right click on the Queues node and create it):
Opening up the Message Queue shows an empty table of messages. There is a set of icons on the top right of this table – click the ‘Add Message’ icon:
You will see a dialog box pop up – add some text in here (if your WebJob is expecting a more complex object, stick some Json in here to represent that object):
You will see the message added to the Message Queue table:
Now just sit back and relax for a few moments, and wait for your WebJob to be triggered. Cross your fingers and toes and shortly you should hit the breakpoint you added to your ProcessMessageQueue method (you did add a breakpoint didn’t you?):
Yes it really is that simple to set up a WebJob!
There’s only one thing left to show you before I go to bed, and that’s how to get the Dashboard up and running.
In Server Explorer if you right click on your WebJob and click ‘Dashboard’ you will probably see something like this:
If you can’t be bothered to read all that text I’ll save you the task. Basically you haven’t yet set up the Connection String for your Dashboard. You need to set this in 2 places – in your App.config file and also in your WebJob ‘Configure’ section in the Azure Management Portal.
To set the Connection String in your App.config, simply add the Storage Account info in the AzureWebJobsDashboard Connection String (you can use the same Storage Account that you used for the Message Queue, or set up a new one if you wish).
In the Azure Management Portal, choose your WebJob and click on ‘Configure’ from the top menu. Scroll towards the bottom of the page and you will see the Connection Strings section. Add your Connection String data in there. You will need to post that entire long string into the ‘Values’ textbox – DefaultEndpointsProtocol=https;AccountName=…;AccountKey=…) and choose ‘Custom’ from the dropdown menu on the right:
Now re-publish your WebJob with that extra config setting, and refresh the Dashboard page again. It should look a bit more like this:
This Dashboard is pretty cool – it tells you the status of the WebJob, it shows you the logfile output of the WebJob (click the ‘Toggle Output’ button to see the log messages). It also allows you to click on the function that you just invoked (the line at the bottom of this image) and view the details of that invocation (click the ‘Toggle Output’ button again and you will see the line of text we wrote to the logfile):
Finally you can also replay this invocation by hitting the ‘Replay Function’ button at the top of the screen which re-submits that message back to the Message Queue for you. This is really useful if the function fails, and you want to try it again to either debug it or run it once you have fixed a bug.
So that’s a really quick introduction to creating WebJobs – I hope I’ve covered all the basics and you can get up and running with this awesome Azure feature! Let me know in the comments if you’ve run into any issues setting this up, and I will try to help out if I can!
I use Trello for everything! From managing collaborative software projects to keeping personal To-Do lists, to tracking my weight loss! It’s an awesome tool and I love the way it visually helps me to plan and prioritise my life! Furthermore I’m a very impatient person with not enough time to do all the things I want! So I’m constantly trying to find little shortcuts here and there that will shave off a few seconds or minutes from the task I’m doing, so I can spend that time doing more stuff!
I was chatting to another developer the other day (we do talk to each other occasionally you know…) and I mentioned a couple of Trello shortcuts that save me those few precious seconds. He didn’t know they existed and he was pretty excited about – he assured me he was going to try them out as soon as he got home!
Maybe he was telling the truth, or maybe he was just humouring me and hoping I would just stop talking to him… either way I thought I would share my favourite Trello shortcuts with you, and you can see for yourself how much they improve your Trello productivity!
Shortcut 1 – Navigate to board
I currently have 14 boards in my Trello and I will switch backwards and forwards between at least 5 of them in any given day. This was the first shortcut I found and I love it! Press B and then start typing the name of your board. Watch with awe as your list of boards appears and is filtered down to the exact board you are looking for. Then hit Enter, lean back and enjoy those extra few seconds you have saved by not having to click on the Boards list, scroll down to find your board and click on it!
Shortcut 2 – Filter your lists
If you have long lists and you’re having trouble finding the card you want, press F and start to type. Magically see every list filtered so you can find your card without having to scroll…
Free bonus tip: Press F and then * and you will see the number of cards displayed at the top of each list!
Shortcut 3 – Add a new list
I found this one totally by accident one day – if you’re in a board and you double click anywhere on the background, the ‘Add List’ dialogue appears and guess what – you can ADD A NEW LIST!!!
Shortcut 4 – send your board an Email!
I’m constantly sending myself email reminders to do things or links to articles I want to read but maybe later. So imagine my glee when I discovered that you can send an email to your Trello board and it will turn your email into a card for you!
Expand the menu bar on the right hand side of the screen, and then click on the “Menu” dropdown at the top of that bar. You will see a link that says “Email-to-board Settings” which contains a randomly-generated email address that you can use to send an email to your board. You can choose which list to create the card in, and where you want it to go in that list.
Each board has it’s own email address for each member that has access to it, and you can regenerate your email address (like I did after taking that screenshot just in case you were wondering…) to ensure that only you can email your specific board.
The subject of the email will become the title of the card, and the body of the email will be the description. There are more formatting tricks skillz you can learn here if you want to become an ’email-to-board’ Ninja!
Shortcut 5 – Toggle side menu
In one of my Trello boards I have 11 lists! Even on my HD laptop screen means I have to scroll my board to see all my lists, and the right-hand side menu bar just gets in the way! But that’s a simple problem to solve – just press W and the side menu will appear or disappear! Nice.
Shortcut 6 – Select a label
Labels are a great way of categorising cards and giving a visual cue as to which cards belong to what category. When you’re on a card, press L and the Lists menu will magically appear. Each label has a number in brackets next to it. Simply press the number for the label you want, hit Enter and your card will be categorised!
Shortcut 7 – Colour-code your boards
If you have a lot of boards and they’re all the standard “Trello Blue” it can be difficult to find the one you want quickly (unless you’re using shortcut 1 that is!) From the right-side menu choose ‘Change Background’ and you will see a small number of colours to choose from (you can get more colours/backgrounds if you’re a paying customer, so if you’re aesthetically inclined, you might want to part with your cash!)
Shortcut 8 – Star your boards
Finally if you have a lot of boards in your Trello and you’re using it through an app, starring your favourite boards moves them all to the top of the Boards list/page and makes it much quicker for you to find the board you want!
Bonus Shortcut – set Trello as your browser homepage!
If you’ve got this far you deserve a reward, so here’s a little bonus shortcut! I set my browser homepage to be my ‘Backlog’ Trello board – this is my prioritised backlog where I bring cards in from all my other boards and stack-prioritise them! So each morning when I open my laptop I can see exactly what my priorities are for that day!
So there you go – if you’re using Trello a lot and you use all these shortcuts I can guarantee that you will save at least 2 extra minutes per day! Just think how many tweets you could send with all that saved time!
If you aren’t using Trello yet, use this link to sign up (it’s free!)
Do you have any other Trello shortcuts that you find invaluable? If so, share them in the comments below!