Apex Liberation - the evolution of Flex Queue (DF15)

28
Apex Liberation : The evolution of Flex Queue Carolina Ruiz Medina Principal Developer, Product Innovation team [email protected] @CarolEnLaNube @CodeCoffeeCloud Stephen Willcock Director, Product Innovation [email protected] @ stephenwillcock

Transcript of Apex Liberation - the evolution of Flex Queue (DF15)

Salesforce

Apex Liberation : The evolution of Flex QueueCarolina Ruiz MedinaPrincipal Developer, Product Innovation [email protected]@CarolEnLaNube@CodeCoffeeCloud

Stephen WillcockDirector, Product [email protected]@stephenwillcock

Carolina Ruiz MedinaPrincipal Developer, Product Innovation Team at [email protected]@CarolEnLaNube@CodeCoffeeCloud

Stephen WillcockDirector, Product Innovation at [email protected]@stephenwillcockfoobarforce.com

About GREAT ALONE. BETTER TOGETHER.Native to Salesforce1 Platform since 2009Investors include Salesforce Ventures650+ employees, San Francisco based

4

Heavy liftingNormal Execution limited by Apex Governors Number of records to processCPU TimeHeap Size

Working Synchronously

Making light work

Making light work Higher limits in Asynchronous @futureQueueable

BatchPipeline (pilot)

Execute when there are available resources @future

Process a higher number of records

Increased Governor Limits in Async

We dont have job id for @future jobs as @future does not return anything

The method does not necessarily execute in the same order is called

We cant monitor @future jobs

Parameters must be primitive data types

@future - show me the code!

public with sharing class FutureClass { @future static void myMethod(String a, Integer i) { System.debug(Primitive variable' + a + ' and ' + i+But I run ASYNCHROUNOUSLY'); // ALL THE LOGIC HERE }}

We can track/monitor the batch jobs: DataBase.executeBatch returns Id

We can only run 5 concurrent jobs

Batch Apex

We can chain batch jobs

Possibility to use iterator and process different objects (or none)

We cannot reorder or set priorities

Process up to 50M records

Batch Apex - show me the code! public class UpdateAccountFields implements Database.Batchable{ public final String Query; public final String Entity; public final String Field; public final String Value; public UpdateAccountFields(String q, String e, String f, String v){ Query=q; Entity=e; Field=f;Value=v; } public Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator(query); } public void execute(Database.BatchableContext BC, List scope){ for(Sobject s : scope){s.put(Field,Value); } update scope; } public void finish(Database.BatchableContext BC){ }}Simple Batch Apex Examples

Async LimitsExecution Governors

Batch startBatch executeBatch finish@futureQueueable250,000 method calls in a 24 hour periodScheduled

The Evolution of Async Apex

The evolution of @future - Queueable public class AsyncExecutionExample implements Queueable, Database.AllowsCallouts { public void execute(QueueableContext context) { Account a = new Account(Name='Acme',Phone='(415) 555-1212'); insert a; }}

ID jobID = System.enqueueJob(new AsyncExecutionExample());AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];

The evolution of @future - Queueable public class AsyncExecutionExample implements Queueable, Database.AllowsCallouts { public void execute(QueueableContext context) { Account a = new Account(Name='Acme',Phone='(415) 555-1212'); insert a; }}

ID jobID = System.enqueueJob(new AsyncExecutionExample());AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];

Supported but undocumented

The evolution of @future Enhanced Futures (pilot)@future (limits=2xHEAP)public static void myMemoryHog() { }

@future (limits=3xCPU)public static void myIntenseLogicalProcessing() { }Blogged June 2014: Bigger Apex Limits with Enhanced Futures

The evolution of Batch ApexFLEX QUEUE

The evolution of Batch Apex - Flex QueueSpring 15

Flex Queue introduced

95 Batch Apex jobs in the Flex Queue waiting to be processed + 5 concurrent Batch Apex processes

Reorder Flex Queue items via a Flex Queue UI

Summer 15

Reorder Flex Queue items programmatically (pilot)Winter 16

Reorder Flex Queue items programmaticallyGA

Reordering Flex Queue items programmaticallySummer 15:Boolean isSuccess = System.moveFlexQueueJob(jobId, positionNumber);

Winter 16:Boolean isSuccess = FlexQueue.moveBeforeJob(jobToMoveId, jobInQueueId);Boolean isSuccess = FlexQueue.moveAfterJob(jobToMoveId, jobInQueueId);Boolean isSuccess = FlexQueue.moveJobToEnd(jobId);Boolean isSuccess = FlexQueue.moveJobToFront(jobId);

The evolution of Batch Apex - Flex QueueShow me the code!Release Notes:FlexQueue ClassNew Methods

Abort Flex Queue jobs and processing jobs in the same way:system.abortJob(jobId);The evolution of Batch Apex - Flex QueueShow me the code!

Proof of ConceptManage Flex Queue jobsLightning Components

Introducing

BatchMan Brad Slater @innovativebrad

Demo

The further evolution of Batch ApexSpring 15

Flex Queue introduced

95 Batch Apex jobs in the Flex Queue waiting to be processed + 5 concurrent Batch Apex processes

Reorder Flex Queue items via a Flex Queue UI

Summer 15

Reorder Flex Queue items programmatically (pilot)Winter 16

Reorder Flex Queue items programmaticallyGA

???

Programmatically determine current Flex Queue order

Queueable jobs in Flex Queue

What can Flex QueueDo for us?

Queue up to 100 jobs rather than killing any more than 5Batch Apex no longer limited to admins if you build an App for your usersWe can implement our own prioritization mechanism

. Rememeber, with a great power, comes great responsibility

. Remember, with great power, comes great responsibilityConsider the effect of new job status value on existing Batch management codeConsider the possibility of jobs never being processedProcesses may be dependent on one another - strategy for chaining jobs in the Flex QueueMultiple processes managing the Flex Queue (currently no ability to read the current order)

RecapHaving Several Async Processes@futureQueueableBatch JobsWhat to do now? Which one to use? Always batch? You should use the one that better fits to your necessity . The power is now in your hands!!

Thank you