EventUtil

class EventUtil()

Scoped utility class for scheduling events and querying sysevent parititions directly and efficiently.

Direct query via sysevent is not permitted from Scoped, and a bad practice from global, because a poorly written query against a rotated table can force a horribly inneficient query accross this very hot table.

EventUtil works around this by querying realavent parititions directly with GlideAggregate and index efficient queries.

This most usefull method here is eventQueueScheduledOnce below which can be used to prevent event stacking.

EventUtil.currentEventPartition

type: string

the current sysevent000.. table partition for events being created right now.

EventUtil.lookback_minutes

type: number

How far into the past should we look when checking for running events? You may need to adjust upward when for very slow running events.

EventUtil.eventQueueScheduledOnce(name, instance, parm1=, parm2=, wait=0, aggregate_by="instance")

Like gs.eventQueueScheduled, but only schedules the event if the “same” event is not already scheduled.

Matching for an already scheduled “same” event always includes the sysevent.name match and can include secondary fields as set by aggregate_by which defaults to instance resulting in scheduled event per unique name,instance. aggregate_by=”none” or “” just disables secondary field and aggregates by name only. This can also be a csv list.

Arguments
  • name (string) – event_name

  • instance (object.<(GlideRecord|GlideElement)>) – GlideRecord or GlideElement that can be resolved by getRefRecord

  • parm1= (string) – field of event.

  • parm2= (string) – field of event.

  • wait (Integer) – seconds to wait before processing the event.

  • aggregate_by (string) – Defaults to instance. csv list of field names from sysevent table instance,parm1,parm2

Returns

boolean – True if new event was created, false if event creation was skipped due to pending events.

Examples:

var when = new GlideDateTime(current.getValue('sys_updated_on'));
when.addSeconds(-120); // two minutes ago
var filter = 'sys_updated_on>=' + when.getValue();
var runAfter = 840 ; // almost 15 minutes from now
var eu = new EventUtil();
eu.eventQueueScheduledOnce('x_dete_emb.edded_content_something', current, filter, "", runAfter, "none"); //-> true
eu.eventQueueScheduledOnce('x_dete_emb.edded_content_something', current, filter, "", runAfter, "none"); //-> false
eu.eventQueueScheduledOnce('x_dete_emb.edded_content_something', current, filter, "", runAfter, "parm1"); //-> false
eu.eventQueueScheduledOnce('x_dete_emb.edded_content_something', current, filter+"^EQ", "", runAfter, "parm1"); //-> true
eu.eventQueueScheduledOnce('x_dete_emb.edded_content_something', current, filter, "1", runAfter, "parm1,parm2"); //-> true
eu.eventQueueScheduledOnce('x_dete_emb.edded_content_something', current, filter, "1", runAfter, "parm1,parm2"); //-> false
eu.eventQueueScheduledOnce('x_dete_emb.edded_content_something', current, filter, "2", runAfter, "parm1,parm2"); //-> true
// If there is already an event waiting to run, nothing is done, otherwise, the event is scheduled to run 840 seconds from now.
EventUtil.getEventPartitions(from=gs.minutesAgo(this.lookback_minutes), to=new GlideDateTime())

Retrieve an array of sysevent table partitions covering events created between two points in time.

Arguments
  • from (object.<GlideDateTime>|string) – GlideDateTime object set to point in time

  • to (object.<GlideDateTime>|string) – GlideDateTime object set to point in time

Throws

ScopedAccessNotGranted

  • read access to sys_table_rotation_schedule not granted

Returns

Array – - sysevent table partitions for events between from and to

EventUtil.hasEvent(encodedQuery, from, to)

Returns true if any sysevents matching encodedQuery are found in realavent partition tables.

Arguments
  • encodedQuery (string) – encoded query string to search sysevent tables.

  • from (object.<GlideDateTime>|string) – passed to getEventPartitions

  • to (object.<GlideDateTime>|string) – passed to getEventPartitions

Returns

boolean – - true if event matching query was found in event table partitions for timespan.