0

Implements a scheduled task using Supercronic

Cron เป็นโปรแกรมสำหรับการทำ schedule tasks  ที่เราต้องการ พบเจอได้ใน Unix-like OS ตามไปอ่านรายละเอียดได้ที่นี่

Supercronic ก็จัดอยู่ในโปรแกรมข้างต้น คนทำให้นิยามว่า

Supercronic is a crontab-compatible job runner, designed specifically to run in containers.

เหตุผลที่ทำ Supercronic ขึ้นมาตามไปอ่านรายละเอียดได้ที่นี่ (ไม่ได้ขี้เกียจนะ 55+) ที่นี้เราลองมาอิมพลีเมนท์กันดู

เริ่มก็สร้าง Dockerfile ตามนี้

FROM alpine:3.8
LABEL author="mf"
LABEL version="latest"

ENV SUPERCRONIC_VERSION="v0.1.6" \
    SUPERCRONIC_PACKAGE=supercronic-linux-amd64 \
    SUPERCRONIC_SHA1SUM=c3b78d342e5413ad39092fd3cfc083a85f5e2b75

ENV SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/$SUPERCRONIC_VERSION/$SUPERCRONIC_PACKAGE

ENV TZ Asia/Bangkok

# install dependencies
RUN apk add --update --no-cache ca-certificates curl \
# install supercronic
    && curl -fsSLO "$SUPERCRONIC_URL" \
    && echo "${SUPERCRONIC_SHA1SUM}  ${SUPERCRONIC_PACKAGE}" | sha1sum -c - \
    && chmod +x "${SUPERCRONIC_PACKAGE}" \
    && mv "${SUPERCRONIC_PACKAGE}" "/bin/${SUPERCRONIC_PACKAGE}" \
    && ln -s "/bin/${SUPERCRONIC_PACKAGE}" /bin/supercronic \
# remove unwanted deps & cleanup
    && apk del --purge ca-certificates curl \
    && rm -rf /tmp/* /var/cache/apk/*

ADD crontab.sample /etc/crontab
RUN chmod 0644 /etc/crontab

ENTRYPOINT ["supercronic"]
CMD ["/etc/crontab"]

** ณ ตอนนี้ (29 พ.ย. 61) เวอร์ชั่นล่าสุดอยู่ที่ 0.1.6 ลองเช็คดูเรื่อย ๆ ที่ Github

ตามด้วยการสร้างไฟล์ Crontab สำหรับทำงาน ตัวอย่าง คือกำหนดให้แสดงผลคำว่า “hello from Supercronic” ทุก 1 นาที

# Run every minute
*/1 * * * * echo "hello from Supercronic"

เสร็จก็ทำการ Build และรัน container sudo docker build -t mf/supercronic . && sudo docker run -it mf/supercronicผลลัพธ์จะได้ตามนี้

จบปิ๊ง ^__^

Source

  • https://github.com/aptible/supercronic