static public void Execute()
        {
            Console.WriteLine("AuthJobExecuteScript - start");

            //
            // 1. Connect to the DeployR Server
            //
            RClient rClient = Utility.Connect();

            //
            // 2. Authenticate the user
            //
            RUser rUser = Utility.Authenticate(rClient);

            //
            // 3. Submit a background job for execution based on a
            // repository-managed R script: /testuser/root/Histogram of Auto Sales.R
            //
            JobExecutionOptions options = new JobExecutionOptions();

            options.priority = JobExecutionOptions.HIGH_PRIORITY;  //Make this a High Priority job
            RJob rJob = rUser.submitJobScript("Background Script Execution",
                                              "Background script execution.",
                                              "Histogram of Auto Sales",
                                              "root",
                                              "testuser",
                                              "",
                                              options);

            Console.WriteLine("AuthJobExecuteScript: submitted background job " +
                              "for execution, rJob=" + rJob);

            //
            // 4. Query the execution status of a background job and loop until the job has finished
            //
            if (rJob != null)
            {
                while (true)
                {
                    String sMsg = rJob.query().status.Value;

                    if (sMsg == RJob.Status.COMPLETED.Value |
                        sMsg == RJob.Status.FAILED.Value |
                        sMsg == RJob.Status.CANCELLED.Value |
                        sMsg == RJob.Status.ABORTED.Value)
                    {
                        break;
                    }
                    else
                    {
                        Thread.Sleep(500);
                    }
                }
            }


            //
            // 5. Retrieve the project from completed job
            //
            RProject rProject = null;

            if (rJob != null)
            {
                // make sure we have a valid project id
                if (rJob.query().project.Length > 0)
                {
                    //get the project using the project id
                    rProject = rUser.getProject(rJob.query().project);

                    Console.WriteLine("AuthJobExecuteScript: retrieved background " +
                                      "job result on project, rProject=" + rProject);
                }
            }

            //
            //  6. Cleanup
            //
            if (rProject != null)
            {
                rProject.close();
                //rProject.delete();  //un-comment if you wish to delete the project
            }

            if (rJob != null)
            {
                //rJob.delete();  //un-comment if you wish to delete the job
            }

            Utility.Cleanup(rUser, rClient);

            Console.WriteLine("AuthJobExecuteScript - end");
        }
        static public void Execute()
        {
            Console.WriteLine("AuthJobStoreResultToRepository - start");

            //
            // 1. Connect to the DeployR Server
            //
            RClient rClient = Utility.Connect();

            //
            // 2. Authenticate the user
            //
            RUser rUser = Utility.Authenticate(rClient);

            //
            // 3. Submit a background job to execute a block of R code
            // that will generate a vector object. We will then use
            // JobExecutionOtpions to request the following behavior:
            //
            // a. Execute the job at low (default) priority.
            // b. Skip the persistence of results to a project.
            // c. Request the persistence of a named vector object
            // as a binary R object file to the repository.
            //

            String rCode = "tutorialVector <- rnorm(100)";

            JobExecutionOptions options = new JobExecutionOptions();

            options.priority  = JobExecutionOptions.LOW_PRIORITY;
            options.noProject = true;

            //
            // 4. Use ProjectStorageOptions to identify the name of one
            // or more workspace objects for storage to the repository.
            //
            // In this case, the named object matches the name of the
            // object created the rCode being executed on the job. We
            // request that the binary R object file be stored into
            // the root directory in the repository.
            //
            options.storageOptions           = new ProjectStorageOptions();
            options.storageOptions.objects   = "tutorialVector";
            options.storageOptions.directory = "root";

            RJob rJob = rUser.submitJobCode("Background Code Execution",
                                            "Demonstrate storing job results to repository.",
                                            rCode,
                                            options);

            Console.WriteLine("AuthJobStoreResultToRepository: submitted background job " +
                              "for execution, rJob=" + rJob);

            //
            // 5. Query the execution status of a background job and loop until the job has finished
            //
            if (rJob != null)
            {
                while (true)
                {
                    String sMsg = rJob.query().status.Value;

                    if (sMsg == RJob.Status.COMPLETED.Value |
                        sMsg == RJob.Status.FAILED.Value |
                        sMsg == RJob.Status.CANCELLED.Value |
                        sMsg == RJob.Status.ABORTED.Value)
                    {
                        break;
                    }
                    else
                    {
                        Thread.Sleep(500);
                    }
                }
            }


            //
            // 6. Retrieve the RepositoryFile from completed job
            //
            RRepositoryFile rRepositoryFile = null;

            if (rJob != null)
            {
                //
                // 7. Retrieve the results of the background job
                // execution. Given the custom JobExecutionOptions
                // specified for this job (noproject=true) there will
                // be no RProject to hold the results.
                //
                // However, the custom JobExecutionOptions specified
                // for this job requested the storage of the
                // "tutorialVector" vector object as a binary R
                // object file (.rData) in the repository.
                //
                // The following code demonstrates how we can
                // retrieve that result from the repository:
                //
                String sUserName = rUser.about().Username;
                rRepositoryFile = rUser.fetchFile("tutorialVector.rData",
                                                  sUserName,
                                                  "root",
                                                  "");

                Console.WriteLine("AuthJobStoreResultToRepository: retrieved background " +
                                  "job result from repository, rRepositoryFile=" + rRepositoryFile);
            }

            //
            //  8. Cleanup
            //
            if (rRepositoryFile != null)
            {
                //rRepositoryFile.delete();  //un-comment if you wish to delete the RepositoryFile
            }

            if (rJob != null)
            {
                //rJob.delete();  //un-comment if you wish to delete the job
            }

            Utility.Cleanup(rUser, rClient);

            Console.WriteLine("AuthJobStoreResultToRepository - end");
        }
        static public void Execute()
        {
            Console.WriteLine("AuthJobExecuteCode - start");

            //
            // 1. Connect to the DeployR Server
            //
            RClient rClient = Utility.Connect();

            //
            // 2. Authenticate the user
            //
            RUser rUser = Utility.Authenticate(rClient);

            //
            // 3. Submit a background job for execution based on an
            // arbitrary block of R code: [codeBlock]
            //
            String codeBlock = "demo(graphics)";

            JobExecutionOptions options = new JobExecutionOptions();

            options.priority = JobExecutionOptions.MEDIUM_PRIORITY;   //Make this a Medium Priority job

            RJob rJob = rUser.submitJobCode("Sample Job",
                                            "Sample description.",
                                            codeBlock,
                                            options);

            Console.WriteLine("AuthJobExecuteCode: submitted background job for execution, rJob=" + rJob);

            //
            // 4. Query the execution status of a background job and loop until the job has finished
            //
            if (rJob != null)
            {
                while (true)
                {
                    String sMsg = rJob.query().status.Value;

                    if (sMsg == RJob.Status.COMPLETED.Value |
                        sMsg == RJob.Status.FAILED.Value |
                        sMsg == RJob.Status.CANCELLED.Value |
                        sMsg == RJob.Status.ABORTED.Value)
                    {
                        break;
                    }
                    else
                    {
                        Thread.Sleep(500);
                    }
                }
            }


            //
            // 5. Retrieve the project from completed job
            //
            RProject rProject = null;

            if (rJob != null)
            {
                // make sure we have a valid project id
                if (rJob.query().project.Length > 0)
                {
                    //get the project using the project id
                    rProject = rUser.getProject(rJob.query().project);

                    Console.WriteLine("AuthJobExecuteCode: retrieved background " +
                                      "job result on project, rProject=" + rProject);
                }
            }

            //
            //  6. Cleanup
            //
            if (rProject != null)
            {
                rProject.close();
                //rProject.delete();  //un-comment if you wish to delete the project
            }

            if (rJob != null)
            {
                //rJob.delete();  //un-comment if you wish to delete the job
            }

            Utility.Cleanup(rUser, rClient);

            Console.WriteLine("AuthJobExecuteCode - end");
        }
            public void onTaskCompleted(RTask rTask, RTaskResult rTaskResult)
            {
                Console.WriteLine("onTaskCompleted: " + rTask + ", result: " + rTaskResult);

                /*
                 * Retrieve Job identifier from RTaskResult.
                 */
                String jobID = rTaskResult.getID();

                Console.WriteLine("onTaskCompleted: " + rTask + ", background Job ID: " + jobID);

                if (m_rBroker != null)
                {
                    /*
                     * Important:
                     *
                     * To handle the results of a Background RTask you
                     * must transition from using the RBroker Framework
                     * API to using the .NET Client Library API.
                     */
                    RUser rUser = m_rBroker.owner();

                    if (rUser != null)
                    {
                        try
                        {
                            RJob rJob = rUser.queryJob(jobID);

                            Console.WriteLine("onTaskCompleted: " + rTask + ", rJob: " + rJob);

                            /*
                             * Next handle the result of the RJob as appropriate.
                             * In this example, simly cancel and delete the job.
                             */

                            try
                            {
                                rJob.cancel();
                            }
                            catch (Exception cex)
                            {
                                Console.WriteLine("rJob.cancel ex=" + cex.ToString());
                            }
                            try
                            {
                                rJob.delete();
                            }
                            catch (Exception dex)
                            {
                                Console.WriteLine("rJob.delete ex=" + dex.ToString());
                            }
                        }
                        catch (Exception jex)
                        {
                            Console.WriteLine("rUser.queryJob ex=" + jex.ToString());
                        }
                    }

                    m_rBroker.shutdown();
                    Console.WriteLine("BackgroundBasics: rBroker has been shutdown.");
                }
            }
Example #5
0
        /// <summary>
        /// Run the acutal background task using Job API
        /// </summary>
        /// <returns>Results of the background task</returns>
        /// <remarks></remarks>
        public RTaskResult call() 
        {


            RTaskResult taskResult = null;
            RJob rJob = null;

            long timeOnCall = 0L;
            //long timeOnServer = 0L;

            try 
            {

                long startTime = Environment.TickCount;

                JobExecutionOptions options = ROptionsTranslator.translate(m_task.options, m_isPriorityTask);

                if(m_task.code != "") 
                {
                    rJob = m_rUser.submitJobCode(m_task.name,
                                        m_task.description,
                                        m_task.code,
                                        options);
                } 
                else
                {
                    if(m_task.external != "") 
                    {
                        rJob = m_rUser.submitJobExternal(m_task.external,
                                        m_task.description,
                                        m_task.code,
                                        options);
                    } 
                    else 
                    {

                        rJob = m_rUser.submitJobScript(m_task.name,
                                          m_task.description,
                                          m_task.filename,
                                          m_task.directory,
                                          m_task.author,
                                          m_task.version,
                                          options);
                    }
                }

                timeOnCall = Environment.TickCount - startTime;

                taskResult = new RTaskResultImpl(rJob.about().id,
                                                 RTaskType.BACKGROUND,
                                                 true,
                                                 0L,
                                                 0L,
                                                 timeOnCall,
                                                 null);

            } 
            catch (Exception ex) 
            {

                //if(ex. instanceof InterruptedException) 
                //{
                    try 
                    {
                        /*
                         * If RTaskToken.cancel() call raises InterruptedException
                         * then ensure any corresponding scheduled RJob is
                         * also cancelled.
                         */
                        rJob.cancel();
                    } 
                    catch(Exception iex) 
                    {
                        throw new Exception("RBroker: could not cancel job, cause:  " + iex.ToString());
                    }
                //}

                taskResult = new RTaskResultImpl(null,
                                                 RTaskType.BACKGROUND,
                                                 false,
                                                 0L,
                                                 0L,
                                                 0L,
                                                 ex);
            } 
            finally 
            {

                /*
                 * Callback to PooledTaskBroker to release
                 * RProject back into pool for other tasks.
                 */
                m_rBroker.callback(m_task, taskResult);
            }

            return taskResult;
        }