Ejemplo n.º 1
0
        public void DoWork()
        {
            GamervineDataContext dataContext = new GamervineDataContext();

            try
            {
                //Get all jobs where the user & gamertags are active and the next run time is in the past or now
                var jobs = from j in dataContext.Jobs
                           join gt in dataContext.Gamertags on j.TagId equals gt.TagId
                           join u in dataContext.Users on gt.UserId equals u.UserId
                           where gt.State == State.Active.GetHashCode() &&
                           u.State == State.Active.GetHashCode() &&
                           j.NextRunTime <= DateTime.UtcNow
                           select j;

                foreach (Job job in jobs)
                {
                    try
                    {
                        IJobHandler jobHandler = JobHandlerFactory.GetHandlerForType(job.Type);
                        string      post       = jobHandler.GetPost(dataContext, job);

                        if (!post.Equals(string.Empty))
                        {
                            var socialConnectors = from sc in dataContext.UserSocialConnections
                                                   join gtsc in dataContext.GamertagSocialConnections on sc.UserSocialConnectionId equals gtsc.UserSocialConnectionId
                                                   where gtsc.TagId == job.TagId
                                                   select sc;

                            if (socialConnectors.Count() > 0)
                            {
                                foreach (UserSocialConnection conn in socialConnectors)
                                {
                                    ISocialConnector socialConnector = SocialConnectorFactory.GetSocialConnectorForType(conn.Type);
                                    socialConnector.Post(conn, post);
                                }
                            }
                            else
                            {
                                //TODO: Perform logic to flag the account that it doesn't have social connectors and they should sign up
                            }

                            //Do any post work that the handler may need to do
                            jobHandler.PostWork(dataContext, job);

                            //Record the post history in the database
                            PostHistory ph = new PostHistory();
                            ph.PostDate      = DateTime.UtcNow;
                            ph.PostHistoryId = Guid.NewGuid().ToString();
                            ph.PostString    = post;
                            ph.PostType      = job.Type;
                            ph.TagId         = job.TagId;

                            dataContext.PostHistories.InsertOnSubmit(ph);
                        }

                        job.NextRunTime = job.CalculateNextRunTime();
                        job.LastRunTime = DateTime.UtcNow;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Exception occurred in PostService.DoWork processing Job ID #" + job.JobId + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
                    }
                    finally
                    {
                        try
                        {
                            dataContext.SubmitChanges();
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Exception occurred in PostService.DoWork -> dataContext.SubmitChanges for Job ID #" + job.JobId + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception occurred in PostService.DoWork:" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
            }
            finally
            {
                Debug.WriteLine("PostService DoWork completed at " + DateTime.Now.ToString());
            }
        }