2019-06-23 03:35:23 -07:00
import { ITriggerFunctions } from 'n8n-core' ;
import {
INodeType ,
INodeTypeDescription ,
ITriggerResponse ,
2021-04-16 09:33:36 -07:00
NodeOperationError ,
2019-06-23 03:35:23 -07:00
} from 'n8n-workflow' ;
export class Interval implements INodeType {
description : INodeTypeDescription = {
displayName : 'Interval' ,
name : 'interval' ,
icon : 'fa:hourglass' ,
2022-04-11 06:12:13 -07:00
group : [ 'trigger' , 'schedule' ] ,
2019-06-23 03:35:23 -07:00
version : 1 ,
2022-10-18 04:59:17 -07:00
hidden : true ,
2019-06-23 03:35:23 -07:00
description : 'Triggers the workflow in a given interval' ,
:sparkles: Improve Waiting Webhook call state in WF Canvas (#2430)
* N8N-2586 Improve Waiting Webhook call state in WF Canvas
* N8N-2586 Added watcher for showing Webhook's Node Tooltip on execution
* N8N-2586 Show helping tooltip for trigger node if wokrflow is running, it is a trigger node, if it is only one trigger node in WF
* N8N-2586 Rework/Move logic to computed property, Created getter for ActveTriggerNodesInWokrflow, Add style to trigger node's tooltip, remove comments
* N8N-2586 Added EventTriggerDescription prop in INodeTypeDescription Interface, Updated Logic for TriggerNode Tooltip based on the new prop
* N8N-2586 Add new use cases/watcher to show Trigger Nodes Tooltip / If has issues, if paused, if wokrlfow is running, Refactor Getter
* N8N-2586 Added z-index to tooltip, Added new Scenario for Tooltip if it is Draged&Droped on the WF
* N8N-2586 Refactor computed property for draged nodes
* N8N-2586 Fixed Conflicts
* N8N-2586 Fixed Tooltip
* N8N-2586 Dont show tooltip on core trigger nodes that execute automatically
* N8N-2586 Fixed Webhook tooltip when adding/deleting canvas during WF execution
* N8N-2586 Updated Logic, Simplify the code
* N8N-2586 Simplify Code
* N8N-2586 Added check for nodetype
* update dragging to use local state
* N8N-2586 Added eventTriggerDescription to Interval Node
* add comment, use new getter
* update to always check
Co-authored-by: Mutasem <mutdmour@gmail.com>
2021-11-25 14:33:41 -08:00
eventTriggerDescription : '' ,
2022-08-17 08:50:24 -07:00
activationMessage :
'Your interval trigger will now trigger executions on the schedule you have defined.' ,
2019-06-23 03:35:23 -07:00
defaults : {
name : 'Interval' ,
color : '#00FF00' ,
} ,
2022-04-22 09:29:51 -07:00
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node
2019-06-23 03:35:23 -07:00
inputs : [ ] ,
outputs : [ 'main' ] ,
properties : [
2022-06-20 12:39:24 -07:00
{
2022-08-17 08:50:24 -07:00
displayName :
'This workflow will run on the schedule you define here once you <a data-key="activate">activate</a> it.<br><br>For testing, you can also trigger it manually: by going back to the canvas and clicking ‘ execute workflow’ ' ,
2022-06-20 12:39:24 -07:00
name : 'notice' ,
type : 'notice' ,
default : '' ,
} ,
2019-06-23 03:35:23 -07:00
{
displayName : 'Interval' ,
name : 'interval' ,
type : 'number' ,
typeOptions : {
minValue : 1 ,
} ,
default : 1 ,
2022-05-06 14:01:25 -07:00
description : 'Interval value' ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'Unit' ,
name : 'unit' ,
type : 'options' ,
options : [
{
name : 'Seconds' ,
2020-10-22 06:46:03 -07:00
value : 'seconds' ,
2019-06-23 03:35:23 -07:00
} ,
{
name : 'Minutes' ,
2020-10-22 06:46:03 -07:00
value : 'minutes' ,
2019-06-23 03:35:23 -07:00
} ,
{
name : 'Hours' ,
2020-10-22 06:46:03 -07:00
value : 'hours' ,
2019-06-23 03:35:23 -07:00
} ,
] ,
default : 'seconds' ,
2022-05-06 14:01:25 -07:00
description : 'Unit of the interval value' ,
2019-06-23 03:35:23 -07:00
} ,
2020-10-22 06:46:03 -07:00
] ,
2019-06-23 03:35:23 -07:00
} ;
async trigger ( this : ITriggerFunctions ) : Promise < ITriggerResponse > {
const interval = this . getNodeParameter ( 'interval' ) as number ;
const unit = this . getNodeParameter ( 'unit' ) as string ;
if ( interval <= 0 ) {
2022-08-17 08:50:24 -07:00
throw new NodeOperationError (
this . getNode ( ) ,
'The interval has to be set to at least 1 or higher!' ,
) ;
2019-06-23 03:35:23 -07:00
}
let intervalValue = interval ;
if ( unit === 'minutes' ) {
intervalValue *= 60 ;
}
if ( unit === 'hours' ) {
intervalValue *= 60 * 60 ;
}
const executeTrigger = ( ) = > {
this . emit ( [ this . helpers . returnJsonArray ( [ { } ] ) ] ) ;
} ;
2021-08-01 12:45:44 -07:00
intervalValue *= 1000 ;
2021-08-22 01:51:32 -07:00
// Reference: https://nodejs.org/api/timers.html#timers_setinterval_callback_delay_args
if ( intervalValue > 2147483647 ) {
2022-07-04 02:12:08 -07:00
throw new NodeOperationError ( this . getNode ( ) , 'The interval value is too large.' ) ;
2021-08-01 12:45:44 -07:00
}
2021-08-02 13:37:20 -07:00
const intervalObj = setInterval ( executeTrigger , intervalValue ) ;
2019-06-23 03:35:23 -07:00
async function closeFunction() {
clearInterval ( intervalObj ) ;
}
async function manualTriggerFunction() {
executeTrigger ( ) ;
}
return {
closeFunction ,
manualTriggerFunction ,
} ;
}
}