-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrigger.py
More file actions
1165 lines (826 loc) · 36.4 KB
/
Copy pathtrigger.py
File metadata and controls
1165 lines (826 loc) · 36.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###########################################################
#
# Copyright (c) 2005, Southpaw Technology
# All Rights Reserved
#
# PROPRIETARY INFORMATION. This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
# Description: Triggers are called periodically in the code based on some event
# These are registered in the global container and listen for events.
__all__ = ["TriggerException", "Trigger", "SampleTrigger", "TimedTrigger", "SampleTimedTrigger"]
import sys, traceback
from pyasm.common import *
#from pyasm.biz import TriggerInCommand
from pyasm.search import ExceptionLog, SearchKey
from command import Command, HandlerCmd
class TriggerException(Exception):
pass
class Trigger(Command):
KEY = "Trigger:triggers2"
STATIC_TRIGGER_KEY = "Trigger:static_trigger"
INTEGRAL_TRIGGER_KEY = "Trigger:integral_trigger"
NOTIFICATION_KEY = "Trigger:notifications"
TRIGGER_EVENT_KEY = "triggers:cache"
NOTIFICATION_EVENT_KEY = "notifications:cache"
def __init__(my):
my.caller = None
my.message = None
my.trigger_sobj = None
my.input = {}
my.output = {}
my.description = ''
super(Trigger,my).__init__()
def get_title(my):
print "WARNING: Should override 'get_title' function for %s" % my
return Common.get_full_class_name(my)
def set_trigger_sobj(my, trigger_sobj):
my.trigger_sobj = trigger_sobj
def get_trigger_sobj(my):
return my.trigger_sobj
def get_trigger_data(my):
data = my.trigger_sobj.get_value("data")
if not data:
return {}
else:
return jsonloads(data)
def set_command(my, command):
my.caller = command
def set_message(my, message):
my.message = message
def get_message(my):
return my.message
def set_event(my, event):
my.message = event
def get_event(my):
return my.message
def get_command(my):
return my.caller
def set_caller(my, caller):
my.caller = caller
def get_caller(my):
return my.caller
def get_command_class(my):
command_class = my.caller.__class__.__name__
return command_class
# set inputs and outputs
def set_input(my, input):
my.input = input
def get_input(my):
return my.input
def set_output(my, output):
my.output = output
def get_output(my):
return my.output
def set_description(my, description):
my.description = description
def get_description(my):
return my.description
def execute(my):
raise TriggerException("Must override execute function")
# static functions
# DEPRECATED
def append_trigger(caller, trigger, event):
'''append to the the list of called triggers'''
#print "Trigger.append_trigger is DEPRECATED"
trigger.set_caller(caller)
trigger.set_event(event)
triggers = Container.append_seq("Trigger:called_triggers",trigger)
append_trigger = staticmethod(append_trigger)
def call_all_triggers():
'''calls all triggers for events that have occurred'''
triggers = Container.get("Trigger:called_triggers")
Container.remove("Trigger:called_triggers")
if not triggers:
return
prev_called_triggers = Container.get_seq("Trigger:prev_called_triggers")
# run each trigger in a separate transaction
for trigger in triggers:
# prevent recursive triggers shutting down the system
input = trigger.get_input()
# remove timestamp (Why was it commented out? i.e. sync related?)
#sobject = input.get('sobject')
#if sobject and sobject.has_key('timestamp'):
# del sobject['timestamp']
input_json = jsondumps(input)
class_name = Common.get_full_class_name(trigger)
event = trigger.get_event()
if class_name == 'pyasm.command.subprocess_trigger.SubprocessTrigger':
class_name = trigger.get_class_name()
if (event, class_name, input_json) in prev_called_triggers:
# handle the emails, which can have multiple per event
if class_name in [
"pyasm.command.email_trigger.EmailTrigger",
"pyasm.command.email_trigger.EmailTrigger2"
]:
pass
else:
#print("Recursive trigger (event: %s, class: %s)" % (event, class_name))
continue
# store previous called triggers
prev_called_triggers.append( (event, class_name, input_json) )
# set call_trigger to false to prevent infinite loops
if not issubclass(trigger.__class__, Trigger):
# if this is not a trigger, then wrap in a command
handler_cmd = HandlerCmd(trigger)
handler_cmd.add_description(trigger.get_description())
trigger = handler_cmd
# triggers need to run in their own transaction when
# they get here.
Trigger.execute_cmd(trigger, call_trigger=False)
# DEPRECATED
#in_transaction = trigger.is_in_transaction()
call_all_triggers = staticmethod(call_all_triggers)
def _get_triggers(cls, call_event, integral_only=False):
if integral_only:
trigger_key = "%s:integral" % cls.TRIGGER_EVENT_KEY
else:
trigger_key = cls.TRIGGER_EVENT_KEY
notification_key = cls.NOTIFICATION_EVENT_KEY
trigger_dict = Container.get(trigger_key)
notification_dict = Container.get(notification_key)
call_event_key = jsondumps(call_event)
# NOTE: get_db_triggers only get triggers for this project ...
# need to update so that triggers from other projects
# are also executed
if trigger_dict == None:
# assign keys to each trigger
trigger_dict = {}
Container.put(trigger_key, trigger_dict)
if integral_only:
# just get all the integral triggers
trigger_sobjs = cls.get_integral_triggers()
else:
# build a list of site and db of the triggers for current
# project
trigger_sobjs = cls.get_db_triggers()
# append all static triggers
static_trigger_sobjs = cls.get_static_triggers()
if static_trigger_sobjs:
trigger_sobjs.extend(static_trigger_sobjs)
# append all integral triggers
integral_trigger_sobjs = cls.get_integral_triggers()
if integral_trigger_sobjs:
trigger_sobjs.extend(integral_trigger_sobjs)
# append all notifications
#notification_sobjs = cls.get_notifications_by_event()
#trigger_sobjs.extend(notification_sobjs)
for trigger_sobj in trigger_sobjs:
trigger_event = trigger_sobj.get_value("event")
trigger_process = trigger_sobj.get_value("process")
trigger_stype = trigger_sobj.get_value("search_type", no_exception=True)
listen_event = {}
listen_event['event'] = trigger_event
if trigger_process:
listen_event['process'] = trigger_process
if trigger_stype:
listen_event['search_type'] = trigger_stype
listen_key = jsondumps(listen_event)
trigger_list = trigger_dict.get(listen_key)
if trigger_list == None:
trigger_list = []
trigger_dict[listen_key] = trigger_list
trigger_list.append(trigger_sobj)
called_triggers = trigger_dict.get(call_event_key)
# assign keys to each notification
if notification_dict == None:
notification_dict = {}
Container.put(notification_key, notification_dict)
# append all notifications without going thru all the logics with project_code
notification_sobjs = cls.get_notifications_by_event()
for trigger_sobj in notification_sobjs:
trigger_event = trigger_sobj.get_value("event")
trigger_process = trigger_sobj.get_value("process")
trigger_stype = trigger_sobj.get_value("search_type", no_exception=True)
trigger_project = trigger_sobj.get_value("project_code", no_exception=True)
listen_event = {}
listen_event['event'] = trigger_event
if trigger_process:
listen_event['process'] = trigger_process
if trigger_stype:
listen_event['search_type'] = trigger_stype
# notification specific
if trigger_project:
listen_event['project_code'] = trigger_project
listen_key = jsondumps(listen_event)
notification_list = notification_dict.get(listen_key)
if notification_list == None:
notification_list = []
notification_dict[listen_key] = notification_list
notification_list.append(trigger_sobj)
# we have to call with and without project_code to cover both cases
key2 = call_event.copy()
from pyasm.biz import Project
project_code = Project.get_project_code()
key2['project_code'] = project_code
call_event_key2 = jsondumps(key2)
matched_notifications = []
for call_event_key in [call_event_key, call_event_key2]:
matched = notification_dict.get(call_event_key)
if matched:
matched_notifications.extend(matched)
combined_triggers = []
if called_triggers:
combined_triggers.extend(called_triggers)
if matched_notifications:
combined_triggers.extend(matched_notifications)
return combined_triggers
_get_triggers = classmethod(_get_triggers)
def get_db_triggers(cls):
site_triggers = Container.get(cls.KEY)
if site_triggers == None:
# find all of the triggers
search = Search("sthpw/trigger")
search.add_project_filter()
site_triggers = search.get_sobjects()
Container.put(cls.KEY, site_triggers)
# find all of the project triggers
from pyasm.biz import Project
project_code = Project.get_project_code()
key = "%s:%s" % (cls.KEY, project_code)
project_triggers = Container.get(key)
if project_triggers == None:
if project_code not in ['admin','sthpw']:
search = Search("config/trigger")
project_triggers = search.get_sobjects()
else:
project_triggers = []
Container.put(key, project_triggers)
triggers = []
triggers.extend(site_triggers)
triggers.extend(project_triggers)
return triggers
get_db_triggers = classmethod(get_db_triggers)
def call_by_key(cls, key, caller, output={}, forced_mode='', integral_only=False ):
event = key.get("event")
#call_event_key = jsondumps(key)
triggers_sobjs = cls._get_triggers(key, integral_only)
if not triggers_sobjs:
return
return cls._handle_trigger_sobjs(triggers_sobjs, caller, event, output, forced_mode=forced_mode)
call_by_key = classmethod(call_by_key)
def call(cls, caller, event, output={}, process=None, search_type=None, forced_mode='' ):
'''message is part of a function name and so should
not contain spaces '''
# build the call event key
call_event = {}
call_event['event'] = event
if process:
call_event['process'] = process
if search_type:
call_event['search_type'] = search_type
#call_event_key = jsondumps(call_event)
triggers_sobjs = cls._get_triggers(call_event)
"""
# get all the triggers for this event
triggers_sobjs = cls.get_by_event(event, process=process)
# append all static triggers
static_trigger_sobjs = cls.get_static_triggers_by_event(event, process=process)
triggers_sobjs.extend(static_trigger_sobjs)
# append all notifications
notification_sobjs = cls.get_notifications_by_event(event, process=process)
triggers_sobjs.extend(notification_sobjs)
"""
if not triggers_sobjs:
return
return cls._handle_trigger_sobjs(triggers_sobjs, caller, event, output, forced_mode=forced_mode)
call = classmethod(call)
def _handle_trigger_sobjs(cls, triggers_sobjs, caller, event, output, forced_mode=''):
triggers = []
# go through each trigger and build the trigger sobject
for trigger_sobj in triggers_sobjs:
mode = trigger_sobj.get_value("mode", no_exception=True)
if not mode:
mode = 'same process,new transaction'
if trigger_sobj.get_base_search_type() == "sthpw/notification":
if forced_mode:
trigger_class = "pyasm.command.EmailTriggerTest"
else:
trigger_class = "pyasm.command.EmailTrigger2"
else:
trigger_class = trigger_sobj.get_value("class_name")
try:
# DEPRECATED
# FIXME: get rid of this hard coding!!!!
"""
if trigger_class.startswith("SPT."):
if trigger_class == 'SPT.MaterialCostTrigger':
script_code = '21MMS'
elif trigger_class == 'SPT.MaterialAggrgtCostTrigger':
script_code = '41MMS'
elif trigger_class == 'SPT.SubtaskCreateTrigger':
script_code = '161MMS'
elif trigger_class == 'SPT.LaborAggrgtTrigger':
script_code = '202MMS'
elif trigger_class == 'SPT.VndrCostAggrgtTrigger':
script_code = '203MMS'
elif trigger_class == 'SPT.PiecesAggrgtTrigger':
script_code = '204MMS'
elif trigger_class == 'SPT.PersonalTimeLogTrigger':
script_code = '261MMS'
elif trigger_class == 'SPT.SubtaskPiecesTrigger':
script_code = '321MMS'
elif trigger_class == 'SPT.SubtaskPiecesDeleteTrigger':
script_code = '322MMS'
elif trigger_class == 'SPT.ProductTypeAggrgtTrigger':
script_code = '681MMS'
elif trigger_class == 'SPT.JobCreatedTrigger':
script_code = '701MMS'
elif trigger_class == 'SPT.SubtaskClosedTrigger':
script_code = '702MMS'
elif trigger_class == 'SPT.JobClosedTrigger':
script_code = '703MMS'
# script_code = '141MMS'
else:
raise TriggerException("No script code found for trigger [%s]" % trigger_class)
from subprocess_trigger import SubprocessTrigger
trigger = SubprocessTrigger()
trigger.set_mode("MMS")
namespace, class_name = trigger_class.split(".")
from pyasm.biz import Project
data = {
"project": Project.get_project_code(),
"ticket": Environment.get_ticket(),
"class_name": class_name,
"script_code": script_code
}
trigger.set_data(data)
"""
if trigger_class in ['pyasm.command.EmailTrigger2', 'pyasm.command.EmailTriggerTest']:
# allow the trigger handler to know the calling sobj
trigger = Common.create_from_class_path(trigger_class)
trigger.set_trigger_sobj(trigger_sobj)
if not forced_mode:
mode = 'separate process,non-blocking'
else:
mode = forced_mode
elif mode in ['same process,same transaction',
'same process,new transaction']:
script_path = trigger_sobj.get_value("script_path")
if trigger_class == '':
from tactic.command.python_cmd import PythonTrigger
trigger = PythonTrigger()
trigger.set_script_path(script_path)
else:
trigger = Common.create_from_class_path(trigger_class)
else:
if trigger_class == '':
script_path = trigger_sobj.get_value("script_path")
#from tactic.command.python_cmd import PythonTrigger
#trigger = PythonTrigger()
#trigger.set_script_path(script_path)
trigger_class = "tactic.command.PythonTrigger"
kwargs = {
"script_path": script_path
}
else:
kwargs = {}
from subprocess_trigger import SubprocessTrigger
trigger = SubprocessTrigger()
trigger.set_mode(mode)
from pyasm.biz import Project
data = {
"project": Project.get_project_code(),
"ticket": Environment.get_ticket(),
"class_name": trigger_class,
"kwargs": kwargs
}
trigger.set_data(data)
trigger.set_event(event)
if isinstance(trigger, Trigger):
trigger.set_trigger_sobj(trigger_sobj)
triggers.append(trigger)
except ImportError, e:
Environment.add_warning("Trigger Not Defined", "Trigger [%s] does not exist" % trigger_class)
#log = ExceptionLog.log(e)
# print the stacktrace
tb = sys.exc_info()[2]
stacktrace = traceback.format_tb(tb)
stacktrace_str = "".join(stacktrace)
print "-"*50
print stacktrace_str
print str(e)
print "-"*50
raise
if issubclass( trigger.__class__, Trigger):
trigger.set_caller(caller)
trigger.set_message(event)
# if it is a subclass of client api handler
# create a package
# transfer outputs to inputs. This allows a command to deliver
# from one process to another
if output:
input = output.copy()
else:
input = caller.get_info()
trigger.set_input(input)
# By default, inputs travel through
trigger.set_output(input)
# set the description properly for transaction_log
trigger.set_description(trigger_sobj.get_value('description'))
# if delayed, then register it to be executed later
if mode != 'same process,same transaction':
Container.append_seq("Trigger:called_triggers",trigger)
continue
# otherwise call the trigger immediately
try:
trigger.execute()
except Exception, e:
#log = ExceptionLog.log(e)
# print the stacktrace
tb = sys.exc_info()[2]
stacktrace = traceback.format_tb(tb)
stacktrace_str = "".join(stacktrace)
print "-"*50
print stacktrace_str
print str(e)
print "-"*50
caller.errors.append("Trigger [%s] failed: %s" \
%(trigger.get_title(), str(e)))
raise
# DEPRECATED
#try:
# exec("trigger.handle_%s()" % event)
#except AttributeError:
# pass
return triggers
_handle_trigger_sobjs = classmethod(_handle_trigger_sobjs)
def get_by_event(cls, event, process=None):
'''get a list of triggers by event'''
site_triggers = Container.get(cls.KEY)
if site_triggers == None:
# find all of the triggers
search = Search("sthpw/trigger")
search.add_project_filter()
site_triggers = search.get_sobjects()
Container.put(cls.KEY, site_triggers)
# find all of the project triggers
from pyasm.biz import Project
project_code = Project.get_project_code()
key = "%s:%s" % (cls.KEY, project_code)
project_triggers = Container.get(key)
if project_triggers == None:
if project_code not in ['admin','sthpw']:
search = Search("config/trigger")
project_triggers = search.get_sobjects()
else:
project_triggers = []
Container.put(key, project_triggers)
triggers = []
triggers.extend(site_triggers)
triggers.extend(project_triggers)
#for trigger in triggers:
# print trigger.get_search_key()
search_type = None
event_triggers = []
for trigger in triggers:
# determin trigger process
trigger_process = trigger.get_value("listen_process", no_exception=True)
if not trigger_process:
trigger_process = trigger.get_value("process", no_exception=True)
# determine if the process matches the trigger process
if trigger_process and not process:
continue
if process and not trigger_process:
continue
if trigger_process and process and trigger_process != process:
continue
# determine the search_type
trigger_stype = trigger.get_value("search_type", no_exception=True)
if trigger_stype and not search_type:
continue
if search_type and not trigger_stype:
continue
if trigger_stype and search_type and trigger_stype != search_type:
continue
#print event, trigger_process, process,trigger.get_id()
if trigger.get_value("event") == event:
event_triggers.append(trigger)
return event_triggers
get_by_event = classmethod(get_by_event)
def get_notifications_by_event(cls, event=None, process=None):
triggers = Container.get(cls.NOTIFICATION_KEY)
if triggers == None:
# find all of the triggers
search = Search("sthpw/notification")
triggers = search.get_sobjects()
Container.put(cls.NOTIFICATION_KEY, triggers)
if event == None:
return triggers
from pyasm.biz import Project
project_code = Project.get_project_code()
event_triggers = []
for trigger in triggers:
trigger_process = trigger.get_value("process")
# if a process is asked for, then the trigger must have a process
if process and not trigger_process:
continue
# if the trigger has a process, then a process must be asked for
if trigger_process and not process:
continue
# the trigger and trigger process must match
if process and trigger_process and trigger_process != process:
continue
# the project must match
trigger_project = trigger.get_value("project_code")
if trigger_project and trigger_project != project_code:
continue
if trigger.get_value("event", no_exception=True) == event:
event_triggers.append(trigger)
return event_triggers
get_notifications_by_event = classmethod(get_notifications_by_event)
STATIC_TRIGGERS = []
def append_static_trigger(cls, trigger, startup=False):
triggers = Container.get(cls.STATIC_TRIGGER_KEY)
if triggers == None:
triggers = []
Container.put(cls.STATIC_TRIGGER_KEY, triggers)
triggers.append(trigger)
# only the startup ones can go to the class variable which stays active before the process exists
if startup:
cls.STATIC_TRIGGERS.append(trigger)
append_static_trigger = classmethod(append_static_trigger)
def get_static_triggers(cls):
triggers = Container.get(cls.STATIC_TRIGGER_KEY)
if triggers == None:
triggers = []
Container.put(cls.STATIC_TRIGGER_KEY, triggers)
# these are all added at start up time
startup_triggers = cls.STATIC_TRIGGERS
tmp_triggers = triggers[:]
tmp_triggers.extend(startup_triggers)
return tmp_triggers
get_static_triggers = classmethod(get_static_triggers)
# NOTE: is this even used?
def get_static_triggers_by_event(cls, event, process=None):
triggers = cls.get_static_triggers()
event_triggers = []
for trigger in triggers:
trigger_process = trigger.get_value("listen_process", no_exception=True)
if not trigger_process:
trigger_process = trigger.get_value("process", no_exception=True)
if trigger_process and not process:
continue
if process and not trigger_process:
continue
if trigger_process and process and trigger_process != process:
continue
if trigger.get_value("event") == event:
event_triggers.append(trigger)
return event_triggers
get_static_triggers_by_event = classmethod(get_static_triggers_by_event)
# integral triggers: these triggers cannot be shut off because they are
# integral to the proper functioning of TACTIC
INTEGRAL_TRIGGERS = []
def append_integral_trigger(cls, trigger, startup=False):
triggers = Container.get(cls.INTEGRAL_TRIGGER_KEY)
if triggers == None:
triggers = []
Container.put(cls.INTEGRAL_TRIGGER_KEY, triggers)
# startup triggersl go to the class variable which stays
# active through many requests
if startup:
cls.INTEGRAL_TRIGGERS.append(trigger)
else:
triggers.append(trigger)
append_integral_trigger = classmethod(append_integral_trigger)
def get_integral_triggers(cls):
triggers = Container.get(cls.INTEGRAL_TRIGGER_KEY)
if triggers == None:
triggers = []
Container.put(cls.INTEGRAL_TRIGGER_KEY, triggers)
# these are all added at start up time
startup_triggers = cls.INTEGRAL_TRIGGERS
# make a copy of the array
tmp_triggers = triggers[:]
tmp_triggers.extend(startup_triggers)
return tmp_triggers
get_integral_triggers = classmethod(get_integral_triggers)
#
# Snapshot is latest trigger
#
__all__.append('SnapshotIsLatestTrigger')
class SnapshotIsLatestTrigger(Trigger):
def is_undoable(cls):
return False
is_undoable = classmethod(is_undoable)
def execute(my):
input = my.get_input()
mode = input.get("mode")
if mode in ['delete','retire']:
sobject_dict = input.get("sobject")
context = sobject_dict.get("context")
search_type = sobject_dict.get("search_type")
search_code = sobject_dict.get("search_code")
search_id = sobject_dict.get("search_id")
search = Search("sthpw/snapshot")
search.add_filter("context", context)
search.add_order_by("timestamp desc")
search.add_filter("search_type", search_type)
if search_code:
search.add_filter("search_code", search_code)
else:
search.add_filter("search_id", search_id)
snapshots = search.get_sobjects()
for i, snapshot in enumerate(snapshots):
if i == 0:
if snapshot.get_value("is_latest") == False:
snapshot.set_value("is_latest", True)
snapshot.update_versionless("latest")
snapshot.commit()
else:
if snapshot.get_value("is_latest") == True:
snapshot.set_value("is_latest", False)
snapshot.commit()
# NOTE: not sure what to do with is_current when the
# current snapshot is deleted
return
sobject_dict = input.get("sobject_dict")
search_key = input.get("search_key")
snapshot = Search.get_by_search_key(search_key)
#print "mode: ", mode
#print "snapshot: ", snapshot.get("version"), snapshot.get("context")
#print "data: ", input.get("update_data").keys()
#print
# if the current snapshot is already the latest, then don't bother
# doing anything
update_data = input.get("update_data")
if update_data.get("is_latest") == True:
snapshot.set_latest(commit=True)
if update_data.get("is_current") == True:
snapshot.set_current(commit=True)
__all__.append('SearchTypeCacheTrigger')
from tactic_client_lib.interpreter import Handler
class SearchTypeCacheTrigger(Handler):
def execute(my):
from pyasm.biz import CacheContainer
print "running cache trigger"
search_type = my.input.get("search_type")
assert search_type
cache = CacheContainer.get(search_type)
cache.make_dirty()
class SampleTrigger(Trigger):
def execute(my):
# filter this to the specific command
command_class = my.get_command_class()
if command_class != "SimpleStatusCmd":
return
print "Executing sample trigger"
import time
class TimedTrigger(Base):
def __init__(my):
# start the clock on creation time
my.start_interval = time.time()
my.interval = 0
my.is_executing = False
def get_execute_interval(my):
'''return number of seconds between execution'''
return
def get_execute_time(my):
'''return time of day this needs to be executed'''
return
def get_time(my):
'''return time when this should be executed'''
pass
def is_in_separate_thread(my):
'''determines whether this trigger should be run in an independent
separate thread'''
return False
def is_ready(my):
if my.is_executing:
return False
execute_interval = my.get_execute_interval()
current = time.time()
my.interval = current - my.start_interval
# check if the execute interval is exceeded
if execute_interval and my.interval >= execute_interval:
return True
# check time of day
execute_time = my.get_execute_time()
if execute_time:
execute_hour, execute_minute = execute_time.split(":")
date = Date()
current_time = date.get_time()
current_hour, current_minute, current_second = current_time.split(":")
if current_hour == execute_hour:
if execute_minute == current_minute:
print "time of day!!!"
return True
return False
def _do_execute(my):
current = time.time()
my.is_executing = True
my.execute()
my.is_executing = False
my.start_interval = current
def execute(my):
raise TriggerException("Must override execute function")
class SampleTimedTrigger(TimedTrigger):