diff --git a/DevCenter/Mobile/Media/aa-mobile.png b/DevCenter/Mobile/Media/aa-mobile.png new file mode 100644 index 00000000000..4b676ed27fc Binary files /dev/null and b/DevCenter/Mobile/Media/aa-mobile.png differ diff --git a/DevCenter/Mobile/Media/mobile-browse-updates-table.png b/DevCenter/Mobile/Media/mobile-browse-updates-table.png new file mode 100644 index 00000000000..4677d370685 Binary files /dev/null and b/DevCenter/Mobile/Media/mobile-browse-updates-table.png differ diff --git a/DevCenter/Mobile/Media/mobile-create-job-dialog.png b/DevCenter/Mobile/Media/mobile-create-job-dialog.png index ec7c0e3d0b0..96be1e88648 100644 Binary files a/DevCenter/Mobile/Media/mobile-create-job-dialog.png and b/DevCenter/Mobile/Media/mobile-create-job-dialog.png differ diff --git a/DevCenter/Mobile/Media/mobile-create-updates-table.png b/DevCenter/Mobile/Media/mobile-create-updates-table.png new file mode 100644 index 00000000000..e58dcd9d3d4 Binary files /dev/null and b/DevCenter/Mobile/Media/mobile-create-updates-table.png differ diff --git a/DevCenter/Mobile/Media/mobile-schedule-job-enabled.png b/DevCenter/Mobile/Media/mobile-schedule-job-enabled.png index 9b1b141502f..ba260e308c5 100644 Binary files a/DevCenter/Mobile/Media/mobile-schedule-job-enabled.png and b/DevCenter/Mobile/Media/mobile-schedule-job-enabled.png differ diff --git a/DevCenter/Mobile/Media/mobile-schedule-job-script-new.png b/DevCenter/Mobile/Media/mobile-schedule-job-script-new.png index 688436684f1..9c0f22f14ca 100644 Binary files a/DevCenter/Mobile/Media/mobile-schedule-job-script-new.png and b/DevCenter/Mobile/Media/mobile-schedule-job-script-new.png differ diff --git a/DevCenter/Mobile/Media/mobile-schedule-job-script.png b/DevCenter/Mobile/Media/mobile-schedule-job-script.png index fbd23cf0d47..8a4a09232c5 100644 Binary files a/DevCenter/Mobile/Media/mobile-schedule-job-script.png and b/DevCenter/Mobile/Media/mobile-schedule-job-script.png differ diff --git a/DevCenter/Mobile/Media/mobile-services-selection.png b/DevCenter/Mobile/Media/mobile-services-selection.png index 2a0550cec15..fae758cc973 100644 Binary files a/DevCenter/Mobile/Media/mobile-services-selection.png and b/DevCenter/Mobile/Media/mobile-services-selection.png differ diff --git a/DevCenter/Mobile/Tutorials/mobile-services-command-prompt-administration.md b/DevCenter/Mobile/Tutorials/mobile-services-command-prompt-administration.md new file mode 100644 index 00000000000..e3cabde7f90 --- /dev/null +++ b/DevCenter/Mobile/Tutorials/mobile-services-command-prompt-administration.md @@ -0,0 +1,62 @@ + + +
Azure Chunk Displayer
No macro content available for WYSIWYG editing
+ +# 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. + + +## Next Steps + +* [Mobile Services server script reference] +
Learn more about registering and using server scripts. + + +[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 + + +[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 + + +[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/ diff --git a/DevCenter/Mobile/Tutorials/mobile-services-schedule-jobs.md b/DevCenter/Mobile/Tutorials/mobile-services-schedule-jobs.md index cabe05f91e1..f3ccf8a7e45 100644 --- a/DevCenter/Mobile/Tutorials/mobile-services-schedule-jobs.md +++ b/DevCenter/Mobile/Tutorials/mobile-services-schedule-jobs.md @@ -2,61 +2,33 @@
Azure Chunk Displayer
No macro content available for WYSIWYG editing
-# 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. - -

Update scriptsUpdate Channel table scripts

+

Create new tableCreate the new Updates table

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. - -
Note -

To restore the original functionality of the push notifications tutorial, replace the copied insert function code.

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

Generate duplicatesInsert data to create duplicate channels

+ 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.

Create a new jobCreate a new scheduled job

@@ -68,55 +40,91 @@ Now that you have disabled duplicate checking on the Channel table, you can run

When you run your mobile service in free mode, you are only able to run one scheduled job at a time. In reserved mode, you can run up to ten scheduled jobs at a time.

-3. In the scheduler dialog, enter cleanup_channels for the **Job Name**, set the schedule interval and units, then click the check button. +3. In the scheduler dialog, enter getUpdates 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. ## Next Steps @@ -124,27 +132,22 @@ Congratulations, you have successfully created a new job schedule in your mobile
Learn more about registering and using server scripts. -[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 -[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 -[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/