Skip to content

Commit

Permalink
Revised the existing Scheduler tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
ggailey777 committed Jan 1, 2013
1 parent 9525dd9 commit 968b04a
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 80 deletions.
Binary file added DevCenter/Mobile/Media/aa-mobile.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified DevCenter/Mobile/Media/mobile-create-job-dialog.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified DevCenter/Mobile/Media/mobile-schedule-job-enabled.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified DevCenter/Mobile/Media/mobile-schedule-job-script-new.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified DevCenter/Mobile/Media/mobile-schedule-job-script.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified DevCenter/Mobile/Media/mobile-services-selection.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,62 @@
<properties linkid="develop-mobile-tutorials-command-prompt-administration" urlDisplayName="Command prompt administration" pageTitle="Administer Mobile Services from the command prompt" metaKeywords="Windows Azure Mobile Services, command prompt, command line tool, mobile services" metaDescription="Learn how to use the Windows Azure command-line tool to automate the creation of management of Windows Azure Mobile Services." metaCanonical="" disqusComments="1" umbracoNaviHide="1" />

<div title="This is rendered content from macro" class="umbMacroHolder" onresizestart="return false;" umbpageid="15161" umbversionid="f1a70b05-645d-4fcd-bb15-74674509c46a" ismacro="true" umb_chunkpath="devcenter/Menu" umb_modaltrigger="" umb_chunkurl="" umb_hide="0" umb_chunkname="MobileArticleLeft" umb_modalpopup="0" umb_macroalias="AzureChunkDisplayer"><!-- startUmbMacro --><span><strong>Azure Chunk Displayer</strong><br />No macro content available for WYSIWYG editing</span><!-- endUmbMacro --></div>

# Administer Mobile Services from the command prompt

This topic shows you how to use the job scheduler functionality in the Management Portal to define server script code that is executed based on a schedule that you define. In this case, the script removes duplicate push notification channel URIs from a table. Duplicate records can occur when you do not check for duplicates before you insert a new channel URI. Duplicate URIs might result in a push notification being sent multiple times to the same device. Some other periodic tasks that can be scheduled include:

+ Archiving old data records.
+ Requesting and storing external data, such as tweets, RSS entries, and location information.
+ Processing or resizing stored images.

This tutorial walks you through these basic steps to demonstrate how to use the job scheduler to create a job that removes duplicate push notification channel URIs from the Channel table:

1. [Update Channel table scripts]
2. [Insert data to create duplicate channels]
3. [Create a new scheduled job]

1. Import publish settings
2. azure mobile delete - remove the site if it already exists
3. azure mobile create - recreate the site
4. Get the site master key
5. azure mobile table create - create a table
6. azure mobile table script create - add a script on the query operation. For example the script can add a single generated property to the returned object.
7. Run a simple client test that verifies the property was added. We will need they key that we fetched in step 4 to be able to write this script. As we had talked, probably curl the URL and check the status code and/or compare the payload to make sure the calculated property was added as expected.
8. We go and delete the mobile service

Depending on what we decide, we can try and test multiple scripts at a time using the same mobile service.

If it is hard, we can skip steps 1-4 and just create/delete tables when we try our scripts.


## <a name="nextsteps"> </a>Next Steps

* [Mobile Services server script reference]
<br/>Learn more about registering and using server scripts.

<!-- Anchors. -->
[Update Channel table scripts]: #update-scripts
[Insert data to create duplicate channels]: #insert-duplicates
[Create a new scheduled job]: #add-job
[Next steps]: #next-steps

<!-- Images. -->
[1]: ../Media/mobile-portal-data-tables-channel.png
[2]: ../Media/mobile-insert-script-channel-clear.png
[3]: ../Media/mobile-services-selection.png
[4]: ../Media/mobile-schedule-new-job.png
[5]: ../Media/mobile-create-job-dialog.png
[6]: ../Media/mobile-schedule-job-script-new.png
[7]: ../Media/mobile-schedule-job-script.png
[8]: ../Media/mobile-verify-channel-duplicates.png
[9]: ../Media/mobile-schedule-job-logs.png
[10]: ../Media/mobile-schedule-job-enabled.png

<!-- URLs. -->
[Push notifications to app users for Windows Store apps]: ../tutorials/mobile-services-push-notifications-to-app-users-dotnet.md
[Push notifications to app users for Windows Phone 8 apps]: ../tutorials/mobile-services-push-notifications-to-app-users-wp8.md
[Push notifications to app users]: ../tutorials/mobile-services-push-notifications-to-app-users-ios.md
[Mobile Services server script reference]: http://go.microsoft.com/fwlink/?LinkId=262293
[WindowsAzure.com]: http://www.windowsazure.com/
[Windows Azure Management Portal]: https://manage.windowsazure.com/
163 changes: 83 additions & 80 deletions DevCenter/Mobile/Tutorials/mobile-services-schedule-jobs.md
Expand Up @@ -2,61 +2,33 @@

<div title="This is rendered content from macro" class="umbMacroHolder" onresizestart="return false;" umbpageid="15161" umbversionid="f1a70b05-645d-4fcd-bb15-74674509c46a" ismacro="true" umb_chunkpath="devcenter/Menu" umb_modaltrigger="" umb_chunkurl="" umb_hide="0" umb_chunkname="MobileArticleLeft" umb_modalpopup="0" umb_macroalias="AzureChunkDisplayer"><!-- startUmbMacro --><span><strong>Azure Chunk Displayer</strong><br />No macro content available for WYSIWYG editing</span><!-- endUmbMacro --></div>

# Schedule backend jobs in Mobile Services
# Schedule recurring jobs in Mobile Services

This topic shows you how to use the job scheduler functionality in the Management Portal to define server script code that is executed based on a schedule that you define. In this case, the script removes duplicate push notification channel URIs from a table. Duplicate records can occur when you do not check for duplicates before you insert a new channel URI. Duplicate URIs might result in a push notification being sent multiple times to the same device. Some other periodic tasks that can be scheduled include:
This topic shows you how to use the job scheduler functionality in the Management Portal to define server script code that is executed based on a schedule that you define. In this case, the script periodically check with a remote service, in this case Twitter, and stores the results in a new table. Some other periodic tasks that can be scheduled include:

+ Archiving old data records.
+ Archiving old or duplicate data records.
+ Requesting and storing external data, such as tweets, RSS entries, and location information.
+ Processing or resizing stored images.

This tutorial walks you through these basic steps to demonstrate how to use the job scheduler to create a job that removes duplicate push notification channel URIs from the Channel table:
This tutorial walks you through the basic steps of how to use the job scheduler to create a scheduled job that requests tweet data from Twitter and stores the tweets in a new Updates table.

1. [Update Channel table scripts]
2. [Insert data to create duplicate channels]
3. [Create a new scheduled job]

This tutorial uses the Channel table. This table is created when you complete the tutorial [Push notifications to app users for Windows Store apps] or [Push notifications to app users for Windows Phone 8 apps]. Completing one of these tutorials is a prerequisite for completing this tutorial. This approach can be used to clean up any duplicate data, no matter how it was generated.

<a name="update-scripts"></a><h2><span class="short-header">Update scripts</span>Update Channel table scripts</h2>
<a name="create-table"></a><h2><span class="short-header">Create new table</span>Create the new Updates table</h2>

1. Log on to the [Windows Azure Management Portal], click **Mobile Services**, and then click your mobile service.

![][3]

2. Click the **Data** tab, then click the **Channel** table.

![][1]
2. Click the **Data** tab, then click **+Create**.

3. In **channel**, click the **Script** tab, select **Insert**, click **Clear**, then click **Yes** to confirm.

![][2]

This removes the code that checks for duplicate URI values before inserting them into the Channel table.

<div class="dev-callout"><b>Note</b>
<p>To restore the original functionality of the push notifications tutorial, replace the copied <strong>insert</strong> function code.</p>
</div>

Now that you have disabled duplicate checking on the Channel table, you can run the app to insert duplicate records.

<a name="insert-duplicates"></a><h2><span class="short-header">Generate duplicates</span>Insert data to create duplicate channels</h2>
This displays the **Create new table** dialog.

1. In the appropriate version of Visual Studio 2012 Express, open the app project from either [Push notifications to app users for Windows Store apps] or [Push notifications to app users for Windows Phone 8 apps].
3. In **Table name** type _Updates_, then click the check button.

2. Press the **F5** key to rebuild the project and start the app.

3. Stop or close the app and repeat steps 2.

This adds duplicate channel URIs into the Channel table.

5. Back in the Management Portal, click **Browse**

![][8]
![][3]

Notice that there are two or more entries in the table with the same **Uri** value.
This creates a new storage table **Updates**.

6. Click the back arrow to return to the mobile service page.
Now that you have somewhere to store Twitter data, you can create the scheduled job.

<a name="add-job"></a><h2><span class="short-header">Create a new job</span>Create a new scheduled job</h2>

Expand All @@ -68,83 +40,114 @@ Now that you have disabled duplicate checking on the Channel table, you can run
<p>When you run your mobile service in <i>free</i> mode, you are only able to run one scheduled job at a time. In <i>reserved</i> mode, you can run up to ten scheduled jobs at a time.</p>
</div>

3. In the scheduler dialog, enter <i>cleanup_channels</i> for the **Job Name**, set the schedule interval and units, then click the check button.
3. In the scheduler dialog, enter <i>getUpdates</i> for the **Job Name**, set the schedule interval and units, then click the check button.

![][5]

This creates a new job named **cleanup_channels**.
This creates a new job named **getUpdates**.

4. Click the new job you just created, then click the **Script** tab.

![][6]

5. Replace the placeholder function **cleanup_channels** with the following code:

function cleanup_channels() {
var sql = "SELECT MAX(Id) as Id, Uri FROM Channel " +
"GROUP BY Uri HAVING COUNT(*) > 1";
var channelTable = tables.getTable('Channel');
5. Replace the placeholder function **getUpdates** with the following code:

var updatesTable = tables.getTable('Updates');
var request = require('request');
function getUpdates() {
// Check what is the last tweet we stored when the job last ran
// and ask Twitter to only give us more recent tweets
appendLastTweetId(
'http://search.twitter.com/search.json?q=%23mobileservices&result_type=recent',
function twitterUrlReady(url){
request(url, function tweetsLoaded (error, response, body) {
if (!error && response.statusCode == 200) {
var results = JSON.parse(body).results;
if(results){
console.log('Fetched new results from Twitter');
results.forEach(function visitResult(tweet){
if(!filterOutTweet(tweet)){
var update = {
twitterId: tweet.id,
text: tweet.text,
author: tweet.from_user,
date: tweet.created_at
};
updatesTable.insert(update);
}
});
}
} else {
console.error('Could not contact Twitter');
}
});
});
}
// Find the largest (most recent) tweet ID we have already stored
// (if we have stored any) and ask Twitter to only return more
// recent ones
function appendLastTweetId(url, callback){
updatesTable
.orderByDescending('twitterId')
.read({success: function readUpdates(updates){
if(updates.length){
callback(url + '&since_id=' + updates[0].twitterId + 1);
} else {
callback(url);
}
}});
}

mssql.query(sql, {
success: function(results) {
if (results.length > 0) {
for (var i = 0; i < results.length; i++) {
channelTable.del(results[i].Id);
console.log('Deleted duplicate channel:' +
results[i].Uri);
}
} else {
console.log('No duplicate rows found.');
}
}
});
}

6. Click **Save**, then **Run Once** to test the script.
function filterOutTweet(tweet){
// Remove retweets and replies
return !((tweet.text.indexOf('RT') !== 0) && (tweet.to_user_id === 0));
}

This script calls the Twitter query API to request recent tweets that contain the hashtag #mobileservices. Duplicate tweets and replies are removed from the results before they are stored in the table.

6. Click **Run Once** to test the script.

![][7]

This executes the job while it remains disabled in the scheduler.
This saves and executes the job while it remains disabled in the scheduler.

7. Click the back button, click **Logs**, locate the **Deleted duplicate...** item, click **Details**, and verify that a duplicate row was deleted.
7. Click the back button, click **Data**, click the **Updates** table, click **Browse**, and verify that Twitter data has been inserted into the table.

![][9]
![][8]

8. Click **Scheduler**, select **cleanup_channels**, then click **Enable**.
8. Click the back button, click **Scheduler**, select **getUpdates**, then click **Enable**.

![][10]
![][9]

This enables the job to run on the specified schedule, in this case every hour.

Congratulations, you have successfully created a new job schedule in your mobile service. This job will be executed as scheduled until you disable or modify it.
Congratulations, you have successfully created a new scheduled job in your mobile service. This job will be executed as scheduled until you disable or modify it.

## <a name="nextsteps"> </a>Next Steps

* [Mobile Services server script reference]
<br/>Learn more about registering and using server scripts.

<!-- Anchors. -->
[Update Channel table scripts]: #update-scripts
[Insert data to create duplicate channels]: #insert-duplicates
[Create the new Tweets table]: #create-table
[Create a new scheduled job]: #add-job
[Next steps]: #next-steps

<!-- Images. -->
[1]: ../Media/mobile-portal-data-tables-channel.png
[2]: ../Media/mobile-insert-script-channel-clear.png
[3]: ../Media/mobile-services-selection.png
[1]: ../Media/mobile-services-selection.png
[2]: ../Media/mobile-data-tab-empty.png
[3]: ../Media/mobile-create-updates-table.png
[4]: ../Media/mobile-schedule-new-job.png
[5]: ../Media/mobile-create-job-dialog.png
[6]: ../Media/mobile-schedule-job-script-new.png
[7]: ../Media/mobile-schedule-job-script.png
[8]: ../Media/mobile-verify-channel-duplicates.png
[9]: ../Media/mobile-schedule-job-logs.png
[10]: ../Media/mobile-schedule-job-enabled.png
[8]: ../Media/mobile-browse-updates-table.png
[9]: ../Media/mobile-schedule-job-enabled.png

<!-- URLs. -->
[Push notifications to app users for Windows Store apps]: ../tutorials/mobile-services-push-notifications-to-app-users-dotnet.md
[Push notifications to app users for Windows Phone 8 apps]: ../tutorials/mobile-services-push-notifications-to-app-users-wp8.md
[Push notifications to app users]: ../tutorials/mobile-services-push-notifications-to-app-users-ios.md
[Mobile Services server script reference]: http://go.microsoft.com/fwlink/?LinkId=262293
[WindowsAzure.com]: http://www.windowsazure.com/
[Windows Azure Management Portal]: https://manage.windowsazure.com/

0 comments on commit 968b04a

Please sign in to comment.