{ "cells": [ { "cell_type": "markdown", "id": "1cb14cc6-c543-49e3-b692-72cac0b25f88", "metadata": {}, "source": [ "# Demonstrate Events" ] }, { "cell_type": "code", "execution_count": null, "id": "c409851d-e366-4301-940e-ae88a594a3e4", "metadata": {}, "outputs": [], "source": [ "from DATA225utils import make_connection, dataframe_query" ] }, { "cell_type": "code", "execution_count": null, "id": "eb637f90-2084-4c2f-8b3d-26ae6512f2d4", "metadata": {}, "outputs": [], "source": [ "conn = make_connection(config_file = 'monitor.ini')\n", "cursor = conn.cursor()" ] }, { "cell_type": "markdown", "id": "c24069f8-cc73-4ddc-a2fb-63d14af9de85", "metadata": {}, "source": [ "## Is the Event Scheduler On?" ] }, { "cell_type": "code", "execution_count": null, "id": "bcf90dea-1acb-4c9f-b75c-eaa6f481a8d3", "metadata": {}, "outputs": [], "source": [ "_, df = dataframe_query(conn, \"SHOW VARIABLES LIKE 'event_scheduler'\")\n", "df" ] }, { "cell_type": "markdown", "id": "c4c0f923-0cce-4a3a-8ce7-9201a6867762", "metadata": {}, "source": [ "## The recording table" ] }, { "cell_type": "code", "execution_count": null, "id": "9c937338-30d6-4eed-93a8-10ebe738b759", "metadata": {}, "outputs": [], "source": [ "cursor.execute('DROP TABLE IF EXISTS recording')\n", "\n", "sql = ( \"\"\"\n", " CREATE TABLE recording\n", " (\n", " id INT UNIQUE NOT NULL AUTO_INCREMENT,\n", " timestamp DATETIME NOT NULL,\n", " message VARCHAR(32),\n", " PRIMARY KEY (id)\n", " )\n", " \"\"\"\n", " )\n", "\n", "cursor.execute(sql);" ] }, { "cell_type": "markdown", "id": "3e51b161-ec7e-41d3-8f75-190f797d0b4d", "metadata": {}, "source": [ "## Event: Fire Only Once" ] }, { "cell_type": "code", "execution_count": null, "id": "695469c6-db5f-45ec-bc99-a3904364d461", "metadata": {}, "outputs": [], "source": [ "cursor.execute('DROP EVENT IF EXISTS start_recording')\n", "\n", "sql = ( \"\"\"\n", " CREATE EVENT start_recording\n", " ON SCHEDULE AT NOW()\n", " DO BEGIN\n", " INSERT INTO recording(timestamp, message)\n", " VALUES (NOW(), CONCAT('Starting at ', \n", " DATE_FORMAT(NOW(), '%r')));\n", " END\n", " \"\"\"\n", " )\n", "\n", "cursor.execute(sql)" ] }, { "cell_type": "markdown", "id": "5e4f759b-f634-4628-a7fd-8dba40d2033c", "metadata": {}, "source": [ "## Event: Fire Repeatedly" ] }, { "cell_type": "code", "execution_count": null, "id": "6aeffa9b-9679-4b6e-b8a8-0098ae5ee3b2", "metadata": {}, "outputs": [], "source": [ "cursor.execute('DROP EVENT IF EXISTS monitor_every_2_seconds')\n", "\n", "sql = ( \"\"\"\n", " CREATE EVENT monitor_every_2_seconds\n", " ON SCHEDULE EVERY 2 SECOND\n", " STARTS NOW() + INTERVAL 5 SECOND\n", " ENDS NOW() + INTERVAL 30 SECOND\n", " DO BEGIN\n", " INSERT INTO recording(timestamp, message)\n", " VALUES (NOW(), CONCAT('Recording at ', \n", " DATE_FORMAT(NOW(), '%r')));\n", " END\n", " \"\"\"\n", " )\n", "\n", "cursor.execute(sql)" ] }, { "cell_type": "code", "execution_count": null, "id": "8f35e5c6-9083-4b6f-aaf2-e4f2e0943f27", "metadata": {}, "outputs": [], "source": [ "cursor.close()\n", "conn.close()" ] }, { "cell_type": "code", "execution_count": null, "id": "b77a2a70-71c8-4cb0-ba68-45ca2ba6e8e3", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }