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 static JobExecutionOptions translate(BackgroundTaskOptions taskOptions, Boolean isPriorityTask)
        {
            JobExecutionOptions options = null;

            if (taskOptions != null)
            {
                options = new JobExecutionOptions();

                /*
                 * BackgroundTaskOptions to JobExecutionOptions.
                 */

                if (isPriorityTask)
                {
                    options.priority = "HIGH_PRIORITY";
                }

                options.noProject = taskOptions.noProject;

                if (taskOptions.repeatCount > 0)
                {
                    options.schedulingOptions                = new JobSchedulingOptions();
                    options.schedulingOptions.repeatCount    = taskOptions.repeatCount;
                    options.schedulingOptions.repeatInterval = taskOptions.repeatInterval;
                    options.schedulingOptions.startTime      = taskOptions.startTime;
                }

                /*
                 * BackgroundTaskOptions to ProjectExecutionOptions.
                 */

                options.rinputs    = taskOptions.rinputs;
                options.csvrinputs = taskOptions.csvrinputs;

                if (taskOptions.preloadWorkspace != null)
                {
                    options.preloadWorkspace           = new ProjectPreloadOptions();
                    options.preloadWorkspace.filename  = taskOptions.preloadWorkspace.filename;
                    options.preloadWorkspace.directory = taskOptions.preloadWorkspace.directory;
                    options.preloadWorkspace.author    = taskOptions.preloadWorkspace.author;
                    options.preloadWorkspace.version   = taskOptions.preloadWorkspace.version;
                }

                if (taskOptions.preloadDirectory != null)
                {
                    options.preloadDirectory           = new ProjectPreloadOptions();
                    options.preloadDirectory.filename  = taskOptions.preloadDirectory.filename;
                    options.preloadDirectory.directory = taskOptions.preloadDirectory.directory;
                    options.preloadDirectory.author    = taskOptions.preloadDirectory.author;
                    options.preloadDirectory.version   = taskOptions.preloadDirectory.version;
                }

                options.preloadByDirectory = taskOptions.preloadByDirectory;

                options.graphicsDevice = taskOptions.graphicsDevice;
                options.graphicsWidth  = taskOptions.graphicsWidth;
                options.graphicsHeight = taskOptions.graphicsHeight;
                options.echooff        = taskOptions.echooff;
                options.consoleoff     = taskOptions.consoleoff;
                options.routputs       = taskOptions.routputs;
                options.encodeDataFramePrimitiveAsVector = taskOptions.encodeDataFramePrimitiveAsVector;
                options.nan      = taskOptions.nan;
                options.infinity = taskOptions.infinity;

                if (taskOptions.storageOptions != null)
                {
                    options.storageOptions            = new ProjectStorageOptions();
                    options.storageOptions.directory  = taskOptions.storageOptions.directory;
                    options.storageOptions.files      = taskOptions.storageOptions.files;
                    options.storageOptions.newVersion = taskOptions.storageOptions.newVersion;
                    options.storageOptions.objects    = taskOptions.storageOptions.objects;
                    options.storageOptions.published  = taskOptions.storageOptions.published;
                    options.storageOptions.workspace  = taskOptions.storageOptions.workspace;
                }
            }

            return(options);
        }
예제 #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;
        }