def update_pidfile(pidfile):
    """Update pidfile.

    Notice:
        We should call this function only after we have successfully acquired
        a lock and never before. It exits main program if it fails to parse
        and/or write pidfile.

    Arguments:
        pidfile (str): pidfile to update

    """
    try:
        with open(pidfile, mode='r') as _file:
            pid = _file.read(1024).rstrip()

        try:
            pid = int(pid)
        except ValueError:
            print("cleaning stale pidfile with invalid data:'{}'".format(pid))
            write_pid(pidfile)
        else:
            if running(pid):
                # This is to catch migration issues from 0.7.x to 0.8.x
                # version, where old process is still around as it failed to
                # be stopped. Since newer version has a different locking
                # mechanism, we can end up with both versions running.
                # In order to avoid this situation we refuse to startup.
                sys.exit("process {} is already running".format(pid))
            else:
                # pidfile exists with a PID for a process that is not running.
                # Let's update PID.
                print("updating stale processID({}) in pidfile".format(pid))
                write_pid(pidfile)
    except FileNotFoundError:
        # Either it's 1st time we run or previous run was terminated
        # successfully.
        print("creating pidfile {f}".format(f=pidfile))
        write_pid(pidfile)
    except OSError as exc:
        sys.exit("failed to update pidfile:{e}".format(e=exc))
Update pidfile.

    Notice:
        We should call this function only after we have successfully acquired
        a lock and never before. It exits main program if it fails to parse
        and/or write pidfile.

    Arguments:
        pidfile (str): pidfile to update