อยากทราบถ้าหากต้องการที่ทดสอบการทำงานของของ Event cron จะต้องทำอย่างไรบางครับ

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
Ittichai_chupol
PHP VIP Members
PHP VIP Members
โพสต์: 5410
ลงทะเบียนเมื่อ: 19/09/2018 10:33 am

อยากทราบถ้าหากต้องการที่ทดสอบการทำงานของของ Event cron จะต้องทำอย่างไรบางครับ

โพสต์ที่ยังไม่ได้อ่าน โดย Ittichai_chupol »

คือต้องการที่จะสร้าง Event cron ไว้สำหรับการแจ้งเตือนสมาชิกที่มีสถานะการเป็นสมาชิกตามที่กำหนดไว้ โดยจะแจ้งผ่านทั้ง Email และ pm ครับ

โดยผมได้ทดสอบสร้าง Event cron ที่ใช้สำหรับการสร้างโฟลเดอขึ้นมา ดังนี้

1. ในโฟลเดอ config ได้สร้างไฟล์ services.yml โดยมีโคดดังนี้

โค้ด: เลือกทั้งหมด


services:
    forumhulp.cron.deleteinactiveusers:
        class: forumhulp\deleteinactiveusers\cron\task\core\delete_inactive_users
        arguments:
            - @user
            - @config
            - @config_text
            - @dbal.conn
            - @log
            - %core.root_path%
            - %core.php_ext%
        calls:
            - [set_name, [forumhulp.cron.delete_inactive_users]]
        tags:
            - { name: cron.task }
    forumhulp.deleteinactiveusers.listener:
        class: forumhulp\deleteinactiveusers\event\listener
        arguments:
            - @controller.helper
        tags:
            - { name: event.listener }
            
2.โฟลเดอ event ได้สร้างไฟล์ listener ดังนี้

โค้ด: เลือกทั้งหมด

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class listener implements EventSubscriberInterface {
    protected $helper;
    public function __construct(\phpbb\controller\helper $helper) {
        $this->helper = $helper;
    }

    static public function getSubscribedEvents() {
        return array(
            'core.acp_board_config_edit_add' => 'load_config_on_setup',
        );
    }
    public function load_config_on_setup($event) {
        if ($event['mode'] == 'features') {
            $display_vars = $event['display_vars'];

// ส่วววนี้เป็นส่วนสำหรับเรียกใช้งาน cron
            $add_config_var['delete_inactive_users_days'] = [
                'lang' => 'INACTIVE_USERS_DAYS',
                'validate' => 'int',
                'type' => 'custom',
                'function' => __NAMESPACE__ . '\listener::delete_users_options',
                'explain' => true
            ];
            $display_vars['vars'] = phpbb_insert_config_array($display_vars['vars'], $add_config_var, array('after' => 'allow_quick_reply'));
            $event['display_vars'] = array('title' => $display_vars['title'], 'vars' => $display_vars['vars']);
        }
    }
}

3. สร้าง โฟลเดอร์ cron / task / core แล้วสร้างไฟล์ชื่อ delete_inactive_users.php

โค้ด: เลือกทั้งหมด

<?php
namespace forumhulp\deleteinactiveusers\cron\task\core;
class delete_inactive_users extends \phpbb\cron\task\base {

    protected $user;
    protected $config;
    protected $config_text;
    protected $db;
    protected $log;
    protected $phpbb_root_path;
    protected $php_ext;

    public function __construct(\phpbb\user $user, \phpbb\config\config $config, \phpbb\config\db_text $config_text, \phpbb\db\driver\driver_interface $db, \phpbb\log\log $log, $phpbb_root_path, $php_ext) {
        $this->user = $user;
        $this->config = $config;
        $this->config_text = $config_text;
        $this->db = $db;
        $this->log = $log;
        $this->phpbb_root_path = $phpbb_root_path;
        $this->php_ext = $php_ext;
    }
    public function run() {
    
    // ส่วนนี้ไว้สำหรับการสร้างโฟลเดอร์
        $path_img = './images/test';
        if (!file_exists($path_img)) {
            mkdir($path_img);
            chmod($path_img, 0777);
        }
    }
    public function is_runnable() {
        return (bool) $this->config['delete_inactive_users_days'];
    }

    public function should_run() {
        return $this->config['delete_inactive_users_last_gc'] < time() - $this->config['delete_inactive_users_gc'];
    }

}

โดยปัญหาก็คือ ไม่มีการสร้างโฟลเดอร์ตามที่ต้อง จึงอย่า่งทราบว่าจะปรับแก้ไขอย่างไรครับ

โดยผมได้ลองศึกษาการสร้าง cron มาจาก https://www.phpbb.com/customise/db/exte ... s/faq/1746

และได้ทดลองนำ Extension จาก
forumhulp.tar.gz
(6.28 KiB) ดาวน์โหลดแล้ว 72 ครั้ง
ครับ
ขอให้วันนี้เป็นวันที่ดี
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41131
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: อยากทราบถ้าหากต้องการที่ทดสอบการทำงานของของ Event cron จะต้องทำอย่างไรบางครับ

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ตั้งค่าให้ admin ให้ cron ทำงานหรือยัง
ถ้าตั้งแล้ว

ลอง สร้างใน __construct เลยครับ
จะได้รู้ว่า class นี้ถูกเรียก หรือยัง ถ้ายังไม่ได้ แสดงว่าโครงสร้างผิด
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 57