0

I'm creating a scheduler using Golang's crontab and resty.v2 to call a POST api(Dropbox file upload api).
When I'm invoking the file upload method manually its working fine. But when the same method is invoked via the crontab scheduler its not making the rest call.
Interesting fact is that, it's neither throwing any error nor providing any rest call response.
PFB the scheduler code to invoke the upload method:

func StartJob() {
ctab := crontab.New()

for _, v := range strings.Split(os.Getenv("schedules"), commaSeparator) {
    match, _ := regexp.MatchString(regex, v)
    if match == true {
        hhmm := strings.Split(v, colonSeparator)
        expresion := fmt.Sprintf(cronExpression, hhmm[1], hhmm[0])
        ctab.MustAddJob(expresion, func() {
            go service.Upload(dropboxEndpoint, dropboxAccessToken, os.Getenv("hkpath"))
        })
    } else {
        fmt.Printf("Not acceptable time-format : %s\n", v)
    }
}}

And here is the upload method code:

func Upload(dropboxEndpoint string, dropboxAccessToken string, path string) {
_, fileName := filepath.Split(path)
fileBytes, fileErr := ioutil.ReadFile(path)

if fileErr == nil {
    fmt.Println(path)
    resp, restErr := client.R().
        SetBody(fileBytes).
        SetContentLength(true).
        SetHeader("Authorization", fmt.Sprintf("Bearer %s", dropboxAccessToken)).
        SetHeader("Dropbox-API-Arg", fmt.Sprintf("{\"path\": \"/home/f1/%s/%s\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}", os.Getenv("name"), fileName)).
        SetHeader("Content-Type", "application/octet-stream").
        Post(dropboxEndpoint)

    if restErr != nil {
        log.Fatal(restErr)
    } else {
        fmt.Println(resp)
    }
} else {
    log.Fatal(fileErr)
}}

Any idea what's the wrong I'm doing?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Abhinab Kanrar
  • 1,532
  • 2
  • 20
  • 46
  • can you share the value of environment variable `schedules`? – novalagung Jun 28 '19 at 10:20
  • for 3:53PM & 3:55PM i have set it as 15:53,15:55 – Abhinab Kanrar Jun 28 '19 at 10:23
  • One point to mention is that the schduler is working fine, i mean it's triggeing the job as it should – Abhinab Kanrar Jun 28 '19 at 10:24
  • so invoking the method manually is working fine, and scheduler is also working fine. sorry what was the problem again? – novalagung Jun 28 '19 at 10:29
  • You are correct. The method is working fine and the scheduler is working fine too(meaning it's invoking the upload method successfully). But the rest api call inside the method is failing without providing any response or error – Abhinab Kanrar Jun 28 '19 at 10:33
  • Please provide a [mre]. It is not obvious which cron implementation you are using. Assuming it is https://github.com/mileusna/crontab - worked with it yet, so just a wild guess, bit you are putting the code to be executed (which will most likely be run in a goroutine) inside another goroutine (`go service.Upload...`). Without the code, however, it is not easy to debug. – Markus W Mahlberg Jun 30 '19 at 11:30
  • I got the solution actually....in the main method I used for to block the shutdown which was causing the issue....now after using select instead of for it's working fine.Anyway, thanks for your points – Abhinab Kanrar Jun 30 '19 at 12:10

0 Answers0