batch jobs: beyond the basics

24
Batch Jobs: Beyond the Basics Eunice Lee Senior Developer [email protected]

Upload: salesforce-developers

Post on 16-Apr-2017

216 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Batch Jobs: Beyond the Basics

Batch Jobs: Beyond the Basics

 Eunice Lee  Senior Developer  [email protected]

Page 2: Batch Jobs: Beyond the Basics

 Safe harbor statement under the Private Securities Litigation Reform Act of 1995:

 This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-looking statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of product or service availability, subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of management for future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments and customer contracts or use of our services.

 The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our service, new products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth, interruptions or delays in our Web hosting, breach of our security measures, the outcome of any litigation, risks associated with completed and any possible mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling non-salesforce.com products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of salesforce.com, inc. is included in our annual report on Form 10-K for the most recent fiscal year and in our quarterly report on Form 10-Q for the most recent fiscal quarter. These documents and others containing important disclosures are available on the SEC Filings section of the Investor Information section of our Web site.

 Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.

Safe Harbor

Page 3: Batch Jobs: Beyond the Basics

Eunice Lee Senior Developer, Arrowpointe Corp.

Page 4: Batch Jobs: Beyond the Basics

•  Batch Jobs Overview

•  Pushing the Limits

•  Frameworks for Scale and Reliability

•  Other Tips and Tricks

•  Q&A

Agenda

Page 5: Batch Jobs: Beyond the Basics

•  Resource-intensive processes

•  Asynchronous

•  Higher Governor Limits

•  Basic structure: Database.Batchable, start, execute, finish

Batch Jobs Overview

Page 6: Batch Jobs: Beyond the Basics

Pushing the Limits Go to 11

Page 7: Batch Jobs: Beyond the Basics

Governor Limits

•  Most commonly hit limits during batch jobs

•  CPU

•  Heap

•  DML record count

•  SOQL record count

•  Callouts

•  Lesser extent:

•  SOQL statements, timeout

•  DML statements

•  Number of asynchronous Apex method executions

•  Can’t catch Limit Exceptions

Page 8: Batch Jobs: Beyond the Basics

Example Application  News Coverage Analyzer

Requirements

•  News data retrieved from external web service

•  Collect data about when specific people talk about specific topics in specific news sources

•  Include other info (e.g. when, tone, etc)

•  500,000+ Contacts

•  Topics picklist

•  News Source picklist

Page 9: Batch Jobs: Beyond the Basics

Example: News Coverage Analyzer  General Flow

Page 10: Batch Jobs: Beyond the Basics

Example: News Coverage Analyzer  Attempt #1: The simplest implementation

Hit callout timeout limit

Hit heap limit

Hit CPU limit Hit DML record

limit

Hit heap limit

Page 11: Batch Jobs: Beyond the Basics

Example: News Coverage Analyzer  Mediocre Fix Option

Reduce scope to avoid CPU and DML limits:

e.g. change Database.executeBatch(new NewsBatchJob(), 200)

to Database.executeBatch(new NewsBatchJob(), 20)

Reduce X to avoid Heap size limit

These are not bad changes, but they should not be your first line of defense.

Page 12: Batch Jobs: Beyond the Basics

Strategies to Avoid Limits  Optimize your code

Use Database.Stateful interface and leverage start() method for preprocessing

Page 13: Batch Jobs: Beyond the Basics

More Strategies to Avoid Limits  Know when to stop or keep going

•  Use System.Limits

•  Chain batch jobs

Page 14: Batch Jobs: Beyond the Basics

Even More Strategies to Avoid Limits Active Heap Management

•  Null variables when you’re done with them

•  SOQL for loop

•  Selective queries

Page 15: Batch Jobs: Beyond the Basics

Frameworks for Scale and Reliability

Common challenges:

•  Obtaining detailed status information

•  Handling errors

•  Flexible scheduling and ordering

 Possible approaches:

•  Base class to standardize behaviors

•  Custom objects and custom settings to manage: configuration, scheduling, running, logging, self-regulation

•  Relax by Zach McElrath

•  Advanced Apex Programming by Dan Appleman

Page 16: Batch Jobs: Beyond the Basics

Other Tips and Tricks

Page 17: Batch Jobs: Beyond the Basics

•  Constructor executes in the context of the calling code

•  Start executes in its own context with own limits

•  Generally want errors and error handling to occur asynchronously

Empty or Lean Constructor

Put this in the

start() method

Page 18: Batch Jobs: Beyond the Basics

The records in your scope reflect the values they had at the time the start() method executed.

Refresh Your Scope

Page 19: Batch Jobs: Beyond the Basics

•  Often an inverse relationship between CPU time and Heap size

Trade-offs between CPU and Heap (and SOQL queries)

vs.

Page 20: Batch Jobs: Beyond the Basics

•  Can’t use protected Custom Settings for your scope

•  Use a proxy or dummy scope

•  Fix scheduled for Winter ‘16?

(Only applies to managed packages) Batching over Protected Custom Settings

Page 21: Batch Jobs: Beyond the Basics
Page 22: Batch Jobs: Beyond the Basics
Page 23: Batch Jobs: Beyond the Basics
Page 24: Batch Jobs: Beyond the Basics