src/pyams_scheduler/trigger.py
changeset 85 421da87c4120
parent 68 0169d052e20d
equal deleted inserted replaced
84:688aa8041b11 85:421da87c4120
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 
    13 __docformat__ = 'restructuredtext'
    13 __docformat__ = 'restructuredtext'
    14 
    14 
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from pyams_scheduler.interfaces import ITaskSchedulingMode, ICronTaskScheduling, ICronTask, SCHEDULER_TASK_CRON_INFO, \
       
    20     IDateTaskScheduling, IDateTask, ILoopTaskScheduling, SCHEDULER_TASK_DATE_INFO, ILoopTask, SCHEDULER_TASK_LOOP_INFO
       
    21 
       
    22 # import packages
       
    23 from apscheduler.triggers.cron import CronTrigger
    15 from apscheduler.triggers.cron import CronTrigger
    24 from apscheduler.triggers.date import DateTrigger
    16 from apscheduler.triggers.date import DateTrigger
    25 from apscheduler.triggers.interval import IntervalTrigger
    17 from apscheduler.triggers.interval import IntervalTrigger
    26 from persistent import Persistent
    18 from persistent import Persistent
       
    19 from zope.componentvocabulary.vocabulary import UtilityVocabulary
       
    20 from zope.schema.fieldproperty import FieldProperty
       
    21 
       
    22 from pyams_scheduler.interfaces import ICronTask, ICronTaskScheduling, IDateTask, IDateTaskScheduling, ILoopTask, \
       
    23     ILoopTaskScheduling, ITaskSchedulingMode, SCHEDULER_TASK_CRON_INFO, SCHEDULER_TASK_DATE_INFO, \
       
    24     SCHEDULER_TASK_LOOP_INFO
    27 from pyams_utils.adapter import adapter_config, get_annotation_adapter
    25 from pyams_utils.adapter import adapter_config, get_annotation_adapter
    28 from pyams_utils.date import date_to_datetime
    26 from pyams_utils.date import date_to_datetime
       
    27 from pyams_utils.factory import factory_config
    29 from pyams_utils.registry import utility_config
    28 from pyams_utils.registry import utility_config
    30 from pyams_utils.timezone import tztime
    29 from pyams_utils.timezone import tztime
    31 from pyams_utils.vocabulary import vocabulary_config
    30 from pyams_utils.vocabulary import vocabulary_config
    32 from zope.componentvocabulary.vocabulary import UtilityVocabulary
       
    33 from zope.interface import implementer
       
    34 from zope.schema.fieldproperty import FieldProperty
       
    35 
    31 
    36 from pyams_scheduler import _
    32 from pyams_scheduler import _
    37 
    33 
    38 
    34 
    39 @vocabulary_config(name='PyAMS scheduling modes')
    35 @vocabulary_config(name='PyAMS scheduling modes')
    46 
    42 
    47 #
    43 #
    48 # Cron-style scheduling mode
    44 # Cron-style scheduling mode
    49 #
    45 #
    50 
    46 
    51 @implementer(ICronTaskScheduling)
    47 @factory_config(ICronTaskScheduling)
    52 class CronTaskScheduleInfo(Persistent):
    48 class CronTaskScheduleInfo(Persistent):
    53     """Cron-style schedule info"""
    49     """Cron-style schedule info"""
    54 
    50 
    55     active = FieldProperty(ICronTaskScheduling['active'])
    51     active = FieldProperty(ICronTaskScheduling['active'])
    56     start_date = FieldProperty(ICronTaskScheduling['start_date'])
    52     start_date = FieldProperty(ICronTaskScheduling['start_date'])
    61     week = FieldProperty(ICronTaskScheduling['week'])
    57     week = FieldProperty(ICronTaskScheduling['week'])
    62     day_of_week = FieldProperty(ICronTaskScheduling['day_of_week'])
    58     day_of_week = FieldProperty(ICronTaskScheduling['day_of_week'])
    63     hour = FieldProperty(ICronTaskScheduling['hour'])
    59     hour = FieldProperty(ICronTaskScheduling['hour'])
    64     minute = FieldProperty(ICronTaskScheduling['minute'])
    60     minute = FieldProperty(ICronTaskScheduling['minute'])
    65     second = FieldProperty(ICronTaskScheduling['second'])
    61     second = FieldProperty(ICronTaskScheduling['second'])
       
    62 
       
    63 
       
    64 @adapter_config(context=ICronTask, provides=ICronTaskScheduling)
       
    65 def cron_task_scheduler_info_factory(context):
       
    66     """Cron-style task scheduling info factory"""
       
    67     return get_annotation_adapter(context, SCHEDULER_TASK_CRON_INFO, ICronTaskScheduling,
       
    68                                   notify=False, locate=False)
    66 
    69 
    67 
    70 
    68 @utility_config(name='Cron-style scheduling', provides=ITaskSchedulingMode)
    71 @utility_config(name='Cron-style scheduling', provides=ITaskSchedulingMode)
    69 class CronTaskScheduler(object):
    72 class CronTaskScheduler(object):
    70     """Cron-style scheduler mode"""
    73     """Cron-style scheduler mode"""
    86                            second=info.second or '0',
    89                            second=info.second or '0',
    87                            start_date=tztime(date_to_datetime(info.start_date)),
    90                            start_date=tztime(date_to_datetime(info.start_date)),
    88                            end_date=tztime(date_to_datetime(info.end_date)))
    91                            end_date=tztime(date_to_datetime(info.end_date)))
    89 
    92 
    90 
    93 
    91 @adapter_config(context=ICronTask, provides=ICronTaskScheduling)
       
    92 def cron_task_scheduler_info_factory(context):
       
    93     """Cron-style task scheduling info factory"""
       
    94     return get_annotation_adapter(context, SCHEDULER_TASK_CRON_INFO, CronTaskScheduleInfo,
       
    95                                   notify=False, locate=False)
       
    96 
       
    97 
       
    98 #
    94 #
    99 # Date-style scheduling mode
    95 # Date-style scheduling mode
   100 #
    96 #
   101 
    97 
   102 @implementer(IDateTaskScheduling)
    98 @factory_config(IDateTaskScheduling)
   103 class DateTaskScheduleInfo(Persistent):
    99 class DateTaskScheduleInfo(Persistent):
   104     """Date-style schedule info"""
   100     """Date-style schedule info"""
   105 
   101 
   106     active = FieldProperty(IDateTaskScheduling['active'])
   102     active = FieldProperty(IDateTaskScheduling['active'])
   107     start_date = FieldProperty(IDateTaskScheduling['start_date'])
   103     start_date = FieldProperty(IDateTaskScheduling['start_date'])
       
   104 
       
   105 
       
   106 @adapter_config(context=IDateTask, provides=IDateTaskScheduling)
       
   107 def date_task_scheduler_info_factory(context):
       
   108     """Date-style task scheduling info factory"""
       
   109     return get_annotation_adapter(context, SCHEDULER_TASK_DATE_INFO, IDateTaskScheduling,
       
   110                                   notify=False, locate=False)
   108 
   111 
   109 
   112 
   110 @utility_config(name='Date-style scheduling', provides=ITaskSchedulingMode)
   113 @utility_config(name='Date-style scheduling', provides=ITaskSchedulingMode)
   111 class DateTaskScheduler(object):
   114 class DateTaskScheduler(object):
   112     """Date-style scheduler mode"""
   115     """Date-style scheduler mode"""
   119             raise Exception(_("Task is not configured for date-style scheduling!"))
   122             raise Exception(_("Task is not configured for date-style scheduling!"))
   120         info = self.schema(task)
   123         info = self.schema(task)
   121         return DateTrigger(run_date=tztime(date_to_datetime(info.start_date)))
   124         return DateTrigger(run_date=tztime(date_to_datetime(info.start_date)))
   122 
   125 
   123 
   126 
   124 @adapter_config(context=IDateTask, provides=IDateTaskScheduling)
       
   125 def date_task_scheduler_info_factory(context):
       
   126     """Date-style task scheduling info factory"""
       
   127     return get_annotation_adapter(context, SCHEDULER_TASK_DATE_INFO, DateTaskScheduleInfo,
       
   128                                   notify=False, locate=False)
       
   129 
       
   130 
       
   131 #
   127 #
   132 # Loop-style scheduling mode
   128 # Loop-style scheduling mode
   133 #
   129 #
   134 
   130 
   135 @implementer(ILoopTaskScheduling)
   131 @factory_config(ILoopTaskScheduling)
   136 class LoopTaskScheduleInfo(Persistent):
   132 class LoopTaskScheduleInfo(Persistent):
   137     """Loop-style schedule info"""
   133     """Loop-style schedule info"""
   138 
   134 
   139     active = FieldProperty(ILoopTaskScheduling['active'])
   135     active = FieldProperty(ILoopTaskScheduling['active'])
   140     start_date = FieldProperty(ILoopTaskScheduling['start_date'])
   136     start_date = FieldProperty(ILoopTaskScheduling['start_date'])
   142     weeks = FieldProperty(ILoopTaskScheduling['weeks'])
   138     weeks = FieldProperty(ILoopTaskScheduling['weeks'])
   143     days = FieldProperty(ILoopTaskScheduling['days'])
   139     days = FieldProperty(ILoopTaskScheduling['days'])
   144     hours = FieldProperty(ILoopTaskScheduling['hours'])
   140     hours = FieldProperty(ILoopTaskScheduling['hours'])
   145     minutes = FieldProperty(ILoopTaskScheduling['minutes'])
   141     minutes = FieldProperty(ILoopTaskScheduling['minutes'])
   146     seconds = FieldProperty(ILoopTaskScheduling['seconds'])
   142     seconds = FieldProperty(ILoopTaskScheduling['seconds'])
       
   143 
       
   144 
       
   145 @adapter_config(context=ILoopTask, provides=ILoopTaskScheduling)
       
   146 def loop_task_scheduler_info_factory(context):
       
   147     """Loop-style task scheduling info factory"""
       
   148     return get_annotation_adapter(context, SCHEDULER_TASK_LOOP_INFO, ILoopTaskScheduling,
       
   149                                   notify=False, locate=False)
   147 
   150 
   148 
   151 
   149 @utility_config(name='Loop-style scheduling', provides=ITaskSchedulingMode)
   152 @utility_config(name='Loop-style scheduling', provides=ITaskSchedulingMode)
   150 class LoopTaskScheduler(object):
   153 class LoopTaskScheduler(object):
   151     """Loop-style scheduler mode"""
   154     """Loop-style scheduler mode"""
   162                                hours=info.hours,
   165                                hours=info.hours,
   163                                minutes=info.minutes,
   166                                minutes=info.minutes,
   164                                seconds=info.seconds,
   167                                seconds=info.seconds,
   165                                start_date=tztime(date_to_datetime(info.start_date)),
   168                                start_date=tztime(date_to_datetime(info.start_date)),
   166                                end_date=tztime(date_to_datetime(info.end_date)))
   169                                end_date=tztime(date_to_datetime(info.end_date)))
   167 
       
   168 
       
   169 @adapter_config(context=ILoopTask, provides=ILoopTaskScheduling)
       
   170 def loop_task_scheduler_info_factory(context):
       
   171     """Loop-style task scheduling info factory"""
       
   172     return get_annotation_adapter(context, SCHEDULER_TASK_LOOP_INFO, LoopTaskScheduleInfo,
       
   173                                   notify=False, locate=False)