Giter VIP home page Giter VIP logo

gocra's Introduction

gocra

gocra is a port to Cipres Restful API (CRA) using golang.

When using gocra alway consut the CRA User Guide along with gocra documentation.

gocra is capable of submmiting jobs, monitoring jobs status, download results and delete jobs.

You need a cra_auth.json file (the source code contais an example that can be edited) in your user home folder containing you credentials used to login to Cipres API.

{
    "url": "https://cipresrest.sdsc.edu/cipresrest/v1",
    "appid": "Your application ID",
    "username": "Your Username",
    "password": "Your Password"
}

Here is an example of gocran package usage:

package main

import (
    "fmt"
    "log"
    "os"
    "time"

    "github.com/Godrigos/gocra"
)

func main() {

    var jr gocra.JobResults
    var total float64
    jl := gocra.ListJobs()
    count := len(jl.Jobs.Jobstatus)

    // Ends the execution if there is no job on server.
    if count == 0 {
        fmt.Println("There are no jobs on Cipres servers.")
        os.Exit(0)
    }

    for i := range jl.Jobs.Jobstatus {
        job := gocra.JobStat(jl.Jobs.Jobstatus[i].SelfURI.URL)
        wdir := gocra.WorkDir(job)
        jr = gocra.JobResult(job)
        // Destination folder for the downloaded files
        destDir := "/home/user/Downloads"

        /* Based on job state passed by jobStage field from a JobStatus structure
           prints to stdout the actual job stage and download its results if the
           job is completed. */
        switch {
        case job.JobStage == "QUEUE":
            fmt.Printf("The job \033[41;1;37m%s\033[0m has been validated"+
                " and placed in CIPRES's queue.\n", job.Metadata.Entry.Value)
            fmt.Printf("  \u25CF Job Handle: %s.\n", job.JobHandle)

        case job.JobStage == "COMMANDRENDERING":
            fmt.Printf("The job \033[41;1;37m%s\033[0m has reached the head"+
                " of the queue and CIPRES has created the command line that"+
                " will be run.\n", job.Metadata.Entry.Value)
            fmt.Printf("  \u25CF Job Handle: %s.\n", job.JobHandle)

        case job.JobStage == "INPUTSTAGING":
            fmt.Printf("CIPRES has created a temporary working directory"+
                " for the job \033[41;1;37m%s\033[0m on the execution host"+
                " and copied the input files over.\n",
                job.Metadata.Entry.Value)
            fmt.Printf("  \u25CF Job Handle: %s.\n", job.JobHandle)

        case job.JobStage == "SUBMITTED":
            fmt.Printf("The job \033[41;1;37m%s\033[0m has been submited"+
                " to the scheduler on the execution host.\n",
                job.Metadata.Entry.Value)
            fmt.Printf("  \u25CF Job Handle: %s.\n", job.JobHandle)
            fmt.Println("  \u25cf Working Directory Files List:")
            for j := range wdir.Jobfiles.Jobfile {
                fmt.Printf("      %-25s %12.2f kB\n",
                    wdir.Jobfiles.Jobfile[j].Filename,
                    float64(wdir.Jobfiles.Jobfile[j].Length)/1000)
                total += float64(wdir.Jobfiles.Jobfile[j].Length) / 1000
            }
            fmt.Printf("  \u25CF Total size %29.2f kB\n\n", total)

        case job.JobStage == "LOAD_RESULTS":
            fmt.Printf("The job \033[41;1;37m%s\033[0m has finished running"+
                " on the execution host and CIPRES has begun to transfer the"+
                " results.\n", job.Metadata.Entry.Value)
            fmt.Printf("  \u25CF Job Handle: %s.\n", job.JobHandle)

        case job.JobStage == "COMPLETED":
            fmt.Printf("Job \033[41;1;37m%s\033[0m results successfully"+
                " transferred and available.\n", job.Metadata.Entry.Value)
            fmt.Printf("  \u25CF Job Handle: %s.\n", job.JobHandle)
            fmt.Println("  \u25cf Results Files List:")
            for j := range jr.Jobfiles.Jobfile {
                fmt.Printf("      %-25s %12.2f kB\n",
                    jr.Jobfiles.Jobfile[j].Filename,
                    float64(jr.Jobfiles.Jobfile[j].Length)/1000)
                total += float64(jr.Jobfiles.Jobfile[j].Length) / 1000
            }
            fmt.Printf("\nDownloading files of job %s (%.2f MB)...\n",
                job.Metadata.Entry.Value, total/1000)
            // Parallel downloading
            gocra.DownloadAll(jr, job, destDir)
            gocra.Delete(job.SelfURI.URL)

        default:
            fmt.Println("Can not define job state. Try again later!")
        }

        /* When there is more than one job on CIPRES server, applies a delay
           between jobs access, acconding to the MinPollIntervalSeconds
           field from a JobStatus structure */
        if count > 1 {
            time.Sleep(time.Duration(job.MinPollIntervalSeconds) * time.Second)
            count--
        }
    }
}

gocra's People

Contributors

godrigos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.