dshearer / jobber

An alternative to cron, with sophisticated status-reporting and error-handling
https://dshearer.github.io/jobber/
MIT License
1.4k stars 79 forks source link

Angle brackets (“<” and “>”) are encoded as u003c and u003e in JSON output #316

Open ckotte opened 4 years ago

ckotte commented 4 years ago

I get this..

{
    "job": {
        "command": "/backup/container-backup/container-backup.sh -p syncthing-data u003eu003e /home/jobber/backup-syncthing-data.log 2u003eu003e /home/jobber/backup-syncthing-data.log",
        "name": "backup_syncthing-data",
        "status": "Good",
        "time": "0 0 3 * * *"
    },
    "startTime": "Sep 14 03:00:00 2020",
    "stderr": "",
    "stderr_base64": false,
    "stdout": "",
    "stdout_base64": false,
    "succeeded": true,
    "user": "root"
}

This is the "prettified" JSON output, but it looks similar when I don't use

#!/bin/bash
...
read stdin
...
json=$(echo "$stdin" | python3 -m json.tool)
sendmail ${MAIL_ADDRESS} <<MAIL_END
Subject: ${subject}
From: jobber <${MAIL_ADDRESS}>

$text

$json
MAIL_END

.. but the command is /backup/container-backup/container-backup.sh -p syncthing-data >> /home/jobber/backup-syncthing-data.log 2>> /home/jobber/backup-syncthing-data.log

I read that this behavior can be disabled in "Go's JSON marshaller" (https://discuss.elastic.co/t/beats-encodes-angle-brackets-and-as-u003c-and-u003e-in-json-output/60667).

ckotte commented 3 years ago

While trying to implement json.NewEncoder I found out that this is already implemented in func SerializeRunRec and that this is controlled via the undocumented variable runRecFormatVersion.

I can fix this by using runRecFormatVersion: 1.4:

  test:
    cmd: ls / >> ~/log/test.log
    time: '0 */1 * * * *'
    onError: Continue
    notifyOnSuccess:
      - type: program
        path: /usr/local/bin/send_jobber_email.sh
        runRecFormatVersion: 1.4
    notifyOnError:
      - type: program
        path: /usr/local/bin/send_jobber_email.sh
        runRecFormatVersion: 1.4

Before:

{
    "job": {
        "command": "ls / u003eu003e ~/log/test.log",
        "name": "letsencrypt_renewal_test",
        "status": "Good",
        "time": "0 */1 * * * *"
    },
    "startTime": "Oct 17 18:27:00 2020",
    "stderr": "",
    "stderr_base64": false,
    "stdout": "",
    "stdout_base64": false,
    "succeeded": true,
    "user": "jobber"
}

After:

{
    "fate": 0,
    "job": {
        "command": "ls / >> ~/log/test.log",
        "name": "letsencrypt_renewal_test",
        "status": "Good",
        "time": "0 */1 * * * *"
    },
    "startTime": 1602952140,
    "stderr": "",
    "stdout": "",
    "succeeded": true,
    "user": "jobber",
    "version": "1.4"
}

However, the startTime is in Unix format now and a few new keys are added as well.

@dshearer I guess runRecFormatVersion is not documented because func SerializeRunRec and the new RunRec are still WIP?