locked
Help with background task, system trigger, app on lockscreen

    Question

  • Hello, I have a problem in my WinJS app, I have a backgroundtask.js code that runs on system trigger (app is on lock screen) After several iterations the feeling is that the code is not running anymore. Debugging did not yeld any results. Here's the general description of what goes on in there

    Task registration

    1.       When app first starts it registers the background task to run every 15 minutes.  Task executable is JS file bgtask.js

    2.       On next runs the task is not registered anymore (avoid duplication)

    Task execution

    1.       To use SharePoint JS Client Object Model we must emulate a browser.

    2.       Inside a bgtask.js we create a web worker object which emulates browser.

    3.       With that object we make calls to SharePoint

    a.       Authentication

    b.      Getting assignment data

    c.       Getting URLs of SharePoint sites in which to check for discussions

    d.      Check URLs for new discussions

    e.      Check new discussions for new replies (a new discussion is either just a discussion object modified, or it has a new reply, we have to check both)

    f.        New discussions are stored in the internal settings of the app and will be displayed in all live update iterations until user opens the main app

    4.       On all steps of 3. There are handlers to catch success and failure states. Success – logic continues, failure – task closes (actually in the version I sent you task does not close – instead it tries to call the last method – scheduling of tiles, but this method has try-catch statements which if error occurs – close the bgtask instance)

    5.       After data is get, the last method schedules tile updates for next 15 minutes with interval ~15 seconds and closes the instance

    6.       On next bgtask instance run the tile is cleaned of all previous updates (the last ones could still be showing, we don’t want that, we want new updates to appear) – this is how I can see – was the next instance started or no, if at some point live tile becomes blank then the instance is fired

    Main application and BG code interaction

    1.       As mention in task execution section, bgtask.js code will store info about new discussions in an internal app array (persistent between main app and bgtask.js iterations)

    2.       In each bgtask instance discussion lists are queried for changes that happened in last 15 minutes, but if there are some discussions from previous instances of bgtask in the internal array – those are also displayed in current live tile schedulted notifications

    3.       When user actually opens the main app – this array is nullified, marking those discussions as “viewed”

    the section that registers the task is like this:

          var taskRegistered = false;
            var exampleTaskName = Windows.Storage.ApplicationData.current.localSettings.values.currentBgTaskName == undefined ? "nxBgTask" :
                Windows.Storage.ApplicationData.current.localSettings.values.currentBgTaskName;

            var background = Windows.ApplicationModel.Background;
            var iter = background.BackgroundTaskRegistration.allTasks.first();
            while (iter.hasCurrent) {
                var task = iter.current.value;
                if (task.name === exampleTaskName) {
                    //taskRegistered = true;
                    task.unregister(true);
                    break;
                }
                iter.moveNext();
            }

            if (!taskRegistered) {
                var taskName = uniqueTaskName();
                Windows.Storage.ApplicationData.current.localSettings.values.currentBgTaskName = taskName;
                var taskEntryPoint = "backgroundtask.js";
                var builder = new Windows.ApplicationModel.Background.BackgroundTaskBuilder();
                builder.name = taskName;
                builder.taskEntryPoint = taskEntryPoint;
                builder.setTrigger(new Windows.ApplicationModel.Background.TimeTrigger(15, false));

                var task = builder.register();
                attachProgressAndCompletedHandlers(task);
            }

    Thursday, May 30, 2013 5:56 PM

Answers

  • Have you have enabled debug tracing? To enable debug tracing of background tasks, use the following steps:

    1. Open event viewer by pressing the Windows button and searching for eventvwr.exe.
    2. Go to Application and Services Logs -> Microsoft -> BackgroundTaskInfrastructure in the event viewer.
    3. In the actions pane, select View-> Show Analytic and Debug Logs to enable the diagnostic logging.
    4. Select the Diagnostic log and click Enable Log.
    5. Now try activating the background tasks again.
    6. View the Diagnostic logs for detailed error information. 

    Each app on the lock screen receives 2 seconds of CPU time and 0.469 Mb data limit every 15 min as a quota. Refer this for CPU quota or network data usage quota and other useful things.

    I have an impression one complete execution of your background task is taking more than your allowed quota [so getting suspended] and hence you have a feeling that it never runs to completion. 

    In that case I think Push notification is a solution. Configure a Azure Mobile Service to do periodic check and if there is something of interest it should send push notification.


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you.

    Happy Coding!!!


    Saturday, June 1, 2013 3:30 PM

All replies

  • Have you have enabled debug tracing? To enable debug tracing of background tasks, use the following steps:

    1. Open event viewer by pressing the Windows button and searching for eventvwr.exe.
    2. Go to Application and Services Logs -> Microsoft -> BackgroundTaskInfrastructure in the event viewer.
    3. In the actions pane, select View-> Show Analytic and Debug Logs to enable the diagnostic logging.
    4. Select the Diagnostic log and click Enable Log.
    5. Now try activating the background tasks again.
    6. View the Diagnostic logs for detailed error information. 

    Each app on the lock screen receives 2 seconds of CPU time and 0.469 Mb data limit every 15 min as a quota. Refer this for CPU quota or network data usage quota and other useful things.

    I have an impression one complete execution of your background task is taking more than your allowed quota [so getting suspended] and hence you have a feeling that it never runs to completion. 

    In that case I think Push notification is a solution. Configure a Azure Mobile Service to do periodic check and if there is something of interest it should send push notification.


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you.

    Happy Coding!!!


    Saturday, June 1, 2013 3:30 PM
  • Thanks Vishal for your reply. We are investigating the issue further and when done, Oleg will update this. We are trying to optimize the code so "canceled" event is not triggered by Win.
    Sunday, June 2, 2013 7:42 PM
  • Hi Vishal,

    Still our app throws some errors like:

    The background task with entry point backgroundtask-simplified.js and name f2416a11-105e-4050-818e-96b23081fb58 failed to activate with error code 0x80010008.

    Background task for package NeoxenLearningGateway_1.0.0.19_x86__waqkqstjem8pa with entry point backgroundtask-simplified.js did not complete in response to a cancel notification.

    As far as I see the error code means:

    0x80010008 RPC_E_CLIENT_DIED The caller (client) disappeared while the callee(server) was processing a call

    So the task was killed while waiting for some SharePoint response probably. Is there any work around this or we should abandon this and look at Push notification. What do you think?

    Thursday, June 6, 2013 6:10 PM
  • It appears still your task is taking longer to complete and what you have mentioned as error "not complete in response to a cancel notification." tells me that you are not properly handling task cancellation. Please read this How to handle a cancelled background task (Windows Store apps using JavaScript and HTML)

    If the background task does not respond to the cancellation notification and does not return from its background task within 5 seconds, the app is terminated. To ensure a graceful exit and ensure consistent app state, apps are encouraged to register a cancellation handler and save state and exit the background task in response to a cancellation request.

    I think carefully reading Guidelines and checklist for background tasks will help you a lot.


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you.

    Happy Coding!!!



    Friday, June 7, 2013 8:04 AM
  • thanks for your reply Vishal. Your feedback is greatly appreciated

    What I was thinking as I am not a Win8 guru, we need something reliable for production. Even if we now don't treat right that cancel notification, doesn't make much difference at high level as the bkg task will still be killed, as it is consuming too many resources right? So we can not be sure that at any point the users will see up to date info on the tile. 

    We need a reliable way to get the updated info to the users and as I see it (again without being a specialist here) with those limitations (2sec CPU time,etc) we are at risk. What do you think?

    Friday, June 7, 2013 12:29 PM
  • I completely understand your concern and this limitation. With your issue,

    1) it is primarily push notification that can help.

    2) If this is a LOB app (that too not in app store) and you are not looking to support ARM devices then you can write a Windows service to take this load from your app and directly populate a file in App's local storage folder, then a background task can simply be timed to load it and update live tile.

    3) you can move decision making to your actual app server in from of WCF, if that service is optimized enough then it can also help you. Optimized meaning your background task should just get processed data to be used as it is.


    -- Vishal Kaushik --

    Please 'Mark as Answer' if my post answers your question and 'Vote as Helpful' if it helps you.

    Happy Coding!!!





    Friday, June 7, 2013 4:55 PM
  • It probably will go in app store so I guess the final decision here is to use Push notifications.

    thanks Vishal again for your help, much appreciated.

    Saturday, June 8, 2013 12:27 PM