public override void Run() { // This is a sample worker implementation. Replace with your logic. Trace.WriteLine("FacebookWorker entry point called", "Information"); var account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("DataConnectionString")); var queueClient = account.CreateCloudQueueClient(); var queue = queueClient.GetQueueReference(FriendLikesService.FRIEND_LIKES_QUEUE); while (true) { Thread.Sleep(10000); Trace.WriteLine("Working", "Information"); var msg = queue.GetMessage(TimeSpan.FromMinutes(5)); // Let's give us five minutes to process if (msg != null) { var p = msg.AsString.Split('%'); Trace.WriteLine("Processing " + p[0], "Information"); // TODO: start new thread var service = new FriendLikesService(p[0], p[1]); // TODO: we probably need a way to refresh the data from time to time: // - test existence of the response BLOB // - refresh if blob older than n days/hours if (service.isCached()) { Trace.WriteLine("Already in cache, skipping " + p[0], "Information"); } else { var watch = new Stopwatch(); watch.Start(); service.GetFriendsLikes(); service.SaveFriendLikes(); watch.Stop(); Trace.WriteLine("Done with " + p[0] + " time: " + watch.Elapsed, "Information"); } queue.DeleteMessage(msg); } } }
public ActionResult Index() { var fb = new FacebookWebClient(); dynamic me = fb.Get("me"); ViewBag.id = me.id; ViewBag.name = me.name; ViewBag.firstname = me.first_name; FriendLikesService service = new FriendLikesService(me.id, fb.AccessToken); ViewBag.webUrl = RoleEnvironment.GetConfigurationSettingValue("WebURL"); ViewBag.blobUrl = RoleEnvironment.GetConfigurationSettingValue("BlobURL"); if (service.isCached()) { //// The data is cached, we can retrieve it and use it server-side... //// The ViewBag is used by the default Index view //service.GetFriendsLikes(); //ViewBag.friendLikes = service.GetOrderedFriendLikes(); //var cats = service.GetCategories().OrderByDescending(k => k.Value).ToList(); //var max = cats.Max(k => k.Value); //var min = cats.Min(k => k.Value); //ViewBag.categories = cats; //ViewBag.max = max; //ViewBag.min = min; // Return the IndexJS view to use the cached data on the client return View("IndexJS"); } else { // Need to send this to the Worker Role service.QueueLike(); return View("PleaseWait"); } return View(); }
public ActionResult IsCached(string id) { var service = new FriendLikesService(id); return Json(service.isCached(), JsonRequestBehavior.AllowGet); }