Example #1
0
        void SenderWorker(object state)
        {
            SenderWorkerState sws = state as SenderWorkerState;

            if (sws == null)
            {
                Log(LogSeverity.Error, "Internal error - SenderWorker got a null state.");
                return;
            }

            string           filePath    = sws.WorkItemPath;
            SenderState      senderState = sws.State;
            JsonDeserializer des         = senderState.Deserializer;

            try {
                string fileText = HttpUtility.UrlDecode(File.ReadAllText(filePath));
                if (String.IsNullOrEmpty(fileText))
                {
                    Log(LogSeverity.Error, "Empty payload for item '{0}'", filePath);
                    PutWorkItemBack(filePath, senderState);
                    return;
                }

                if (!fileText.StartsWith("payload=", StringComparison.Ordinal))
                {
                    Log(LogSeverity.Error, "Invalid payload format for item '{0}'", filePath);
                    PutWorkItemBack(filePath, senderState);
                    return;
                }

                fileText = fileText.Substring(8);
                Push push = des.Deserialize <Push> (fileText);
                push.CHAuthID = senderState.CommitSourceID;
                Mailer mailer = new Mailer();
                if (mailer.Send(push))
                {
                    try {
                        File.Delete(filePath);
                    } catch (Exception ex) {
                        Log(LogSeverity.Warning, "Failed to delete work item '{0}', the mail message might be sent twice. Exception {1} was thrown: {2}",
                            filePath, ex.GetType(), ex.Message);
                    }

                    try {
                        CachingFetcher.RemoveCommitsFromCache(push.CHAuthID, push.Commits);
                    } catch (Exception ex) {
                        Log(ex, "Failed to clean up commit cache, some files may have been left behind. {2} ({1})");
                    }
                }
                else
                {
                    Log(LogSeverity.Info, "Mail not sent.");
                    PutWorkItemBack(filePath, senderState, (push.Commits == null || push.Commits.Count == 0));
                }
            } catch (Exception ex) {
                Log(ex, "Attempt to send work item '{4}' failed. Exception {1} was thrown: {2}", Path.GetFileName(filePath));
                PutWorkItemBack(filePath, senderState);
            }
        }
Example #2
0
        protected virtual void Application_Start(object sender, EventArgs e)
        {
            Config config = Config.Instance;

            Console.Error.WriteLine(config.Version + " starting");
            string configDir = WebConfigurationManager.AppSettings ["ConfigDirectory"];

            if (String.IsNullOrEmpty(configDir))
            {
                throw new InvalidOperationException("ConfigDirectory application setting must be present in web.config");
            }
            configDir = HostingEnvironment.MapPath(configDir);

            if (!Directory.Exists(configDir))
            {
                throw new InvalidOperationException("Config directory must exist.");
            }

            string configFile = Path.Combine(configDir, "CaptainHook.xml");

            if (!File.Exists(configFile))
            {
                throw new InvalidOperationException("Config file must exist.");
            }

            config.RootPath = configDir;
            config.LoadConfigFile(configFile);

            string csDataDir = Path.Combine(Config.Instance.RootPath, "data");

            if (Directory.Exists(csDataDir))
            {
                try {
                    string[] files = Directory.GetFiles(csDataDir, "*.json.processing", SearchOption.AllDirectories);
                    if (files != null && files.Length > 0)
                    {
                        string newFile;
                        foreach (string f in files)
                        {
                            newFile = f.Substring(0, f.Length - 11);
                            try {
                                File.Move(f, newFile);
                            } catch (Exception ex) {
                                Console.Error.WriteLine("Error moving {0} to {1}", f, newFile);
                                Console.Error.WriteLine(ex);
                            }
                        }
                    }
                } catch (Exception ex) {
                    Console.Error.WriteLine("Error processing stale files on application startup:");
                    Console.Error.WriteLine(ex);
                }
            }
            CachingFetcher.Init();
            periodicSender = new PeriodicSender();
            periodicSender.Init();
        }
Example #3
0
        public bool FetchDiff(Push push)
        {
            string url = null;

            try {
                string response = CachingFetcher.FetchDiff(push.CHAuthID, push.Repository.Owner.Name, push.Repository.Name, ID, out url);
                if (response == null)
                {
                    return(false);
                }

                var jdes    = new JsonDeserializer();
                var wrapper = jdes.Deserialize <CommitWithDiffJsonWrapper> (response);
                if (wrapper != null)
                {
                    var diff = wrapper.Commit;
                    if (!diff.FetchBlobs(push))
                    {
                        Log(LogSeverity.Error, "Failed to fetch blobs for commit '{0}' from URL '{1}'", ID, url);
                        return(false);
                    }
                    Diff = diff;
                }
                else
                {
                    Log(LogSeverity.Error, "Failed to fetch diff for commit '{0}' from URL '{1}'", ID, url);
                    return(false);
                }
            } catch (Exception ex) {
                Log(ex, "Exception while fetching diff for commit '{4}' from URL '{5}'\n{0}", ID, url);
                return(false);
            }

            CommitWithDiff ret = Diff;

            if (ret == null)
            {
                Log(LogSeverity.Info, "FetchDiff did not fail, but no diff retrieved?");
            }

            return(ret != null);
        }
Example #4
0
        Blob Fetch(string commitSource, string repositoryName, string repositoryOwner, string tree, string file)
        {
            string response;
            string url = null;

            try {
                response = CachingFetcher.FetchBlob(commitSource, repositoryOwner, repositoryName, tree, file, out url);
                var jdes    = new JsonDeserializer();
                var wrapper = jdes.Deserialize <BlobJsonWrapper> (response);
                if (wrapper != null)
                {
                    var blob = wrapper.Blob;
                    if (blob == null)
                    {
                        Log(LogSeverity.Error, "Empty blob for commit '{0}', file '{1}'", ID, file);
                    }
                    return(blob);
                }
                else
                {
                    Log(LogSeverity.Error, "Failed to fetch blob for commit '{0}', file '{1}'", ID, file);
                    return(null);
                }
            } catch (WebException ex) {
                var resp = ex.Response as HttpWebResponse;
                if (resp != null && resp.StatusCode == HttpStatusCode.NotFound)
                {
                    Log(ex, "A HTTP NotFound error while fetching blob for commit '{4}'. Failed URL was '{5}'.\n{0}",
                        ID, url);
                }
                else
                {
                    Log(ex);
                }
                return(null);
            } catch (Exception ex) {
                Log(ex);
                return(null);
            }
        }