之前几篇完成了缩图功能,今天在常帅的指导下,完成了golang操作队列删除图片功能,之前打算用lua-resty-redis弄,尼玛,不懂怎么触发,SB了。然后常帅说用Golang,好吧.就有了下面的代码。
package main import ( "fmt" "time" "gopkg.in/redis.v5" "strings" "os" "path/filepath" ) var client *redis.Client func main() { fmt.Println("Hello, 世界") client = redis.NewClient(&redis.Options{ Addr: "ip:6379", DialTimeout: 10 * time.Second, ReadTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second, PoolSize: 10, PoolTimeout: 30 * time.Second, }) pong, err := client.Ping().Result() fmt.Println(pong,err) if err != nil { panic(err) } for{ result, err := client.BLPop(0, "sync_img").Result() if err != nil { fmt.Println("error: %s",err) continue } img_path := result[1] if len(img_path) < 1 { fmt.Println(" path is null") continue } if(strings.Contains(img_path,"..")){ fmt.Println(" path can't contains ..") continue } // (gif|jpg|png|jpeg if(strings.HasSuffix(img_path, ".gif") || strings.HasSuffix(img_path, ".jpg") || strings.HasSuffix(img_path, ".png") || strings.HasSuffix(img_path, ".jpeg") ){ full_path := "/tmp"+img_path files, err := filepath.Glob(full_path+"*") if err != nil{ fmt.Println(err) continue } for _, f := range files{ fmt.Println("rm path :%s",f) removeErr :=os.Remove(f) if removeErr != nil { fmt.Printf("file remove Error! %s", removeErr) } else { //如果删除成功则输出 file remove OK! fmt.Print("file remove OK!") } } }else{ fmt.Println(" only receive : gif | jpg |png |jpeg ") } } }
上面的代码东拼西凑的,对了,需要先用Go安装: go get gopkg.in/redis.v5
然后在控制台执行运行命令: go run xxx.go
然后常帅说在服务器上使用root运行权限太大了,一不小心把删掉别的数据怎么办,然后找到使用nobody运行命令,修改如下
vim /etc/passwd 找到 nobody:x:99:99:Nobody:/:/sbin/nologin 改为nobody:x:99:99:Nobody:/:/bin/bash 后保存执行su -nobody
接着就可以执行: su -m nobody -c "go run redis_queue.go &"
资源: https://github.com/go-redis/redis 打完收工
来源我的博客:http://www.hihubs.com/article/271
有疑问加站长微信联系(非本文作者)