Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
D
drupal
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
225
Merge Requests
225
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
drupal
Commits
493eac19
Commit
493eac19
authored
Aug 09, 2016
by
alexpott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2778461
by kiamlaluno, joachim: Update hook_cron() sample code
parent
559ddb94
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
10 deletions
+21
-10
core/core.api.php
core/core.api.php
+21
-10
No files found.
core/core.api.php
View file @
493eac19
...
...
@@ -1894,21 +1894,32 @@
function
hook_cron
()
{
// Short-running operation example, not using a queue:
// Delete all expired records since the last cron run.
$expires
=
\
Drupal
::
state
()
->
get
(
'mymodule.
cron_last_run'
,
REQUEST_TIME
);
db_
delete
(
'mymodule_table'
)
$expires
=
\
Drupal
::
state
()
->
get
(
'mymodule.
last_check'
,
0
);
\
Drupal
::
database
()
->
delete
(
'mymodule_table'
)
->
condition
(
'expires'
,
$expires
,
'>='
)
->
execute
();
\
Drupal
::
state
()
->
set
(
'mymodule.
cron_last_run
'
,
REQUEST_TIME
);
\
Drupal
::
state
()
->
set
(
'mymodule.
last_check
'
,
REQUEST_TIME
);
// Long-running operation example, leveraging a queue:
// Fetch feeds from other sites.
$result
=
db_query
(
'SELECT * FROM {aggregator_feed} WHERE checked + refresh < :time AND refresh <> :never'
,
array
(
':time'
=>
REQUEST_TIME
,
':never'
=>
AGGREGATOR_CLEAR_NEVER
,
));
// Queue news feeds for updates once their refresh interval has elapsed.
$queue
=
\
Drupal
::
queue
(
'aggregator_feeds'
);
foreach
(
$result
as
$feed
)
{
$queue
->
createItem
(
$feed
);
$ids
=
\
Drupal
::
entityManager
()
->
getStorage
(
'aggregator_feed'
)
->
getFeedIdsToRefresh
();
foreach
(
Feed
::
loadMultiple
(
$ids
)
as
$feed
)
{
if
(
$queue
->
createItem
(
$feed
))
{
// Add timestamp to avoid queueing item more than once.
$feed
->
setQueuedTime
(
REQUEST_TIME
);
$feed
->
save
();
}
}
$ids
=
\
Drupal
::
entityQuery
(
'aggregator_feed'
)
->
condition
(
'queued'
,
REQUEST_TIME
-
(
3600
*
6
),
'<'
)
->
execute
();
if
(
$ids
)
{
$feeds
=
Feed
::
loadMultiple
(
$ids
);
foreach
(
$feeds
as
$feed
)
{
$feed
->
setQueuedTime
(
0
);
$feed
->
save
();
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment