{"id":5423,"date":"2020-01-31T17:33:31","date_gmt":"2020-01-31T17:33:31","guid":{"rendered":"https:\/\/musilda.cz\/?p=5423"},"modified":"2020-01-31T17:33:31","modified_gmt":"2020-01-31T17:33:31","slug":"action-scheduler","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/action-scheduler\/","title":{"rendered":"Action scheduler"},"content":{"rendered":"\n

WP Cron a Action Scheduler jsou dva zp\u016fsoby, jak ve WordPressu pl\u00e1novat \u00falohy. Prvn\u00ed z nich je obsa\u017een\u00fd ve WordPressu a druh\u00fd je distribuov\u00e1n s WooCommerce. <\/p>\n\n\n\n

O to com co je a jak funguje WP Cron<\/a> jsem ji\u017e psal d\u0159\u00edve. Dnes se pod\u00edv\u00e1me, jak funguje Action Scheduler. <\/p>\n\n\n\n

Scheduled Actions<\/h2>\n\n\n\n

V p\u0159\u00edpad\u011b, \u017ee pou\u017e\u00edv\u00e1te WooCommerce, mohli jste od jist\u00e9 verze zaznamenat v N\u00e1stroj\u00edch novou polo\u017eku nazvanou Scheduled actions. <\/p>\n\n\n\n

WooCommerce ji pou\u017e\u00edv\u00e1 na napl\u00e1nov\u00e1n\u00ed \u00faloh, kter\u00e9 chce v budoucnu prov\u00e9st. V n\u011bkter\u00e9m z dal\u0161\u00edch \u010dl\u00e1nc\u00edch budu ps\u00e1t o lookup tabulk\u00e1ch produkt\u016f, v kter\u00fdch jsou nov\u011b ulo\u017eena data produkt\u016f. <\/p>\n\n\n\n

Ta byla p\u016fvodn\u011b v postmeta tabulce a v\u00fdvoj\u00e1\u0159i WooCommerce se rozhodli, p\u0159esunout tato data do vlastn\u00ed tabulky. <\/p>\n\n\n\n

Ur\u010dit\u011b jste po aktualizaci zaznamenali upozorn\u011bn\u00ed, \u017ee je t\u0159eba prov\u00e9st aktualizaci datab\u00e1ze WooCommerce a kdy\u017e jste aktualizaci spustili, zobrazilo se info, \u017ee aktualizace bude prob\u00edhat na pozad\u00ed. <\/p>\n\n\n\n

Pokud by jste se v tu chv\u00edli pod\u00edvali do Scheduled actions, vid\u011bli by jste seznam napl\u00e1novan\u00fdch \u00faloh, s aktualizacemi dat produkt\u016f, nastaven\u00fdch na d\u00e1vky. T\u00edm WooCommerce dos\u00e1hla toho, \u017ee p\u0159esun dat prob\u00edhal postupn\u011b a v\u00fdkon e-shopu to nijak neovlivnilo. <\/p>\n\n\n\n

Action Scheduler<\/h2>\n\n\n\n

Kdy\u017e si ve WooCommerce pluginu otev\u0159ete slo\u017eku includes\/libraries, najdete knihovnu scheduleru. Krom\u011b toho, \u017ee je knihovna distribuov\u00e1na s Woo, m\u016f\u017eete ji pou\u017e\u00edt jako samostatnou knihovnu do sv\u00e9ho pluginu, nebo jako plugin a ve vlastn\u00edch pluginech, nebo \u0161ablon\u011b jenom pl\u00e1novat \u00falohy. <\/p>\n\n\n\n

K \u010demu se d\u00e1 scheduler vyu\u017e\u00edt? WooCommerce p\u0159es n\u011bj pl\u00e1nuje opakovan\u00e9 platby, aktualizace stavu objedn\u00e1vek, e-maily, kter\u00e9 se maj\u00ed odeslat s n\u011bjakou prodlevou, nebo aktualizace datab\u00e1ze po updatu. <\/p>\n\n\n\n

Instalace scheduleru<\/h2>\n\n\n\n

Scheduler je dostupn\u00fd na GitHubu, kde si m\u016f\u017eete st\u00e1hnout zip soubor s releasem, kter\u00fd pot\u0159ebujete – https:\/\/github.com\/woocommerce\/action-scheduler\/releases<\/a>. Pak sta\u010d\u00ed aktivovat jako oby\u010dejn\u00fd WordPress plugin.<\/p>\n\n\n\n

Dal\u0161\u00ed mo\u017enost\u00ed je vlo\u017eit knihovnu do pluginu, nebo \u0161ablony. Knihovnu pak mus\u00edte v k\u00f3du inicializovat:<\/p>\n\n\n\n

require_once( plugin_dir_path( __FILE__ ) . '\/libraries\/action-scheduler\/action-scheduler.php' );<\/code><\/pre>\n\n\n\n

Proto\u017ee action scheduler zaregistruje s\u00e1m sebe do hooku plugins_loaded s prioritou 0, vy mus\u00edte zav\u011bsit require souboru do plugins_loaded p\u0159ed prioritu 0, nap\u0159\u00edklad -10 ( negativn\u00ed priorita ). <\/p>\n\n\n\n

Pou\u017eit\u00ed scheduleru<\/h2>\n\n\n\n

Pou\u017eit\u00ed je podobn\u00e9, jako pou\u017eit\u00ed WP Cron \u00falohy. Kompletn\u00ed p\u0159ehled najdete zde https:\/\/actionscheduler.org\/api\/<\/a> , my si uk\u00e1\u017eeme p\u0159\u00edklad a napl\u00e1nujeme si odesl\u00e1n\u00ed e-mailu za 10 minut od vytvo\u0159en\u00ed tasku. <\/p>\n\n\n\n

Nejprve zaregistrujeme \u00falohu:<\/p>\n\n\n\n

as_schedule_single_action( time() + 600, 'musilda_scheduled_mail_action', array( 'message' => $message ) );<\/code><\/pre>\n\n\n\n

Tato funkce vytvo\u0159\u00ed task, kter\u00fd se provede jednou, n\u011bkdy v budoucnu. Nap\u0159\u00edklad as_schedule_reccuring_action vytv\u00e1\u0159\u00ed opakovan\u00e9 \u00falohy. <\/p>\n\n\n\n

Funkce p\u0159ij\u00edm\u00e1 \u010dty\u0159i argumenty<\/strong><\/p>\n\n\n\n

  • $time – term\u00edn proveden\u00ed \u00falohy<\/li>
  • $hook – akci, do kter\u00e9 zav\u011bs\u00edme callback<\/li>
  • $args – hodnoty, kter\u00e9 chceme p\u0159edat do callbacku<\/li>
  • $groups – zde nepou\u017eijeme, jde jen o sdru\u017eov\u00e1n\u00ed \u00faloh do skupin <\/li><\/ul>\n\n\n\n

    Zaregistrujeme hook s callbackem:<\/p>\n\n\n\n

    add_action( 'musilda_scheduled_mail_action', 'musilda_scheduled_mail', 10, 1 );<\/code><\/pre>\n\n\n\n

    Vytvo\u0159\u00edme callback:<\/p>\n\n\n\n

    function musilda_scheduled_mail( $message ){\n         mail( 'musilekmail@seznam.cz', 'Scheduler test', $message );\n     }<\/code><\/pre>\n\n\n\n

    Cel\u00fd k\u00f3d:<\/p>\n\n\n\n

    function musilda_scheduled_mail( $message ){\n        mail( 'musilekmail@seznam.cz', 'Scheduler test', $message );\n}\nadd_action( 'musilda_scheduled_mail_action', 'musilda_scheduled_mail', 10, 1 );\n\n$message = 'Test e-mailu.';\nas_schedule_single_action( time() + 600, 'musilda_scheduled_mail_action', array( 'message' => $message ) );<\/code><\/pre>\n\n\n\n

    N\u00e1sledn\u011b se n\u00e1m \u00faloha zobraz\u00ed ve Scheduled actions:<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    A do e-mailov\u00e9 schr\u00e1nky p\u0159ijde e-mail: <\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    Pro\u010d pou\u017e\u00edvat Action Scheduler?<\/h2>\n\n\n\n

    Prvn\u00ed ot\u00e1zka kter\u00e1 v\u00e1s napadne, bude, pro\u010d to pou\u017e\u00edvat? V\u017edy\u0165 WordPress m\u00e1 WP Cron. <\/p>\n\n\n\n

    To je pravda, ale scheduler byl vyvinut s ohledem na v\u00fdkon. D\u00edky tomu, \u017ee jej pou\u017e\u00edv\u00e1 WooCommerce, m\u00e1 grafick\u00e9 rozhran\u00ed, kter\u00e9 WP Cron nem\u00e1 a je ur\u010den k prov\u00e1d\u011bn\u00ed mno\u017estv\u00ed n\u00e1ro\u010dn\u00fdch \u00faloh, bez toho, aby to v\u00fdznamn\u011b omezovalo chod webu. <\/p>\n\n\n\n

    Oproti b\u011b\u017en\u00e9mu WordPress webu, je e-shop pom\u011brn\u011b rozs\u00e1hl\u00fd, nez\u0159\u00eddka s deseti tis\u00edci polo\u017ekami, tis\u00edci objedn\u00e1vkami a mno\u017estv\u00edm obr\u00e1zk\u016f. <\/p>\n\n\n\n

    Action scheduler nespust\u00ed dal\u0161\u00ed \u00falohu pokud:<\/strong><\/p>\n\n\n\n

    • je pam\u011b\u0165 vyu\u017eita v\u00edce ne\u017e z 90ti %<\/li>
    • by zpracov\u00e1n\u00ed n\u00e1sleduj\u00edch t\u0159\u00ed \u00faloh m\u011blo p\u0159ekro\u010dit 30 vte\u0159in po\u017eadovan\u00e9ho \u010dasu<\/li><\/ul>\n\n\n\n

      Zv\u00fd\u0161en\u00ed \u010dasov\u00e9ho limitu<\/strong><\/p>\n\n\n\n

      Scheduler umo\u017e\u0148uje pomoc\u00ed action_scheduler_queue_runner_time_limit<\/code> zv\u00fd\u0161it time limit, ale i tak jste omezen\u00ed nastaven\u00edm php na va\u0161em serveru (hostingu). <\/p>\n\n\n\n

      Zv\u00fd\u0161en\u00ed d\u00e9lky d\u00e1vky<\/strong><\/p>\n\n\n\n

      Vy v\u00fdchoz\u00edm nastaven\u00ed pou\u0161t\u00ed scheduler d\u00e1vky o d\u00e9lce 25 \u00faloh. V p\u0159\u00edpad\u011b, \u017ee v\u00edte, \u017ee va\u0161e \u00falohy jsou drobn\u00e9ho r\u00e1zu, m\u016f\u017eete zv\u00fd\u0161it d\u00e9lku d\u00e1vky pomoc\u00ed action_scheduler_queue_runner_batch_size<\/code>.<\/p>\n\n\n\n

      Tak\u00e9 je k dispozici mo\u017enost spou\u0161t\u011bt soub\u011b\u017en\u011b v\u00edce d\u00e1vek, co\u017e v z\u00e1kladu nelze. To ale m\u016f\u017ee m\u00edt dopad na v\u00fdkon va\u0161eho webu. tak\u017ee s t\u00edm opatrn\u011b. <\/p>\n\n\n\n

      Ukl\u00e1d\u00e1n\u00ed dat \u00faloh<\/h2>\n\n\n\n

      V\u011b\u010dn\u00fdm t\u00e9matem WordPressu je jeho datab\u00e1ze a ukl\u00e1d\u00e1n\u00ed dat. Scheduler vych\u00e1z\u00ed z n\u00e1vrhu data storage WooCommerce 3.+, tak\u017ee jsou v\u0161echna data ukl\u00e1d\u00e1na do zvl\u00e1\u0161tn\u00edch tabulek a nemus\u00edte m\u00edt starost se zapl\u0148ov\u00e1n\u00edm tabulek, jako jsou post, postmeta a options. Data storage t\u0159\u00eddy nav\u00edc umo\u017e\u0148uj\u00ed p\u0159epsat um\u00edst\u011bn\u00ed dat, tak\u017ee si m\u016f\u017eete vytvo\u0159it i vlastn\u00ed tabulky, ale mysl\u00edm si, \u017ee to je zbyte\u010dn\u00e9.<\/p>\n\n\n\n

      <\/p>\n","protected":false},"excerpt":{"rendered":"

      WP Cron a Action Scheduler jsou dva zp\u016fsoby, jak ve WordPressu pl\u00e1novat \u00falohy. Prvn\u00ed z nich je obsa\u017een\u00fd ve WordPressu a druh\u00fd je distribuov\u00e1n s WooCommerce. O to com co je a jak funguje WP Cron jsem ji\u017e psal d\u0159\u00edve. Dnes se pod\u00edv\u00e1me, jak funguje Action Scheduler. Scheduled Actions V p\u0159\u00edpad\u011b, \u017ee pou\u017e\u00edv\u00e1te WooCommerce, mohli<\/p>\n","protected":false},"author":1,"featured_media":8732,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[25],"tags":[432],"class_list":["post-5423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-woocommerce","tag-woocommerce-action-scheduler"],"_links":{"self":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/5423"}],"collection":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/comments?post=5423"}],"version-history":[{"count":0,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/posts\/5423\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media\/8732"}],"wp:attachment":[{"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/media?parent=5423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/categories?post=5423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/affinite.io\/cs\/wp-json\/wp\/v2\/tags?post=5423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}