{ "cells": [ { "cell_type": "markdown", "id": "725384bf", "metadata": {}, "source": [ "# Schedule and modify jobs" ] }, { "cell_type": "markdown", "id": "21f8d5bf", "metadata": {}, "source": [ "This example shows how to create a job request, schedule it for execution in the\n", "future, and modify the scheduled job before it completes.\n", "\n", "This example uses an Excel import job, but the functionality demonstrated here can be applied\n", "to text import and Excel export jobs." ] }, { "cell_type": "markdown", "id": "ccc87b5b", "metadata": {}, "source": [ "## Connect to Granta MI" ] }, { "cell_type": "markdown", "id": "39c2a1e0", "metadata": {}, "source": [ "Import the ``Connection`` class and create the connection. For more information, see\n", "the [Connect and access the job queue](0_Getting_started.ipynb) example." ] }, { "cell_type": "code", "execution_count": null, "id": "fb096856", "metadata": { "tags": [] }, "outputs": [], "source": [ "from ansys.grantami.jobqueue import Connection\n", "\n", "server_url = \"http://my_grantami_server/mi_servicelayer\"\n", "client = Connection(server_url).with_credentials(\"user_name\", \"password\").connect()" ] }, { "cell_type": "markdown", "id": "a42d20a8", "metadata": {}, "source": [ "## Create an ``ExcelImportJobRequest`` object" ] }, { "cell_type": "markdown", "id": "128896c8", "metadata": {}, "source": [ "This cell creates an Excel import job request and schedules it for execution tomorrow.\n", "This example does not contain a full description of the ``ExcelImportJobRequest`` object. For\n", "more information, see the [Create an Excel import job](1_Excel_import_job.ipynb) example." ] }, { "cell_type": "code", "execution_count": null, "id": "b731f516", "metadata": {}, "outputs": [], "source": [ "import datetime\n", "import pathlib\n", "\n", "try:\n", " # Python 3.11+\n", " from datetime import UTC as utc\n", "except ImportError:\n", " # Python 3.9 and 3.10\n", " from datetime import timezone\n", "\n", " utc = timezone.utc\n", "\n", "from ansys.grantami.jobqueue import ExcelImportJobRequest\n", "\n", "tomorrow = datetime.datetime.now(utc) + datetime.timedelta(days=1)\n", "combined_excel_import_request = ExcelImportJobRequest(\n", " name=\"Excel Import (combined template and data file)\",\n", " description=\"An example excel import job\",\n", " combined_files=[pathlib.Path(\"assets/combined_import_file.xlsx\")],\n", " scheduled_execution_date=tomorrow,\n", ")\n", "\n", "combined_excel_import_request" ] }, { "cell_type": "markdown", "id": "8fb1256b", "metadata": {}, "source": [ "## Submit the job\n", "Next, submit the jobs to the server. There are two methods for submitting job\n", "requests:\n", "\n", "* ``create_job()``: Submit the job request to the server and immediately return an\n", " ``AsyncJob`` object in the *pending* state.\n", "* ``create_job_and_wait()``: Submit the job request to the server and block until the job\n", " either completes or fails. Return an ``AsyncJob`` object in the *succeeded* or *failed* state.\n", "\n", "Because you have configured the Excel job request object to execute tomorrow, you must use the\n", "``create_job()`` method. If you used the ``create_job_and_wait()`` method, it would block until\n", "the job completed, which means this script would take 24 hours to complete." ] }, { "cell_type": "code", "execution_count": null, "id": "2bf6a992", "metadata": {}, "outputs": [], "source": [ "deferred_job = client.create_job(combined_excel_import_request)\n", "deferred_job" ] }, { "cell_type": "markdown", "id": "ba4f0a94", "metadata": {}, "source": [ "## List jobs\n", "Use the ``.jobs`` property to list the jobs on the server." ] }, { "cell_type": "code", "execution_count": null, "id": "abaa356d", "metadata": {}, "outputs": [], "source": [ "client.jobs" ] }, { "cell_type": "markdown", "id": "d81a6a18", "metadata": {}, "source": [ "Note that only jobs that you have access to are included in this property. Non-administrator users\n", "can only access and modify their own jobs. Administrator users can access and modify all jobs on the\n", "server." ] }, { "cell_type": "markdown", "id": "77bc1203", "metadata": {}, "source": [ "## Edit existing jobs\n", "You can edit the properties of a running or completed job with the associated ``AsyncJob``\n", "object. The following cell shows how to update the name and description of the job and to change the\n", "scheduled execution to occur immediately." ] }, { "cell_type": "code", "execution_count": null, "id": "1b7fc59c", "metadata": {}, "outputs": [], "source": [ "deferred_job.update_name(\"Combined Excel Import (modified)\")\n", "deferred_job.update_description(\"A new description for a combined Excel import job\")\n", "\n", "now = datetime.datetime.now(utc)\n", "deferred_job.update_scheduled_execution_date_time(now)\n", "\n", "client.jobs" ] }, { "cell_type": "markdown", "id": "54873dbc", "metadata": {}, "source": [ "## Retrieve long-running jobs\n", "If the job is expected to take a long time to complete, you can save the job ID to disk and\n", "use it with the ``client.get_job_by_id()`` method to check the status of the job later." ] }, { "cell_type": "code", "execution_count": null, "id": "2393097e", "metadata": {}, "outputs": [], "source": [ "job_id = deferred_job.id\n", "retrieved_job = client.get_job_by_id(job_id)\n", "retrieved_job" ] }, { "cell_type": "markdown", "id": "8b632b21", "metadata": {}, "source": [ "Wait for the pending job to complete." ] }, { "cell_type": "code", "execution_count": null, "id": "63b98096", "metadata": {}, "outputs": [], "source": [ "import time\n", "\n", "from ansys.grantami.jobqueue import JobStatus\n", "\n", "while deferred_job.status not in [JobStatus.Succeeded, JobStatus.Failed]:\n", " time.sleep(1)\n", " deferred_job.update()\n", "\n", "deferred_job.status" ] }, { "cell_type": "markdown", "id": "2561f8a2", "metadata": {}, "source": [ "## Access output files\n", "The job is now complete. You can access the files generated by the job in the same way as for\n", "jobs that execute immediately." ] }, { "cell_type": "code", "execution_count": null, "id": "0f964265", "metadata": {}, "outputs": [], "source": [ "log_file_name = next(name for name in deferred_job.output_file_names if \"log\" in name)\n", "log_file_content = deferred_job.get_file_content(log_file_name)\n", "log_file_string = log_file_content.decode(\"utf-8\")\n", "print(f\"{log_file_name} (first 200 characters):\")\n", "print(f\"{log_file_string[:500]}...\")" ] }, { "cell_type": "markdown", "id": "fc3ab5e3", "metadata": {}, "source": [ "## Delete a job\n", "You can delete jobs from the job queue using the ``.delete_jobs()`` method. This method\n", "accepts a list of jobs, which means that you can delete multiple jobs with a single request if required." ] }, { "cell_type": "code", "execution_count": null, "id": "5f9c0fb1", "metadata": {}, "outputs": [], "source": [ "client.delete_jobs([deferred_job])\n", "client.jobs" ] } ], "metadata": { "granta": { "clean_database": true }, "jupytext": { "notebook_metadata_filter": "granta" }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }