Example #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Press [enter] to start ...");
            Console.ReadLine();

            try
            {
                ga = new GApplication(GConnection.FromConsole("localhost", "9000", "user", "user"));
                ga.ApplicationName = "Grid Reverser - Alchemi sample";

                ga.ThreadFinish += new GThreadFinish(JobFinished);
                ga.ApplicationFinish += new GApplicationFinish(ApplicationFinished);

                ga.Manifest.Add(new EmbeddedFileDependency("Reverse.exe", @"..\..\..\Reverse\bin\Debug\Reverse.exe"));

                for (int jobNum=0; jobNum<2; jobNum++)
                {
                    GJob job = new GJob();
                    string inputFileName = string.Format("input{0}.txt", jobNum);
                    string outputFileName = string.Format("output{0}.txt", jobNum);

                    job.InputFiles.Add(new EmbeddedFileDependency(inputFileName, @"..\..\" + inputFileName));
                    job.RunCommand = string.Format("Reverse {0} > {1}", inputFileName, outputFileName);
                    job.OutputFiles.Add(new EmbeddedFileDependency(outputFileName));

                    ga.Threads.Add(job);
                }

                ga.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.GetType() + " : " + e.Message);
                Console.ReadLine();
                return;
            }
            Console.WriteLine("Started .. Waiting for jobs to finish ..\n");
            Console.ReadLine();
        }
        //Gets the XML representing a job
        private static string XmlFromJob(GJob job)
        {
            XmlStringWriter xsw = new XmlStringWriter();

            xsw.Writer.WriteStartElement("job");
            xsw.Writer.WriteAttributeString("id", job.Id.ToString());
            xsw.Writer.WriteStartElement("input");
            xsw.Writer.WriteFullEndElement(); // close input
            xsw.Writer.WriteStartElement("work");
            xsw.Writer.WriteFullEndElement(); // close work
            xsw.Writer.WriteStartElement("output");

            foreach (EmbeddedFileDependency fileDep in job.OutputFiles)
            {
                xsw.Writer.WriteStartElement("embedded_file");
                xsw.Writer.WriteAttributeString("name", fileDep.FileName);
                xsw.Writer.WriteString(fileDep.Base64EncodedContents);
                xsw.Writer.WriteFullEndElement(); // close embedded_file element
            }

            /*
            //in the new API for GJob, the stderr, stdout are properties.
            //put them into XML as output elements
             */
            //stdout
            xsw.Writer.WriteStartElement("embedded_file");
            xsw.Writer.WriteAttributeString("name", "stdout.txt");
            xsw.Writer.WriteString(Utils.EncodeBase64(job.Stdout));
            xsw.Writer.WriteFullEndElement(); // close embedded_file element
            logger.Debug("Stdout " + job.Stdout);

            //stderr
            xsw.Writer.WriteStartElement("embedded_file");
            xsw.Writer.WriteAttributeString("name", "stderr.txt");
            xsw.Writer.WriteString(Utils.EncodeBase64(job.Stderr));
            xsw.Writer.WriteFullEndElement(); // close embedded_file element
            logger.Debug("Stderr " + job.Stderr);

            //log
            xsw.Writer.WriteStartElement("embedded_file");
            xsw.Writer.WriteAttributeString("name", "log.txt");
            xsw.Writer.WriteString(Utils.EncodeBase64(job.Log));
            xsw.Writer.WriteFullEndElement(); // close embedded_file element
            logger.Debug("Log " + job.Log);

            xsw.Writer.WriteEndElement(); // close output
            xsw.Writer.WriteEndElement(); // close task

            return xsw.GetXmlString();
        }
        //Gets the GJob object from the given xml
        private static GJob JobFromXml(int jobId, string jobXml)
        {
            // TODO: validate against schema
            GJob job = new GJob();

            job.SetId(jobId);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(jobXml);

            logger.Debug("Getting JobFromXML...");
            foreach (XmlNode inputFile in doc.SelectNodes("job/input/embedded_file"))
            {
                EmbeddedFileDependency dep = new EmbeddedFileDependency(inputFile.Attributes["name"].Value);
                dep.Base64EncodedContents = inputFile.InnerText;
                job.InputFiles.Add(dep);
                logger.Debug("adding input filedep:" + dep.FileName);
            }

            job.RunCommand = doc.SelectSingleNode("job/work").Attributes["run_command"].Value;

            logger.Debug("Job run command=" + job.RunCommand);

            foreach (XmlNode outputFile in doc.SelectNodes("job/output/embedded_file"))
            {
                EmbeddedFileDependency dep = new EmbeddedFileDependency(outputFile.Attributes["name"].Value);
                job.OutputFiles.Add(dep);
                logger.Debug("adding output filedep:" + dep.FileName);
            }

            return job;
        }
        //-----------------------------------------------------------------------------------------------
        //Gets the XML representing a job
        private static string XmlFromJob(GJob job)
        {
            XmlStringWriter xsw = new XmlStringWriter();

            xsw.Writer.WriteStartElement("job");
            xsw.Writer.WriteAttributeString("id", job.Id.ToString());
            xsw.Writer.WriteStartElement("input");
            xsw.Writer.WriteFullEndElement(); // close input
            xsw.Writer.WriteStartElement("work");
            xsw.Writer.WriteFullEndElement(); // close work
            xsw.Writer.WriteStartElement("output");

            foreach (EmbeddedFileDependency fileDep in job.OutputFiles)
            {
                xsw.Writer.WriteStartElement("embedded_file");
                xsw.Writer.WriteAttributeString("name", fileDep.FileName);
                xsw.Writer.WriteString(fileDep.Base64EncodedContents);
                xsw.Writer.WriteFullEndElement(); // close embedded_file element
            }

            xsw.Writer.WriteEndElement(); // close output
            xsw.Writer.WriteEndElement(); // close task

            return xsw.GetXmlString();
        }