# Buggy code
def handle(self, *args, **options):
    interval = float(options.get("interval") or DEFAULT_PING_INTERVAL)
    checkrate = float(options.get("checkrate") or DEFAULT_PING_CHECKRATE)
    server = options.get("server") or DEFAULT_SERVER_URL

    self.started = datetime.now()

    while True:
        try:
            logger.info("Attempting a ping.")
            with vacuum_db_lock:
                data = self.perform_ping(server)
                logger.info("Ping succeeded! (response: {})".format(data))
                if "id" in data:
                    self.perform_statistics(server, data["id"])
            logger.info("Sleeping for {} minutes.".format(interval))
            time.sleep(interval * 60)
            continue
        except ConnectionError:
            logger.warn(
                "Ping failed (could not connect). Trying again in {} minutes.".format(
                    checkrate
                )
            )
        except Timeout:
            logger.warn(
                "Ping failed (connection timed out). Trying again in {} minutes.".format(
                    checkrate
                )
            )
        except RequestException as e:
            logger.warn(
                "Ping failed ({})! Trying again in {} minutes.".format(e, checkrate)
            )
        time.sleep(checkrate * 60)


# Traceback (TypeError)
INFO:kolibri.core.analytics.management.commands.ping:Ping succeeded! (response: {'id': 87135})
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/Users/d/Projects/learning_equality/repos/k011x/kolibri/utils/server.py", line 109, in run
call_command("ping")
File "/Users/d/Projects/learning_equality/repos/k011x/.venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 131, in call_command
return command.execute(*args, **defaults)
File "/Users/d/Projects/learning_equality/repos/k011x/.venv/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/d/Projects/learning_equality/repos/k011x/kolibri/core/analytics/management/commands/ping.py", line 58, in handle
self.perform_statistics(server, data["id"])
File "/Users/d/Projects/learning_equality/repos/k011x/kolibri/core/analytics/management/commands/ping.py", line 123, in perform_statistics
jsondata = dump_zipped_json(data)
File "/Users/d/Projects/learning_equality/repos/k011x/kolibri/core/analytics/utils.py", line 37, in dump_zipped_json
jsondata = json.dumps(data)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'bytes' is not JSON serializable

# Fixed code
def handle(self, *args, **options):
    interval = float(options.get("interval") or DEFAULT_PING_INTERVAL)
    checkrate = float(options.get("checkrate") or DEFAULT_PING_CHECKRATE)
    server = options.get("server") or DEFAULT_SERVER_URL
    once = options.get("once") or False

    self.started = datetime.now()

    while True:
        try:
            logger.info("Attempting a ping.")
            with vacuum_db_lock:
                data = self.perform_ping(server)
                logger.info("Ping succeeded! (response: {})".format(data))
                if "id" in data:
                    self.perform_statistics(server, data["id"])
            if once:
                break
            logger.info("Sleeping for {} minutes.".format(interval))
            time.sleep(interval * 60)
            continue
        except ConnectionError:
            logger.warn(
                "Ping failed (could not connect). Trying again in {} minutes.".format(
                    checkrate
                )
            )
        except Timeout:
            logger.warn(
                "Ping failed (connection timed out). Trying again in {} minutes.".format(
                    checkrate
                )
            )
        except RequestException as e:
            logger.warn(
                "Ping failed ({})! Trying again in {} minutes.".format(e, checkrate)
            )
        if once:
            break
        time.sleep(checkrate * 60)