python调度器和作业管理器。

pypyrus-runner的Python项目详细描述


流道

本项目的目标是在python中创建简单有效的自动化作业调度和管理工具。

主要功能:

  • 最小且用户友好的命令行界面。
  • 跨平台-应用程序可以在任何使用Python3的操作系统中使用。
  • 有机会在一个地方处理所有作业,而不是使用许多作业(Oracle Enterprise Scheduler、Linux crontab等)。
  • 生成内置计划程序。
  • 生成内置作业。
  • 编辑生成的内置作业。
  • 删除生成的内置作业。
  • 使用ini文件进行配置。
  • 在特定时间、特定期间或每个时间周期内自动安排和执行作业。
  • 现在或特定日期和时间的手动作业执行。
  • 使用简单的tsv文件进行计划。
  • 使用虚拟python环境运行作业。
  • 内置日志记录。
  • 内置通知和警报。

主要目标:

  • 添加一些cli命令。
  • 将数据库附加到Runner作为计划和新数据流存储的选项。
  • 添加简单的Web图形用户界面。
  • 改进通知的机制和外观。
  • 优化。

开始

要求

操作系统:Windows、Linux、Mac OS。

python版本:3.7.1.

python模块依赖项(使用最新版本):日志通知程序

安装

要安装,只需下载最新的runnerrunner版本并将/src/runner文件夹复制到/python/folder/lib/site packages/

如何使用

要开始使用runner,只需要一个包含runner.manager实例的简单文件。

让我们转到磁盘的根目录*c:*(或其他任何位置,这无关紧要)并创建文件夹runner 移动到该文件夹并创建名为manager.py的新文件,其中包含接下来的几串代码。

> C:\runner\manager.py

import runner

manager = runner.Manager()

现在manager.py是您的主要runner接口,也是在c:\ runner文件夹中部署应用程序的方法。

执行manager.py。您将收到主帮助说明,也可以使用manager.py帮助进行调用。

当您第一次执行manager.py时,它会在同一文件夹中生成主配置文件config.ini。

$ python manager.py
INFO: File C:\runner\config.ini created.

List of available commands:

create scheduler    Generate a scheduler with all elements.
create job          Generate a job with all elements.
list jobs           Show all jobs registered in the schedule.
edit schedule       Open the schedule file in the editor.

edit job            Open the job script in the editor.
run job             Execute the job by id with or without run time.
run jobs            Execute the jobs listed in the file.
delete job          Delete the job by id.

edit config         Open one of the configuration files in the editor.

help                Show this message.

For more details type [command] help.                              

您可以为每个命令获得更具体的帮助:

$ python manager.py run job help

Execute the job by id with or without run time.
Parameters:
id         integer                  Id of the job you want to run.
trigger    yyyy-mm-dd/hh24:mi:ss    Date with or without time in ISO format
           yyyy-mm-dd               for what you want to run the job.

我们建议对每个命令重复该操作,以获得更多理解。

创建计划程序

键入commandcreate scheduler生成计划程序。 系统将询问您所需的属性,然后在文件夹中生成计划程序。

$ python manager.py create scheduler

Please follow the steps to create the scheduler.
Enter the name or leave empty to use default:

Enter the description or leave empty to use default:

********************************************************************************
INFO: Creating scheduler scheduler...
INFO: File C:\runner\scheduler.py created.
INFO: File C:\runner\config.ini updated.
INFO: File C:\runner\schedule.tsv created.
INFO: Folder C:\runner\jobs created.

关于文件:

C:\ runner\scheudler.py-调度程序实例和启动程序。

C:\ runner\config.ini-主配置文件。

C:\ runner\schedule.tsv-作业计划。

C:\ runner\jobs-未来作业的文件夹。

启动计划程序

要运行调度程序,只需执行filescheduler.py

$ python scheduler.py

现在调度程序正在工作。 日志可以在文件夹c:\ runner\logs中找到。

要停止调度程序进程,只需键入ctrl+c即可。

创建作业

调度程序已就绪,可以创建作业。

键入commandcreate job生成作业。 系统将询问您所需的属性,然后生成作业。

$ python manager.py create job

Follow the instructions to create the job.
Inputs with * are mandatory.
Enter the name or leave empty to use default:
test
Enter the short description:
Test Job
Enter the environment or leave empty to use Python:

Enter the month day (1-31):

Enter the week day (1-7):

Enter the hour (0-23):
8
Enter the minute (0-59):
0
Enter the second (0-59):
0
Activate job (Y/N)? *:
N
********************************************************************************
INFO: Creating job...
INFO: Job ID: 0
INFO: Folder C:\runner\jobs\0 created.
INFO: File C:\runner\jobs\0\job.py created.
INFO: File C:\runner\jobs\0\config.ini created.
INFO: File C:\runner\jobs\0\script.py created.
INFO: Job test successfully added to schedule!

关于文件:

C:\ runner\jobs\0-这是与作业ID相对应的作业文件夹。

C:\runner\jobs\0\job.py-作业实例和启动程序。

C:\ runner\jobs\0\config.ini-作业配置文件。

C:\ runner\jobs\0\script.py-必须由作业执行的脚本。

schedule.tsv中,将提供已创建作业的新记录。

注意,我们使用了statusn。 在状态更改为Y之前,具有此状态的作业将不会执行。 这样做是为了在作业未完成之前防止意外运行。

日程安排

日程安排是应用程序中最重要的部分之一。

在schedule.tsv中计划的所有作业。 文件由Runner扫描,其格式非常关键。

主要格式要求是:

  • 每一行是一个表格行。
  • 列分隔符只是一个表格。
  • 最后一行必须始终为空。

当schedule.tsv被修改时,runner会捕获更改,进行必要的更新,并将有关该更改的消息打印到日志中。

要查看所有计划的作业,请使用命令列出作业

$ python manager.py list jobs

-----------------------------------------------------------------------------------------------------------------------------
|ID |NAME |DESCRIPTION |ENVIRONMENT |FILE                    |MONTH_DAY |WEEK_DAY |HOUR |MINUTE |SECOND |PARAMETERS |STATUS |
-----------------------------------------------------------------------------------------------------------------------------
|0  |test |Test Job    |python      |C:\runner\jobs\0\job.py |*         |*        |8    |0      |0      |           |N      |
-----------------------------------------------------------------------------------------------------------------------------

要仅查看活动工单,请添加活动

$ python manager.py list jobs active

也可以直接打开文件或使用命令manager.py edit schedule在主配置中使用所选编辑器打开文件。

日程说明 <表><广告>字段名说明 < /广告><正文> ID作业在创建过程中自动接收的唯一ID。姓名工作名称。说明工作说明。环境必须在其中执行作业的环境的名称。所有在Runner环境中使用的都必须列在主配置中。文件输入文件。如果使用内置作业,则它是一个job.py。在其他情况下,如何开始工作取决于您。月日必须执行作业的月份。星期日必须执行作业的星期几。小时必须执行作业的时间。分钟必须执行工作的分钟。秒必须执行作业的秒。参数执行时将传递给文件的其他参数。状态作业的状态。y为活动,n为非活动。

跑步者的工作频率相当灵活。 注意,因为有些组合可以强制runner每秒执行作业!

您可以通过以下几种方式使用时间字段来安排作业:

  1. 在特定的日期和时间执行作业。使用简单整数。
  2. 每个时间周期执行作业。使用带符号/的整数来表示它是一个循环值。
  3. 仅在特定期间执行作业。使用带-的整数来指出它是一个句点值。
  4. < > >

    为了加深理解,我们列出了最流行的组合:

    <表><广告>月日 星期日 小时分钟秒 何时执行 < /广告><正文>每月的第一天上午8点。*每周的第一天上午8点。**每天早上8点。**/1</td>每小时。***/5</td>每五分钟。* 1-5工作日每天上午8点。1,11,21*每月1日、11日和21日上午8点。

    有关日程安排的详细信息

    runner中的自动调度基于时刻。 矩-是一个动态调度程序属性,将当前时间戳描述为从纪元开始算起的过去秒数。

    如果需要的话,你可以记录每一个计划时刻。 为此,将主配置的log部分中的showtime选项设置为true。 然后在日志中,每个时刻都将被记录为一条空消息:

    2018-12-31 23:59:59|INFO|
    

    力矩包括主动相位和被动相位。

    活动阶段代表调度和内部运行器需要的所有必要操作。 主动阶段结束后,被动阶段开始,在此期间跑步者睡眠到下一步。

    如果活动阶段在出现故障时超过1秒,那么调度程序的内部时间将与实际时间不同。 当发生这种情况时,runner将自动与实时同步。 将在日志中注册,这可能有助于排除故障:

    > C:\runner\manager.py
    
    import runner
    
    manager = runner.Manager()
    
    0

    如果要记录每个活动相位的时间消耗,请将主配置中的log部分的showdelay选项设置为true

    任务

    任务是作业必须执行的一组操作。 所有任务都必须在特殊的作业脚本文件中描述。 对于我们的测试作业,这是文件jobs\0\script.py。 该文件已包含job实例,因此也可以在脚本中使用。

    打开脚本文件或使用特殊命令编辑作业,该命令将在主配置中使用选定的编辑器打开作业。

    > C:\runner\manager.py
    
    import runner
    
    manager = runner.Manager()
    
    1

    按以下示例修改文件:

    > C:\runner\manager.py
    
    import runner
    
    manager = runner.Manager()
    
    2

    现在转到schedule.tsv并将修改作业的状态从n更改为y

    作业已准备就绪,将在下一个08:00:00执行。

    运行作业

    作业可以由调度器自动执行,也可以由用户手动执行。

    对于计划中的自动执行,必须确定tsv

    1. 环境这是主配置中描述的环境的名称。
    2. 月日周日小时分钟-执行时间。
    3. < > >

      用户执行可以通过两种方式完成。

      第一种方法是使用特殊命令run job,其中包含强制参数id和可选参数trigger

      参数id是可以在schedule.tsv中找到的作业的唯一标识号。

      参数trigger是必须执行作业的ISO时间戳格式的日期或日期时间。

      在下面的示例中,我们使用ID 0执行2019年1月1日上午8点的作业。

      > C:\runner\manager.py
      
      import runner
      
      manager = runner.Manager()
      
      3

      第二个是执行要运行的作业的job.py文件。 以下命令将在当前时刻执行ID为0的作业:

      > C:\runner\manager.py
      
      import runner
      
      manager = runner.Manager()
      
      4

      也可以使用参数-t/--trigger传递特定的日期和时间。 因此,在2019年1月1日上午8点执行ID为0的作业将如下所示:

      > C:\runner\manager.py
      
      import runner
      
      manager = runner.Manager()
      
      5

      另外,如果有许多作业必须执行,则可以使用特殊文件。 让我们用列出的id和触发器创建文件

      > C:\runner\manager.py
      
      import runner
      
      manager = runner.Manager()
      
      6

      注意,id和trigger之间有一个空格。

      执行命令:

      > C:\runner\manager.py
      
      import runner
      
      manager = runner.Manager()
      
      7

      日志记录

      登录runner日志上实现,并通过日志对象提供。 因此,您甚至可以在script.py中使用日志

      > C:\runner\manager.py
      
      import runner
      
      manager = runner.Manager()
      
      8

      假设今天是2018年12月31日,23:59:59。 运行作业并在日志文件中看到:

      > C:\runner\manager.py
      
      import runner
      
      manager = runner.Manager()
      
      9

      访问日志页以了解更多功能。

      通知和警报

      有时在工作中你可能需要从工作中得到反馈。 很容易做到hemail对象的帮助,它允许在电子邮件上写入消息。

      要使用通知程序,您需要SMTP服务器。 必须在配置中定义连接:IP端口用户密码作业配置(或其他外部配置)中[email]部分的电子邮件地址选项。

      在电子邮件中发送简单的文本消息:

      $ python manager.py
      INFO: File C:\runner\config.ini created.
      
      List of available commands:
      
      create scheduler    Generate a scheduler with all elements.
      create job          Generate a job with all elements.
      list jobs           Show all jobs registered in the schedule.
      edit schedule       Open the schedule file in the editor.
      
      edit job            Open the job script in the editor.
      run job             Execute the job by id with or without run time.
      run jobs            Execute the jobs listed in the file.
      delete job          Delete the job by id.
      
      edit config         Open one of the configuration files in the editor.
      
      help                Show this message.
      
      For more details type [command] help.                              
      
      0

      使用notifier的另一种情况是通知作业执行中出现错误。 我们在Runner中为此目的使用警报。 使用alarm()在作业配置中的[常规]部分的人员选项中列出的电子邮件上发送标准警报通知。

      让我们用0位id来改变我们的工作并执行它。

      $ python manager.py
      INFO: File C:\runner\config.ini created.
      
      List of available commands:
      
      create scheduler    Generate a scheduler with all elements.
      create job          Generate a job with all elements.
      list jobs           Show all jobs registered in the schedule.
      edit schedule       Open the schedule file in the editor.
      
      edit job            Open the job script in the editor.
      run job             Execute the job by id with or without run time.
      run jobs            Execute the jobs listed in the file.
      delete job          Delete the job by id.
      
      edit config         Open one of the configuration files in the editor.
      
      help                Show this message.
      
      For more details type [command] help.                              
      
      1
      $ python manager.py
      INFO: File C:\runner\config.ini created.
      
      List of available commands:
      
      create scheduler    Generate a scheduler with all elements.
      create job          Generate a job with all elements.
      list jobs           Show all jobs registered in the schedule.
      edit schedule       Open the schedule file in the editor.
      
      edit job            Open the job script in the editor.
      run job             Execute the job by id with or without run time.
      run jobs            Execute the jobs listed in the file.
      delete job          Delete the job by id.
      
      edit config         Open one of the configuration files in the editor.
      
      help                Show this message.
      
      For more details type [command] help.                              
      
      2

      因此,您将在user@email.com上收到一条消息::

      $ python manager.py
      INFO: File C:\runner\config.ini created.
      
      List of available commands:
      
      create scheduler    Generate a scheduler with all elements.
      create job          Generate a job with all elements.
      list jobs           Show all jobs registered in the schedule.
      edit schedule       Open the schedule file in the editor.
      
      edit job            Open the job script in the editor.
      run job             Execute the job by id with or without run time.
      run jobs            Execute the jobs listed in the file.
      delete job          Delete the job by id.
      
      edit config         Open one of the configuration files in the editor.
      
      help                Show this message.
      
      For more details type [command] help.                              
      
      3

      c:\ runner\jobs\0\logs\test_20181231235959.log中,您将找到错误详细信息:

      $ python manager.py
      INFO: File C:\runner\config.ini created.
      
      List of available commands:
      
      create scheduler    Generate a scheduler with all elements.
      create job          Generate a job with all elements.
      list jobs           Show all jobs registered in the schedule.
      edit schedule       Open the schedule file in the editor.
      
      edit job            Open the job script in the editor.
      run job             Execute the job by id with or without run time.
      run jobs            Execute the jobs listed in the file.
      delete job          Delete the job by id.
      
      edit config         Open one of the configuration files in the editor.
      
      help                Show this message.
      
      For more details type [command] help.                              
      
      4

      请访问通知程序页面以了解更多功能。

      配置

      您可以使用配置文件或直接将参数写入实例构造函数来配置计划程序和作业。 请记住,直接写入实例的优先级高于配置文件。

      您还可以通过schedulerjob实例的config参数传递附加配置文件:

      $ python manager.py
      INFO: File C:\runner\config.ini created.
      
      List of available commands:
      
      create scheduler    Generate a scheduler with all elements.
      create job          Generate a job with all elements.
      list jobs           Show all jobs registered in the schedule.
      edit schedule       Open the schedule file in the editor.
      
      edit job            Open the job script in the editor.
      run job             Execute the job by id with or without run time.
      run jobs            Execute the jobs listed in the file.
      delete job          Delete the job by id.
      
      edit config         Open one of the configuration files in the editor.
      
      help                Show this message.
      
      For more details type [command] help.                              
      
      5

      但请始终考虑,一旦您提到这些附加配置文件,它们中的所有参数都将被复制到runner配置文件中。

      要编辑主配置文件,请使用以下命令: 要编辑某些作业配置文件,请使用命令编辑配置作业0

      最有可能使用的选项已放置到配置文件中。 在选项说明下方。

      <表><广告>选项 节 值示例 说明 < /广告><正文>编辑经理记事本,nano用于编辑文件的文本编辑器。姓名调度程序,作业调度程序,作业000计划程序或作业的名称。说明调度程序,作业调度程序,作业0调度程序或作业的说明。日程安排调度程序C:\ runner\schedule.tsv,/runner/schedule.tsv计划文件的路径。控制台日志正确,错误将日志输出到控制台而不是文件。按天数限制 日志正确,错误我们需要在新的一天开始时关闭/打开日志吗?按大小限制 日志正确,错误是否需要在达到最大大小时关闭/打开日志?最大尺寸日志10485760日志的最大大小。显示时间日志正确,错误我们需要查看日志中的每个调度时刻吗?显示延迟日志正确,错误我们需要在日志中看到调度器活动阶段的时间消耗吗?巨蟒环境蟒蛇,蟒蛇3 python可执行文件的路径或命令。 CPP环境 CPP >路径或命令到C++可执行文件< < /td> Java/TDT>环境 Java/TDT>路径或命令到Java可执行文件。 个人作业< /TD>obiwankenobi@email.com接收工作通知和警报的电子邮件地址(一个或多个)。 IP电子邮件127.0.0.1带有SMTP服务器的主机的IP地址。端口电子邮件STM港口p服务器。需要登录电子邮件正确,错误我们需要登录到SMTP服务器吗?用户电子邮件克诺比要登录的用户名。密码电子邮件4向您致意登录密码。地址电子邮件lukeskywalker@email.com发送工作通知和警报的电子邮件地址。需要调试电子邮件正确,错误我们需要查看连接过程的详细信息吗?需要TLS电子邮件正确,错误我们需要用tls协议覆盖到smtp的连接吗?

      有关环境的更多信息

      在runner中,您可以添加任何执行作业的环境。 让我们以python虚拟环境为例

      $ python manager.py
      INFO: File C:\runner\config.ini created.
      
      List of available commands:
      
      create scheduler    Generate a scheduler with all elements.
      create job          Generate a job with all elements.
      list jobs           Show all jobs registered in the schedule.
      edit schedule       Open the schedule file in the editor.
      
      edit job            Open the job script in the editor.
      run job             Execute the job by id with or without run time.
      run jobs            Execute the jobs listed in the file.
      delete job          Delete the job by id.
      
      edit config         Open one of the configuration files in the editor.
      
      help                Show this message.
      
      For more details type [command] help.                              
      
      6

      它将把python虚拟环境与我们的runner应用程序一起部署到文件夹中。

      打开main config并向环境部分添加一个新选项,其中包含部署的venv主可执行文件的路径:

      $ python manager.py
      INFO: File C:\runner\config.ini created.
      
      List of available commands:
      
      create scheduler    Generate a scheduler with all elements.
      create job          Generate a job with all elements.
      list jobs           Show all jobs registered in the schedule.
      edit schedule       Open the schedule file in the editor.
      
      edit job            Open the job script in the editor.
      run job             Execute the job by id with or without run time.
      run jobs            Execute the jobs listed in the file.
      delete job          Delete the job by id.
      
      edit config         Open one of the configuration files in the editor.
      
      help                Show this message.
      
      For more details type [command] help.                              
      
      7

      然后,将创建过程中为测试作业选择的python环境更改为venv

      现在您的工作将由virtual python copy来处理,因此pip安装您需要的任何模块,并让您感到自由自在。


      有关构造函数属性的信息,请参阅技术文档。

      有关外部模块对象的详细信息,请参阅本模块文档的相应页面。

      测试

      您可以在/test中找到windows和linux的测试。

      问题

      有关发现的错误和问题的报告,请参阅问题

      卸载

      要卸载,只需删除/python/folder/lib/site packages/runner文件夹即可。

      欢迎加入QQ群-->: 979659372 Python中文网_新手群

      推荐PyPI第三方库


热门话题
java JavaFX触控事件未触发Ubuntu 20.04触控笔记本电脑   java如何在AWT中关闭窗口?   java Dagger 2:注入具有构造函数参数的成员   创建对象的Java调用类   对象我想在A.java中添加两个数字,并在B.java中打印结果(如何?)   java如何使用AWS SDK for Android从数字海洋空间下载图像?   java Facebook sdk 4.0.1无法使用Android studio获取某些字段   4分钟后web应用程序(Angular 8和Rest API)中的java自动会话超时   在Eclipse for Java EE developers edition中禁用HTML警告   java按字母顺序排列字符串我错过了什么明显的东西吗?   java在Jshell中println和printf有什么不同