Code Monkey home page Code Monkey logo

scheduled-actions's Introduction

NOTE: this library is deprecated and no longer under development.
As of Spinnaker version 1.13, it is no longer used inside Spinnaker.

A library for management, operations and persistence of scheduled actions

Build Status Below are the features:

  • Create and register an com.netflix.scheduledactions.ActionInstance which gives a handle to the implemented action (com.netflix.scheduledactions.Action)
  • Actions can be scheduled using a type of com.netflix.scheduledactions.triggers.Trigger. For example, a scheduled trigger for an action can be a cron expression
  • Track and monitor the action executions with ability to add a listener for events like onCancel(), onStart(), onComplete(), onError(), etc.
  • Actions can be executed in an ad-hoc manner too.
  • Actions can provide cancel hook using the onCancel() callback of an com.netflix.scheduledactions.ExecutionListener
  • Enable/Disable ActionInstances and corresponding scheduled triggers
  • Persist and view a history of executions for a given ActionInstance using scheduled-actions-cassandra module

This library is not a workflow engine nor a scheduling library (It uses fenzo-triggers library for time based scheduling).

Installation

Following are the two artifacts:

Group/Org Artifact Id Required Features
com.netflix.scheduledactions scheduled-actions-core Yes Core library features
com.netflix.scheduledactions scheduled-actions-cassandra No Provides cassandra persistence for ActionInstance, Execution, etc.
com.netflix.scheduledactions scheduled-actions-web No Provides spring REST controller to access the ActionsOperator

Download instructions for gradle:

repositories {
    jcenter()
}

dependencies {
    compile "com.netflix.scheduledactions:${artifactId}:${version}" // For example: compile "com.netflix.scheduledactions:scheduled-actions-core:0.3"
}

Usage

com.netflix.scheduledactions.ActionsOperator is the primary class for registering, enabling, disabling, executing and cancelling ActionInstances.

At a high level, users need to follow below steps to use this library:

  1. Implement an action
  2. Create an ActionInstance
  3. Create an ActionsOperator instance
  4. Register the ActionInstance with ActionsOperator

Details and sample code for each step is explained below:

1. Implement an Action

Create an action by either implementing Action interface or by extending ActionSupport class

public class MyAction implements Action {
    // Implement methods here...
}
public class MyAction extends ActionSupport {
    // Implement or/and override methods here...
}

2. Create an ActionInstance

ActionInstance provides a builder to create an action instance

ActionInstance actionInstance = ActionInstance.newActionInstance()
    .withName("Process Items")
    .withGroup("MyApplication")
    .withAction(MyAction.class)
    .withOwners("[email protected]")
    .withWatchers("[email protected]")
    .build();

A Trigger can be associated with an ActionInstance

ActionInstance actionInstance = ActionInstance.newActionInstance()
    .withName("Process Items")
    .withGroup("MyApplication")
    .withAction(MyAction.class)
    .withTrigger(new CronTrigger("0 0 0/1 * * ?"))  // Run the action every hour
    .build();

Specify a timeout for your action execution

ActionInstance actionInstance = ActionInstance.newActionInstance()
    .withName("Process Items")
    .withGroup("MyApplication")
    .withAction(MyAction.class)
    .withTrigger(new CronTrigger("0 0 0/1 * * ?"))  // Run the action every hour
    .withExecutionTimeoutInSeconds(45*60L)  // Timeout after 45 minutes
    .build();

Specify an ExecutionListener for your action

MyListener implements ExecutionListener {
    // Implement methods here...
}

ActionInstance actionInstance = ActionInstance.newActionInstance()
    .withName("Process Items")
    .withGroup("MyApplication")
    .withAction(MyAction.class)
    .withTrigger(new CronTrigger("0 0 0/1 * * ?"))  // Run the action every hour
    .withExecutionTimeoutInSeconds(45*60L)  // Timeout after 45 minutes
    .withExecutionListener(MyListener.class)
    .build();

A concurrent execution strategy can also be setup for the action instance. The strategy can be set to one of the following:

  • REJECT - skip the action execution if one is already running (default)
  • ALLOW - execute all Executions concurrently
  • REPLACE - cancel the previous one and run the new one

This can be configured while creating the ActionInstance

ActionInstance actionInstance = ActionInstance.newActionInstance()
    .withName("Process Items")
    .withGroup("MyApplication")
    .withAction(MyAction.class)
    .withTrigger(new CronTrigger("0 0 0/1 * * ?"))  // Run the action every hour
    .withExecutionTimeoutInSeconds(45*60L)  // Timeout after 45 minutes
    .withExecutionListener(MyListener.class)
    .withConcurrentExecutionStrategy(ConcurrentExecutionStrategy.ALLOW)
    .build();

3. Create an ActionsOperator instance

For creating an instance of ActionsOperator, use the static factory method in ActionsOperator class

ActionsOperator.getInstance(...);

The getInstance() method above takes following parameters:

  1. DaoConfigurer - a DAO implementations holder class. If using scheduled-actions-cassandra library, then the Cassandra DAO implementations can be used. If not, use the existing InMemoryXXXDao implementations
  2. Executor - an implementation of com.netflix.scheduledactions.executors.Executor interface. Use the com.netflix.scheduledactions.executors.ExecutorFactory to get an executor
  3. int - the size of the scheduler thread pool

So, assuming the scheduled-actions-cassandra library is being used, below is the sample code for creating an ActionsOperator instance

Keyspace keyspace = <Astyanax keyspace instance>
DaoConfigurer daoConfigurer = new DaoConfigurer(new CassandraActionInstanceDao(keyspace), new CassandraTriggerDao(keyspace))
Executor executor = ExecutorFactory.getDefaultExecutor(new CassandraExecutionDao(keyspace), 20)     // 20 is the thread pool size for the executor
ActionsOperator actionsOperator = ActionsOperator.getInstance(daoConfigurer, executor, 20)

4. Register the ActionInstance with ActionsOperator

Once you have a ActionInstance, you can register it with the ActionsOperator

ActionsOperator actionsOperator = ActionsOperator.getInstance();
actionsOperator.registerActionInstance(actionInstance);

Other Features

Vew action execution history

List<Execution> executions = actionsOperator.getExecutions(actionInstance.getId());

Ad-hoc execution of the action

If you want to execute your Action apart from being executed by the Trigger (if ActionInstance is created with a trigger), then you can use ActionsOperator to execute the ActionInstance as well

actionsOperator.execute(actionInstance);
// OR
actionsOperator.execute(actionInstanceId);

Cancel an action execution

A best case attempt will be made to cancel the execution by causing an InterruptedException to the Executor thread

actionsOperator.cancel(execution);
// OR
actionsOperator.cancel(executionId);

Copyright and License

Copyright (C) 2015 Netflix. Licensed under the Apache License.

See LICENSE.txt for more information.

scheduled-actions's People

Contributors

anotherchrisberry avatar cfieber avatar dibyom avatar ezimanyi avatar marchello2000 avatar robzienert avatar sthadeshwar avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.